Struct PyEnv

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

A Python environment that can install packages and execute code.

Implementations§

Source§

impl PyEnv

Source

pub fn new( path: impl Into<PathBuf>, std_out: impl Fn(&str) + 'static, std_err: impl Fn(&str) + 'static, ) -> Self

Constructor for piping stdout and stderr to a custom stream. Use at() if you want to inherit the streams.

Examples found in repository?
examples/pipe.rs (line 6)
3fn main() -> PyResult<()> {
4    let out = |line: &str| println!("{}", line);
5    let err = |line: &str| eprintln!("{}", line);
6    PyEnv::new("./py_test/run", out, err)
7        .execute("print('hello world')")?;
8    Ok(())
9}
Source

pub fn at(path: impl Into<PathBuf>) -> Self

Constructor inheriting default stdout and stderr; use new() to customize the streams.

Examples found in repository?
examples/unwrapped.rs (line 6)
3pub fn main() {
4    // This code attempts to install faker and run a script dependent on it, panicing upon shell
5    // command error (not Python code error).
6    PyEnv::at("./py_test")
7        .try_install("faker")
8        .try_execute("import faker; print(faker.Faker().name())");
9}
Source

pub fn install(&self, package_name: &str) -> PyResult<&Self>

Installs a package in the PyEnv, returning itself to easily chain dependencies.

Source

pub fn try_install(&self, package_name: &str) -> &Self

An unwrapped install() run, which panics upon failure. See install() for the version which returns a PyResult.

Examples found in repository?
examples/unwrapped.rs (line 7)
3pub fn main() {
4    // This code attempts to install faker and run a script dependent on it, panicing upon shell
5    // command error (not Python code error).
6    PyEnv::at("./py_test")
7        .try_install("faker")
8        .try_execute("import faker; print(faker.Faker().name())");
9}
Source

pub fn execute(&self, code: &str) -> PyResult<&Self>

Executes arbitrary code in the PyEnv, returning itself to easily chain runs.

Examples found in repository?
examples/pipe.rs (line 7)
3fn main() -> PyResult<()> {
4    let out = |line: &str| println!("{}", line);
5    let err = |line: &str| eprintln!("{}", line);
6    PyEnv::new("./py_test/run", out, err)
7        .execute("print('hello world')")?;
8    Ok(())
9}
Source

pub fn try_execute(&self, code: &str) -> &Self

An unwrapped execute() run, which panics upon failure. See execute() for the version which returns a PyResult.

Examples found in repository?
examples/unwrapped.rs (line 8)
3pub fn main() {
4    // This code attempts to install faker and run a script dependent on it, panicing upon shell
5    // command error (not Python code error).
6    PyEnv::at("./py_test")
7        .try_install("faker")
8        .try_execute("import faker; print(faker.Faker().name())");
9}
Source

pub fn persistent(&mut self, persistent: bool) -> &Self

Makes the environment impersistent beyond the PyEnv, deleting it upon dropping

Trait Implementations§

Source§

impl Drop for PyEnv

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for PyEnv

§

impl !RefUnwindSafe for PyEnv

§

impl !Send for PyEnv

§

impl !Sync for PyEnv

§

impl Unpin for PyEnv

§

impl !UnwindSafe for PyEnv

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, 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.