Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename some conversion functions #75

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/barplot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{vector_to_array, vector_to_strings, AsVector, GraphMaker};
use super::{vector_to_array, generate_list_quoted, AsVector, GraphMaker};
use std::fmt::Write;

/// Generates a Barplot plot
Expand Down Expand Up @@ -151,7 +151,7 @@ impl Barplot {
vector_to_array(&mut self.buffer, "y", y);
let opt = self.options();
if self.colors.len() > 0 {
vector_to_strings(&mut self.buffer, "colors", self.colors.as_slice());
generate_list_quoted(&mut self.buffer, "colors", self.colors.as_slice());
}
if self.bottom.len() > 0 {
vector_to_array(&mut self.buffer, "bottom", &self.bottom);
Expand Down Expand Up @@ -181,11 +181,11 @@ impl Barplot {
T: AsVector<'a, U>,
U: 'a + std::fmt::Display,
{
vector_to_strings(&mut self.buffer, "x", x);
generate_list_quoted(&mut self.buffer, "x", x);
vector_to_array(&mut self.buffer, "y", y);
let opt = self.options();
if self.colors.len() > 0 {
vector_to_strings(&mut self.buffer, "colors", self.colors.as_slice());
generate_list_quoted(&mut self.buffer, "colors", self.colors.as_slice());
}
if self.bottom.len() > 0 {
vector_to_array(&mut self.buffer, "bottom", &self.bottom);
Expand Down
4 changes: 2 additions & 2 deletions src/contour.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{matrix_to_array, vector_to_array, vector_to_strings, AsMatrix, GraphMaker};
use super::{matrix_to_array, vector_to_array, generate_list_quoted, AsMatrix, GraphMaker};
use std::fmt::Write;

/// Generates a contour plot
Expand Down Expand Up @@ -127,7 +127,7 @@ impl Contour {
matrix_to_array(&mut self.buffer, "y", y);
matrix_to_array(&mut self.buffer, "z", z);
if self.colors.len() > 0 {
vector_to_strings(&mut self.buffer, "colors", &self.colors);
generate_list_quoted(&mut self.buffer, "colors", &self.colors);
}
if self.levels.len() > 0 {
vector_to_array(&mut self.buffer, "levels", &self.levels);
Expand Down
40 changes: 20 additions & 20 deletions src/conversions.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
use super::{AsMatrix, AsVector};
use std::fmt::Write;

/// Converts vector to a Python list of numbers
pub(crate) fn vector_to_numbers<T>(buf: &mut String, name: &str, vector: &[T])
/// Generates a Python list
pub(crate) fn generate_list<T>(buf: &mut String, name: &str, data: &[T])
where
T: std::fmt::Display,
{
write!(buf, "{}=[", name).unwrap();
for val in vector.into_iter() {
for val in data.into_iter() {
write!(buf, "{},", val).unwrap();
}
write!(buf, "]\n").unwrap();
}

/// Converts vector to a Python list of strings
pub(crate) fn vector_to_strings<T>(buf: &mut String, name: &str, vector: &[T])
/// Generates a Python list with quoted entries
pub(crate) fn generate_list_quoted<T>(buf: &mut String, name: &str, data: &[T])
where
T: std::fmt::Display,
{
write!(buf, "{}=[", name).unwrap();
for val in vector.into_iter() {
for val in data.into_iter() {
write!(buf, "'{}',", val).unwrap();
}
write!(buf, "]\n").unwrap();
Expand All @@ -39,13 +39,13 @@ where
write!(buf, "],dtype=float)\n").unwrap();
}

/// Converts a matrix to a nested Python list
pub(crate) fn matrix_to_list<T>(buf: &mut String, name: &str, matrix: &Vec<Vec<T>>)
/// Generates a nested Python list
pub(crate) fn generate_nested_list<T>(buf: &mut String, name: &str, data: &Vec<Vec<T>>)
where
T: std::fmt::Display,
{
write!(buf, "{}=[", name).unwrap();
for row in matrix.into_iter() {
for row in data.into_iter() {
write!(buf, "[").unwrap();
for val in row.into_iter() {
write!(buf, "{},", val).unwrap();
Expand Down Expand Up @@ -77,17 +77,17 @@ where

#[cfg(test)]
mod tests {
use super::{matrix_to_array, matrix_to_list, vector_to_array, vector_to_numbers, vector_to_strings};
use super::{generate_list, generate_list_quoted, generate_nested_list, matrix_to_array, vector_to_array};

#[test]
fn vector_to_numbers_works() {
fn generate_list_works() {
let mut buf = String::new();
let x: Vec<f64> = vec![0.1, 0.2, 0.3];
let y: [f64; 3] = [1.0, 2.0, 3.0];
let z: &[f64] = &[10.0, 20.0, 30.0];
vector_to_numbers(&mut buf, "x", &x);
vector_to_numbers(&mut buf, "y", &y);
vector_to_numbers(&mut buf, "z", z);
generate_list(&mut buf, "x", &x);
generate_list(&mut buf, "y", &y);
generate_list(&mut buf, "z", z);
assert_eq!(
buf,
"x=[0.1,0.2,0.3,]\n\
Expand All @@ -97,14 +97,14 @@ mod tests {
}

#[test]
fn vector_to_strings_works() {
fn generate_list_quoted_works() {
let mut buf = String::new();
let x: Vec<&str> = vec!["red", "green", "blue"];
let y: [String; 3] = ["cyan".to_string(), "magenta".to_string(), "white".to_string()];
let z: &[&str] = &["#f00", "#0f0", "#00f"];
vector_to_strings(&mut buf, "x", &x);
vector_to_strings(&mut buf, "y", &y);
vector_to_strings(&mut buf, "z", z);
generate_list_quoted(&mut buf, "x", &x);
generate_list_quoted(&mut buf, "y", &y);
generate_list_quoted(&mut buf, "z", z);
assert_eq!(
buf,
"x=['red','green','blue',]\n\
Expand All @@ -131,10 +131,10 @@ mod tests {
}

#[test]
fn matrix_to_list_works() {
fn generate_nested_list_works() {
let mut buf = String::new();
let a = vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0], vec![6.0, 7.0, 8.0, 9.0]];
matrix_to_list(&mut buf, "a", &a);
generate_nested_list(&mut buf, "a", &a);
assert_eq!(buf, "a=[[1,2,3,],[4,5,],[6,7,8,9,],]\n");
}

Expand Down
8 changes: 4 additions & 4 deletions src/histogram.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{matrix_to_list, vector_to_strings, GraphMaker};
use super::{generate_list_quoted, generate_nested_list, GraphMaker};
use std::fmt::Write;

/// Generates a Histogram plot
Expand Down Expand Up @@ -92,10 +92,10 @@ impl Histogram {
U: std::fmt::Display,
{
let opt = self.options();
matrix_to_list(&mut self.buffer, "values", values);
vector_to_strings(&mut self.buffer, "labels", labels);
generate_nested_list(&mut self.buffer, "values", values);
generate_list_quoted(&mut self.buffer, "labels", labels);
if self.colors.len() > 0 {
vector_to_strings(&mut self.buffer, "colors", self.colors.as_slice());
generate_list_quoted(&mut self.buffer, "colors", self.colors.as_slice());
}
write!(&mut self.buffer, "plt.hist(values,label=labels{})\n", &opt).unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions src/legend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{vector_to_numbers, GraphMaker};
use super::{generate_list, GraphMaker};
use std::fmt::Write;

/// Generates a Legend
Expand Down Expand Up @@ -99,7 +99,7 @@ impl Legend {
pub fn draw(&mut self) {
let opt = self.options();
if self.outside {
vector_to_numbers(&mut self.buffer, "coo", self.x_coords.as_slice());
generate_list(&mut self.buffer, "coo", self.x_coords.as_slice());
}
write!(&mut self.buffer, "h,l=plt.gca().get_legend_handles_labels()\n").unwrap();
write!(&mut self.buffer, "if len(h)>0 and len(l)>0:\n").unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/plot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{call_python3, vector_to_array, vector_to_strings, AsVector, Legend, StrError, SuperTitleParams};
use super::{call_python3, generate_list_quoted, vector_to_array, AsVector, Legend, StrError, SuperTitleParams};
use std::ffi::OsStr;
use std::fmt::Write;
use std::fs::{self, File};
Expand Down Expand Up @@ -709,7 +709,7 @@ impl Plot {
{
assert_eq!(ticks.vec_size(), labels.len());
vector_to_array(&mut self.buffer, "tx", ticks);
vector_to_strings(&mut self.buffer, "lx", labels);
generate_list_quoted(&mut self.buffer, "lx", labels);
write!(
&mut self.buffer,
"plt.gca().set_xticks(tx)\nplt.gca().set_xticklabels(lx)\n"
Expand All @@ -727,7 +727,7 @@ impl Plot {
{
assert_eq!(ticks.vec_size(), labels.len());
vector_to_array(&mut self.buffer, "ty", ticks);
vector_to_strings(&mut self.buffer, "ly", labels);
generate_list_quoted(&mut self.buffer, "ly", labels);
write!(
&mut self.buffer,
"plt.gca().set_yticks(ty)\nplt.gca().set_yticklabels(ly)\n"
Expand Down
Loading