pyo3

Embedded a #[pyclass] in another #[pyclass]

Embedded a #[pyclass] in another #[pyclass] Question: I am trying to implement a cache for the private variable of any python class. Let’s suppose I have this: #[pyclass] struct ClassA { pv: Py<PyAny>, // GIL independent type, storable in another #[pyclass] field_a: Py<PyAny>, } #[pyclass] struct ClassB { class_a: Py<ClassA>, field_b: Py<PyAny>, } In the …

Total answers: 1

How to iterate over vector of pyclass objects in rust?

How to iterate over vector of pyclass objects in rust? Question: I am using maturin and I am trying to implement the method get_car() for my class. But using the following code use pyo3::prelude::*; #[pyclass] struct Car { name: String, } #[pyclass] struct Garage { cars: Vec<Car>, } #[pymethods] impl Garage { fn get_car(&self, name: …

Total answers: 1

Can you write part of a library in rust (pyo3) but the rest in python?

Can you write part of a library in rust (pyo3) but the rest in python? Question: I want to write part of a python module in rust (with PYo3) but also partly in python, so something like… src/utils.rs: use pyo3::prelude::*; #[pyfunction] fn sum_as_string(a: usize, b: usize) -> PyResult<String> { Ok((a + b).to_string()) } #[pymodule] fn …

Total answers: 1

Exporting HashMap of HashMap to Python

Exporting HashMap of HashMap to Python Question: I have a text parser written in Rust and want to provide a Python interface to it using pyo3. The parser returns a HashMap within a HashMap and the values of the inner HashMap are of type serde_json::Value. When I try to return this as a PyObject I …

Total answers: 1

Return reference to member field in PyO3

Return reference to member field in PyO3 Question: Suppose I have a Rust struct like this struct X{…} struct Y{ x:X } I’d like to be able to write python code that accesses X through Y y = Y() y.x.some_method() What would be the best way to implement it in PyO3? Currently I made two …

Total answers: 2