Dies ist ein Codebeispiel, das "myfunction (x: f64) -> Array2
Cargo.toml
[lib]
name = "mypackage"
crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.11", features = ["extension-module"] }
ndarray = "0.13"
numpy = "0.11"
src/lib.rs
use pyo3::prelude::*;
use ndarray::Array2;
use numpy::{IntoPyArray, PyArray2};
#[pymodule]
fn mypackage(_py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m, "myfunction")]
fn myfunction_py<'py>(py: Python<'py>, x: f64) -> &'py PyArray2<f64> {
let arr = myfunction(x);
arr.into_pyarray(py)
}
Ok(())
}
Beispiel kompilieren und ausführen:
$ cargo build --release
$ ln -s ./target/release/libmypackage.so mypackage.so
$
$ python3
>>> import mypackage
>>> x = 3.14
>>> arr = mypackage.myfunction(x)
Recommended Posts