I am using Pyo3 to call Rust functions from Python and vice versa.
I am trying to achieve the following:
Python calls rust_function_1
Rust function rust_function_1 calls Python function python_function passing Rust function rust_function_2 as a…
I have a rust and python project that I am building using Maturin(https://github.com/PyO3/maturin). It says that it requires a pyproject.toml file for the python dependencies.
I have a dependency of uvloop, which is not supported on windows and arm…
I am using the Rust bindings of the Python interpreter provided by the crate pyo3.
I have the following code:
fn run(script: &str) {
Python::with_gil(|py| py.run(script, None, None)).unwrap();
}
fn main() {
run("print('abc')");
}
I would…
I've found example of how to implement PyIterProtocol in Rust.
use pyo3::prelude::*;
use pyo3::PyIterProtocol;
use pyo3::class::iter::IterNextOutput;
#[pyclass]
struct Iter {
count: usize
}
#[pyproto]
impl PyIterProtocol for Iter {
fn…
I have a struct
#[pyclass]
pub struct DynMat {
...
}
and I have this function
#[pyfunction]
#[text_signature = "(tensor/)"]
pub fn exp<'py>(py: Python<'py>, tensor_or_scalar: &'py PyAny) -> PyResult<&'py PyAny> {
// I need to return &PyAny…
Minimal example that works but isn't quite what I want:
use std::collections::HashMap;
use pyo3::class::basic::CompareOp;
use pyo3::class::PyObjectProtocol;
use pyo3::prelude::*;
#[pyclass]
struct MyStruct {
foo: HashMap
I would like to ask some questions about the underlying principles of python interpreters, because I didn't get much useful information during my own search.
I've been using rust to write python plugins lately, this gives a significant speedup to…
How do I correctly raise an Exception?
I've tried the following:
#[pymethods]
impl Foo {
#[new]
fn __new__(arg1: u8, ...) -> Self {
if arg1 > LIMIT {
let gil = Python::acquire_gil();
let py = gil.python();
…
I am trying to pass a Python object into rust and perform operations using the fields of the Python object.
Python:
class myclass(object):
def __init__(self):
self.a = 3
b = myclass()
print(b.a)
// 3
Rust:
#[pyfn(m, "rust_obj")]
fn…
I am building a Python package which is mostly implemented in Rust, with bindings provided by Pyo3.
Some of the Rust calls can take a long time, and I would like to be able to interrupt the process with Ctrl-c while these calls are happening.
The…
I am attempting to use the python whisper speech to text API via PyO3 rust code.
For the example given on the whisper github found here
import whisper
model = whisper.load_model("base")
result =…
Related to the solution proposed in this other question.
This code creates a polars dataframe of python dictionaries. In Python land, everything is fine. The code fails when we extract to Rust land.
use pyo3::prelude::*;
use pyo3_polars::*;
let…
As of PyO3 version 0.18.1, only fieldless enums are supported.
What are the potential implementations to support enums with fields using PyO3 if implemented manually?? pyclass is of no use here (Correct me if I am wrong).
For instance if I have to…
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…
I am trying to copy a socket and send it to a different process in Python.
The socket is created in rust and is shared as a Python object through PyO3.
Here is the shared socket code
use pyo3::prelude::*;
use socket2::{Domain, Protocol, Socket,…