From d54f71ddb6adc2211213ed0f421e6abba35af1db Mon Sep 17 00:00:00 2001 From: Finkelman Date: Mon, 23 Jan 2017 16:47:30 -0500 Subject: [PATCH] add readme --- src/lib.rs | 10 +++++++++- test.py | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6204b52..4a28fbd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,8 @@ #[macro_use] extern crate cpython; -use cpython::{PyResult, Python}; +use cpython::{PyResult, Python, PyObject}; +use cpython::buffer::PyBuffer; // Our module is named 'hello', and can be imported using `import hello`. // This requires that the output binary file is named `hello.so` (or Windows: `hello.pyd`). @@ -11,6 +12,7 @@ use cpython::{PyResult, Python}; py_module_initializer!(hello, inithello, PyInit_hello, |py, m| { m.add(py, "__doc__", "Module documentation string")?; m.add(py, "func", py_fn!(py, func(x: Vec)))?; + m.add(py, "funcbuff", py_fn!(py, funcbuff(x: &PyObject)))?; Ok(()) }); @@ -22,4 +24,10 @@ py_module_initializer!(hello, inithello, PyInit_hello, |py, m| { // Most functions in the `cpython` crate require that you pass this argument. fn func(_: Python, x: Vec) -> PyResult { Ok(x.into_iter().inspect(|i| println!("{:}", i)).sum()) +} + +fn funcbuff(py: Python, x: &PyObject) -> PyResult { + let x = PyBuffer::get(py, x)?; + let x = x.as_slice::(py).expect("not a buffer of f64"); // fix to return err + Ok(x.into_iter().map(|i| i.get()).inspect(|i| println!("{:}", i)).sum()) } \ No newline at end of file diff --git a/test.py b/test.py index 63c12d2..a2ce452 100644 --- a/test.py +++ b/test.py @@ -11,4 +11,5 @@ x = np.arange(12.).astype('float64').reshape((-1, 2), order='c') print x -print hello.func(x[:, 1])# seams to only work with 1 d arrays +print hello.func(x[:, 1]) # seams to only work with 1 d arrays +print hello.funcbuff(x) # dose not work with slices