Struct PythonProgram

Source
pub struct PythonProgram { /* private fields */ }
Expand description

An instance of code generation unit. It really is just a file with dedicated APIs to write Python into it. Most importantly: it manages indentation for you.

Implementations§

Source§

impl PythonProgram

Source

pub fn new() -> PythonProgram

Creates a named temp file to store the generated python program

Examples found in repository?
examples/save.rs (line 6)
1fn main() {
2    use pycall::MatPlotLib;
3    let mut test_map = std::collections::HashMap::new();
4    test_map.insert("hello".to_owned(), vec![56, 12, 65, 3, 21]);
5    test_map.insert("there".to_owned(), vec![6, 2, 5, 13, 1]);
6    let mut program = pycall::PythonProgram::new();
7    program
8        .define_variable("test_map", &test_map)
9        .write_line("print(test_map)");
10    program.save_as("saved.py");
11}
More examples
Hide additional examples
examples/main.rs (line 12)
1fn main() {
2    let handles = (
3        std::thread::spawn(|| pycall::plot!(&vec![0, 1, 2, 3, 2, 1, 0])),
4        std::thread::spawn(|| {
5            pycall::plot!(&vec![-3, -2, -1, 3, 4, 5, 6], &vec![0, 1, 2, 3, 2, 1, 0])
6        }),
7        std::thread::spawn(|| {
8            pycall::plot!(&vec![0, 1, 2, 3, 4, 5, 6], &vec![0, 1, 2, 3, 2, 1, 0], "+")
9        }),
10    );
11    use pycall::MatPlotLib;
12    let mut program = pycall::PythonProgram::new();
13    program
14        .import_pyplot_as_plt()
15        .plot_y(&vec![0, 1, 2, 3, 2, 1, 0])
16        .plot_xyargs(&vec![0, 1, 2, 3, 4, 5, 6], &vec![0, 1, 2, 3, 2, 1, 0], "+")
17        .show();
18    program.background_run();
19    handles.0.join();
20    handles.1.join();
21    handles.2.join();
22}
Source

pub fn save_as<P: AsRef<Path>>(&self, path: P) -> Result<u64, Error>

Examples found in repository?
examples/save.rs (line 10)
1fn main() {
2    use pycall::MatPlotLib;
3    let mut test_map = std::collections::HashMap::new();
4    test_map.insert("hello".to_owned(), vec![56, 12, 65, 3, 21]);
5    test_map.insert("there".to_owned(), vec![6, 2, 5, 13, 1]);
6    let mut program = pycall::PythonProgram::new();
7    program
8        .define_variable("test_map", &test_map)
9        .write_line("print(test_map)");
10    program.save_as("saved.py");
11}
Source

pub fn run(&self) -> Result<Output, Error>

Runs the program using python3

Source

pub fn background_run(self) -> JoinGuard<Result<Output, Error>>

Spawns a thread to run the program using python3. The returned JoinGuard ensures that the program will be ran to completion.

Examples found in repository?
examples/main.rs (line 18)
1fn main() {
2    let handles = (
3        std::thread::spawn(|| pycall::plot!(&vec![0, 1, 2, 3, 2, 1, 0])),
4        std::thread::spawn(|| {
5            pycall::plot!(&vec![-3, -2, -1, 3, 4, 5, 6], &vec![0, 1, 2, 3, 2, 1, 0])
6        }),
7        std::thread::spawn(|| {
8            pycall::plot!(&vec![0, 1, 2, 3, 4, 5, 6], &vec![0, 1, 2, 3, 2, 1, 0], "+")
9        }),
10    );
11    use pycall::MatPlotLib;
12    let mut program = pycall::PythonProgram::new();
13    program
14        .import_pyplot_as_plt()
15        .plot_y(&vec![0, 1, 2, 3, 2, 1, 0])
16        .plot_xyargs(&vec![0, 1, 2, 3, 4, 5, 6], &vec![0, 1, 2, 3, 2, 1, 0], "+")
17        .show();
18    program.background_run();
19    handles.0.join();
20    handles.1.join();
21    handles.2.join();
22}
Source

