Skip to content

Commit

Permalink
#update
Browse files Browse the repository at this point in the history
  • Loading branch information
yutiansut committed Dec 2, 2019
1 parent 3571fff commit 426a2ac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 72 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ serde_derive = "1.0"
serde = { version = "1.0", features = ["derive"] } # 序列化
rayon = "1.1" # 多线程
ndarray = "0.13.0" # ndarray
arrow = { git = "https://gitee.com/yutiansut/arrow"}
pyo3 = "0.8"
numpy = "0.7.0"

actix-web = "2.0.0-alpha.1"

actix = "0.8.2"
Expand Down
84 changes: 24 additions & 60 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ extern crate chrono;
use chrono::prelude::*;
use ndarray::array;
use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
use numpy::{IntoPyArray, PyArrayDyn,PyArray1, get_array_module};
use pyo3::prelude::{pymodule,ObjectProtocol, Py, PyModule, PyResult, Python};
use pyo3::types::PyDict;
// use numpy::{IntoPyArray, PyArrayDyn,PyArray1, get_array_module};
// use pyo3::prelude::{pymodule,ObjectProtocol, Py, PyModule, PyResult, Python};
// use pyo3::types::PyDict;


fn main() {
Expand All @@ -60,7 +60,7 @@ fn main() {
test_ndarray();
test_datetime();
test_timeseries();
test_pyo3();
// test_pyo3();
//rust_ext();
}

Expand Down Expand Up @@ -135,65 +135,29 @@ fn test_datetime() {
}


fn test_pyo3() -> Result<(), ()> {
let gil = Python::acquire_gil();
test_pyo3_(gil.python()).map_err(|e| {
eprintln!("error! :{:?}", e);
// we can't display python error type via ::std::fmt::Display
// so print error here manually
e.print_and_set_sys_last_vars(gil.python());
})
}

fn test_pyo3_<'py>(py: Python<'py>) -> PyResult<()> {
let np = py.import("numpy")?;
let dict = PyDict::new(py);
dict.set_item("np", np)?;
let pyarray: &PyArray1<i32> = py
.eval("np.absolute(np.array([-1, -2, -3], dtype='int32'))", Some(&dict), None)?
.extract()?;
let slice = pyarray.as_slice()?;
assert_eq!(slice, &[1, 2, 3]);
Ok(())
}



#[pymodule]
fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
// immutable example
fn axpy(a: f64, x: ArrayViewD<f64>, y: ArrayViewD<f64>) -> ArrayD<f64> {
a * &x + &y
}

// mutable example (no return)
fn mult(a: f64, mut x: ArrayViewMutD<f64>) {
x *= a;
}
// fn test_pyo3() -> Result<(), ()> {
// let gil = Python::acquire_gil();
// test_pyo3_(gil.python()).map_err(|e| {
// eprintln!("error! :{:?}", e);
// // we can't display python error type via ::std::fmt::Display
// // so print error here manually
// e.print_and_set_sys_last_vars(gil.python());
// })
// }

// wrapper of `axpy`
#[pyfn(m, "axpy")]
fn axpy_py(
py: Python,
a: f64,
x: &PyArrayDyn<f64>,
y: &PyArrayDyn<f64>,
) -> Py<PyArrayDyn<f64>> {
let x = x.as_array();
let y = y.as_array();
axpy(a, x, y).into_pyarray(py).to_owned()
}
// fn test_pyo3_<'py>(py: Python<'py>) -> PyResult<()> {
// let np = py.import("numpy")?;
// let dict = PyDict::new(py);
// dict.set_item("np", np)?;
// let pyarray: &PyArray1<i32> = py
// .eval("np.absolute(np.array([-1, -2, -3], dtype='int32'))", Some(&dict), None)?
// .extract()?;
// let slice = pyarray.as_slice()?;
// assert_eq!(slice, &[1, 2, 3]);
// Ok(())
// }

// wrapper of `mult`
#[pyfn(m, "mult")]
fn mult_py(_py: Python, a: f64, x: &PyArrayDyn<f64>) -> PyResult<()> {
let x = x.as_array_mut();
mult(a, x);
Ok(())
}

Ok(())
}


fn test_timeseries() {
Expand Down
11 changes: 2 additions & 9 deletions src/qawebsockets/websocketclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,12 @@ where

impl<T: 'static> StreamHandler<Frame, WsProtocolError> for ChatClient<T>
where
T: AsyncRead + AsyncWrite,


T: AsyncRead + AsyncWrite,
{





fn handle(&mut self, msg: Frame, _ctx: &mut Context<Self>) {

if let Frame::Text(txt) = msg {
let mut res = txt.unwrap();
let res = txt.unwrap();
let xu = std::str::from_utf8(&res).unwrap();
println!("{:?}",xu);

Expand Down

0 comments on commit 426a2ac

Please sign in to comment.