我想包装一个函数,该函数采用一维 NDArray (rust-numpy) 和一个 usize 作为参数,并使用 PyO3 返回一个一维数组以从 python 调用代码。不幸的是,我找不到一个很好的例子来说明如何处理 PyO3 中的数组。这是我到目前为止的代码:
use numpy::ndarray::prelude::*;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
#[pyfunction]
fn sma(prices: &Array1<f32>, period: usize) -> PyResult<Array1<f32>> {
let length = prices.len() - period +1;
let mut result = Array1::<f32>::zeros(length);
for i in 0..length {
let slice = prices.slice(s![i..i+period]);
result[i] = slice.sum()/(period as f32);
}
Ok(result)
}
#[pymodule]
fn panther(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sma, m)?)?;
Ok(())
}
我添加了装饰器和模块包装功能。现在我收到此错误:
error[E0277]: the trait bound `Result<ArrayBase<OwnedRepr<f32>, Dim<[usize; 1]>>, PyErr>: IntoPyCallbackOutput<_>` is not satisfied.
这是我的 cargo.toml:
[package]
name = "panther"
version = "0.1.0"
edition = "2021"
[lib]
name = "panther"
crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.12.3", features = ["extension-module"] }
numpy = "0.15"
有人可以告诉我如何实际修改 SMA 函数以与 PyO3 一起使用吗?谢谢!