pub fn flush(&mut self) -> &mut Self

Ensures that the internal file has been flushed. Typically not necessary.

Source

pub fn indent(&mut self, n: isize) -> &mut Self

Moves the indentation level by n. However, I recommend using the dedicated functions when possible/

Source

pub fn end_block(&mut self) -> &mut Self

Removes one indentation level from the cursor. You should call this whenever you’re done with a scope.

Source

pub fn define_variable<T: AsPythonLitteral + ?Sized>( &mut self, name: &str, value: &T, ) -> &mut Self

Writes a line assigning value formatted as a python literal to name

Examples found in repository?
examples/save.rs (line 8)
1fn main() {
2    use pycall::MatPlotLib;
3    let mut test_map = std::collections::HashMap::new();
4    test_map.insert("hello".to_owned(), vec![56, 12, 65, 3, 21]);
5    test_map.insert("there".to_owned(), vec![6, 2, 5, 13, 1]);
6    let mut program = pycall::PythonProgram::new();
7    program
8        .define_variable("test_map", &test_map)
9        .write_line("print(test_map)");
10    program.save_as("saved.py");
11}
Source

pub fn import(&mut self, dependency: &str) -> &mut Self

Writes an import statement for your dependency

Source

pub fn import_as(&mut self, dependency: &str, rename: &str) -> &mut Self

Writes an import statement for your dependency as rename

Source

pub fn write_line(&mut self, line: &str) -> &mut Self

Writes whatever line you passed it, indented at the proper level.

Examples found in repository?
examples/save.rs (line 9)
1fn main() {
2    use pycall::MatPlotLib;
3    let mut test_map = std::collections::HashMap::new();
4    test_map.insert("hello".to_owned(), vec![56, 12, 65, 3, 21]);
5    test_map.insert("there".to_owned(), vec![6, 2, 5, 13, 1]);
6    let mut program = pycall::PythonProgram::new();
7    program
8        .define_variable("test_map", &test_map)
9        .write_line("print(test_map)");
10    program.save_as("saved.py");
11}
Source

pub fn if(&mut self, condition: &str) -> &mut Self

Writes an if, using your condition as a test, and increments indentation.

Source

pub fn elif(&mut self, condition: &str) -> &mut Self

Decrements indentation, writes an elif, using your condition as a test, and increments indentation.

Source

pub fn else(&mut self) -> &mut Self

Decrements indentation, writes an else, using your condition as a test, and increments indentation.

Source

pub fn for(&mut self, range: &str) -> &mut Self

Writes “for range:”, and increments indentation.

Source

pub fn while(&mut self, condition: &str) -> &mut Self

Writes a while, using your condition as a test, and increments indentation.

Trait Implementations§

Source§

impl Display for PythonProgram

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl MatPlotLib for PythonProgram

Source§

fn import_pyplot_as_plt(&mut self) -> &mut Self

Source§

fn plot_y<Y: AsPythonLitteral>(&mut self, y: &Y) -> &mut Self

Source§

fn plot_xy<X: AsPythonLitteral, Y: AsPythonLitteral>( &mut self, x: &X, y: &Y, ) -> &mut Self

Source§

fn plot_xyargs<X: AsPythonLitteral, Y: AsPythonLitteral>( &mut self, x: &X, y: &Y, args: &str, ) -> &mut Self

Source§

fn semilogy_y<Y: AsPythonLitteral>(&mut self, y: &Y) -> &mut Self

Source§

fn semilogy_xy<X: AsPythonLitteral, Y: AsPythonLitteral>( &mut self, x: &X, y: &Y, ) -> &mut Self

Source§

fn semilogy_xyargs<X: AsPythonLitteral, Y: AsPythonLitteral>( &mut self, x: &X, y: &Y, args: &str, ) -> &mut Self

Source§

fn show(&mut self) -> &mut Self

Source§

impl Write for PythonProgram

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Error>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<(), Error>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V