Skip to content

Commit

Permalink
add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Jan 23, 2017
1 parent 4dbd526 commit d54f71d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -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`).
Expand All @@ -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<f64>)))?;
m.add(py, "funcbuff", py_fn!(py, funcbuff(x: &PyObject)))?;
Ok(())
});

Expand All @@ -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<f64>) -> PyResult<f64> {
Ok(x.into_iter().inspect(|i| println!("{:}", i)).sum())
}

fn funcbuff(py: Python, x: &PyObject) -> PyResult<f64> {
let x = PyBuffer::get(py, x)?;
let x = x.as_slice::<f64>(py).expect("not a buffer of f64"); // fix to return err
Ok(x.into_iter().map(|i| i.get()).inspect(|i| println!("{:}", i)).sum())
}
3 changes: 2 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d54f71d

Please sign in to comment.