Skip to content

Commit 5a34793

Browse files
authored
More example in README (#52)
* More example in README * Refactor examples - Use manual error printing in readme example - Remove unnecessary feature gates * Enable readme testing in CI * Fix readme test in appveyor
1 parent 6b6c70c commit 5a34793

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed

README.md

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,67 @@ If you want to use rust-cpython, use version 0.2.1 from crates.io.
2929

3030
Example
3131
---------
32-
Please see [example](example) directory for a complete example
3332

33+
34+
## Exec python program and get data
35+
36+
``` toml
37+
[package]
38+
name = "numpy-test"
39+
40+
[dependencies]
41+
pyo3 = "^0.4.1"
42+
numpy = "0.3"
3443
```
44+
45+
``` rust
46+
extern crate numpy;
47+
extern crate pyo3;
48+
use pyo3::prelude::*;
49+
use numpy::*;
50+
51+
fn main() -> Result<(), ()> {
52+
let gil = Python::acquire_gil();
53+
main_(gil.python()).map_err(|e| {
54+
eprintln!("error! :{:?}", e);
55+
// we can't display python error type via ::std::fmt::Display
56+
// so print error here manually
57+
e.print_and_set_sys_last_vars(gil.python());
58+
})
59+
}
60+
61+
fn main_<'py>(py: Python<'py>) -> PyResult<()> {
62+
let np = PyArrayModule::import(py)?;
63+
let dict = PyDict::new(py);
64+
dict.set_item("np", np.as_pymodule())?;
65+
let pyarray: &PyArray<i32> = py
66+
.eval("np.array([1, 2, 3], dtype='int32')", Some(&dict), None)?
67+
.extract()?;
68+
let slice = pyarray.as_slice().into_pyresult("Array Cast failed")?;
69+
assert_eq!(slice, &[1, 2, 3]);
70+
Ok(())
71+
}
72+
```
73+
74+
## Write Python module by rust
75+
76+
Please see [example](example) directory for a complete example
77+
78+
```toml
3579
[lib]
3680
name = "rust_ext"
3781
crate-type = ["cdylib"]
3882

3983
[dependencies]
4084
numpy = "0.3"
41-
pyo3 = "^0.3.1"
4285
ndarray = "0.11"
86+
87+
[dependencies.pyo3]
88+
version = "^0.4.1"
89+
features = ["extension-module"]
4390
```
4491

4592
```rust
46-
#![feature(use_extern_macros, specialization)]
47-
4893
extern crate ndarray;
4994
extern crate numpy;
5095
extern crate pyo3;
@@ -72,7 +117,7 @@ fn rust_ext(py: Python, m: &PyModule) -> PyResult<()> {
72117

73118
// wrapper of `axpy`
74119
#[pyfn(m, "axpy")]
75-
fn axpy_py(py: Python, a: f64, x: &PyArray, y: &PyArray) -> PyResult<PyArray> {
120+
fn axpy_py(py: Python, a: f64, x: &PyArray<f64>, y: &PyArray<f64>) -> PyResult<PyArray<f64>> {
76121
let np = PyArrayModule::import(py)?;
77122
let x = x.as_array().into_pyresult("x must be f64 array")?;
78123
let y = y.as_array().into_pyresult("y must be f64 array")?;
@@ -81,7 +126,7 @@ fn rust_ext(py: Python, m: &PyModule) -> PyResult<()> {
81126

82127
// wrapper of `mult`
83128
#[pyfn(m, "mult")]
84-
fn mult_py(_py: Python, a: f64, x: &PyArray) -> PyResult<()> {
129+
fn mult_py(_py: Python, a: f64, x: &PyArray<f64>) -> PyResult<()> {
85130
let x = x.as_array_mut().into_pyresult("x must be f64 array")?;
86131
mult(a, x);
87132
Ok(())

appveyor.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ build_script:
2020
- cargo build --verbose
2121

2222
test_script:
23-
- cargo test --verbose
23+
- cargo build --verbose --all
24+
- cargo test --verbose --all
25+
- rustdoc --test README.md -L native="%PYTHON%\\libs" -L target/debug/deps/
2426
- pip install setuptools-rust
2527
- cd example
2628
- python setup.py install

ci/travis/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set -ex
44

55
cargo build --verbose --all
66
cargo test --verbose --all
7+
rustdoc -L target/debug/deps/ --test README.md
78

89
cd example
910
python setup.py install

example/extensions/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(use_extern_macros, specialization)]
2-
31
extern crate ndarray;
42
extern crate numpy;
53
extern crate pyo3;

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//! #[macro_use]
1515
//! extern crate ndarray;
1616
//! extern crate numpy;
17-
//! #[macro_use]
1817
//! extern crate pyo3;
1918
//! use pyo3::prelude::*;
2019
//! use numpy::*;

0 commit comments

Comments
 (0)