Struct py_env::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)
3
4
5
6
7
8
9
fn main() -> PyResult<()> {
    let out = |line: &str| println!("{}", line);
    let err = |line: &str| eprintln!("{}", line);
    PyEnv::new("./py_test/run", out, err)
        .execute("print('hello world')")?;
    Ok(())
}
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)
3
4
5
6
7
8
9
pub fn main() {
    // This code attempts to install faker and run a script dependent on it, panicing upon shell
    // command error (not Python code error).
    PyEnv::at("./py_test")
        .try_install("faker")
        .try_execute("import faker; print(faker.Faker().name())");
}
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)
3
4
5
6
7
8
9
pub fn main() {
    // This code attempts to install faker and run a script dependent on it, panicing upon shell
    // command error (not Python code error).
    PyEnv::at("./py_test")
        .try_install("faker")
        .try_execute("import faker; print(faker.Faker().name())");
}
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)
3
4
5
6
7
8
9
fn main() -> PyResult<()> {
    let out = |line: &str| println!("{}", line);
    let err = |line: &str| eprintln!("{}", line);
    PyEnv::new("./py_test/run", out, err)
        .execute("print('hello world')")?;
    Ok(())
}
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)
3
4
5
6
7
8
9
pub fn main() {
    // This code attempts to install faker and run a script dependent on it, panicing upon shell
    // command error (not Python code error).
    PyEnv::at("./py_test")
        .try_install("faker")
        .try_execute("import faker; print(faker.Faker().name())");
}
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>,

§

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

§

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.