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
impl PythonProgram
Sourcepub fn new() -> PythonProgram ⓘ
pub fn new() -> PythonProgram ⓘ
Creates a named temp file to store the generated python program
Examples found in repository?
More examples
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}
Sourcepub fn background_run(self) -> JoinGuard<Result<Output, Error>>
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?
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}
Sourcepub fn flush(&mut self) -> &mut Self
pub fn flush(&mut self) -> &mut Self
Ensures that the internal file has been flushed. Typically not necessary.
Sourcepub fn indent(&mut self, n: isize) -> &mut Self
pub fn indent(&mut self, n: isize) -> &mut Self
Moves the indentation level by n
. However, I recommend using the dedicated functions when possible/
Sourcepub fn end_block(&mut self) -> &mut Self
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.
Sourcepub fn define_variable<T: AsPythonLitteral + ?Sized>(
&mut self,
name: &str,
value: &T,
) -> &mut Self
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
Sourcepub fn import(&mut self, dependency: &str) -> &mut Self
pub fn import(&mut self, dependency: &str) -> &mut Self
Writes an import statement for your dependency
Sourcepub fn import_as(&mut self, dependency: &str, rename: &str) -> &mut Self
pub fn import_as(&mut self, dependency: &str, rename: &str) -> &mut Self
Writes an import statement for your dependency
as rename
Sourcepub fn write_line(&mut self, line: &str) -> &mut Self
pub fn write_line(&mut self, line: &str) -> &mut Self
Writes whatever line you passed it, indented at the proper level.
Sourcepub fn if(&mut self, condition: &str) -> &mut Self
pub fn if(&mut self, condition: &str) -> &mut Self
Writes an if, using your condition as a test, and increments indentation.
Sourcepub fn elif(&mut self, condition: &str) -> &mut Self
pub fn elif(&mut self, condition: &str) -> &mut Self
Decrements indentation, writes an elif, using your condition as a test, and increments indentation.
Sourcepub fn else(&mut self) -> &mut Self
pub fn else(&mut self) -> &mut Self
Decrements indentation, writes an else, using your condition as a test, and increments indentation.
Trait Implementations§
Source§impl Display for PythonProgram
impl Display for PythonProgram
Source§impl MatPlotLib for PythonProgram
impl MatPlotLib for PythonProgram
fn import_pyplot_as_plt(&mut self) -> &mut Self
fn plot_y<Y: AsPythonLitteral>(&mut self, y: &Y) -> &mut Self
fn plot_xy<X: AsPythonLitteral, Y: AsPythonLitteral>( &mut self, x: &X, y: &Y, ) -> &mut Self
fn plot_xyargs<X: AsPythonLitteral, Y: AsPythonLitteral>( &mut self, x: &X, y: &Y, args: &str, ) -> &mut Self
fn semilogy_y<Y: AsPythonLitteral>(&mut self, y: &Y) -> &mut Self
fn semilogy_xy<X: AsPythonLitteral, Y: AsPythonLitteral>( &mut self, x: &X, y: &Y, ) -> &mut Self
fn semilogy_xyargs<X: AsPythonLitteral, Y: AsPythonLitteral>( &mut self, x: &X, y: &Y, args: &str, ) -> &mut Self
fn show(&mut self) -> &mut Self
Source§impl Write for PythonProgram
impl Write for PythonProgram
Source§fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Source§fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)