Skip to content

Commit 56e0709

Browse files
committed
Add filtering of samples by column names
1 parent a3b8453 commit 56e0709

2 files changed

Lines changed: 23 additions & 20 deletions

File tree

rust/src/lib.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use ::twinleaf::*;
88
struct PyIter {
99
port: data::Device,
1010
n: Option<usize>,
11+
columns: Vec<String>,
1112
}
1213

1314
#[pymethods]
@@ -28,19 +29,23 @@ impl PyIter {
2829
}
2930
}
3031

31-
let sample = slf.port.next();
32+
while dict.is_empty() {
33+
let sample = slf.port.next();
3234

33-
let time = sample.timestamp_end().into_pyobject(slf.py())?;
34-
dict.set_item("time", time)?;
35-
36-
for column in sample.columns {
37-
let name = column.desc.name.clone().into_pyobject(slf.py())?;
38-
match column.value {
39-
data::ColumnData::Int(x) => {dict.set_item(name, x.into_pyobject(slf.py())?)?}
40-
data::ColumnData::UInt(x) => {dict.set_item(name, x.into_pyobject(slf.py())?)?}
41-
data::ColumnData::Float(x) => {dict.set_item(name, x.into_pyobject(slf.py())?)?}
42-
_ => { dict.set_item(name, "UNKNOWN".into_pyobject(slf.py())?)? }
43-
};
35+
for column in &sample.columns {
36+
let name = column.desc.name.clone().into_pyobject(slf.py())?;
37+
let name_str: String = name.extract()?;
38+
if slf.columns.is_empty() || slf.columns.contains(&name_str) {
39+
let time = sample.timestamp_end().into_pyobject(slf.py())?;
40+
dict.set_item("time", time)?;
41+
match column.value {
42+
data::ColumnData::Int(x) => {dict.set_item(name, x.into_pyobject(slf.py())?)?}
43+
data::ColumnData::UInt(x) => {dict.set_item(name, x.into_pyobject(slf.py())?)?}
44+
data::ColumnData::Float(x) => {dict.set_item(name, x.into_pyobject(slf.py())?)?}
45+
_ => { dict.set_item(name, "UNKNOWN".into_pyobject(slf.py())?)? }
46+
};
47+
}
48+
}
4449
}
4550

4651
Ok(Some(dict.into()))
@@ -81,10 +86,10 @@ impl PyDevice {
8186
}
8287
}
8388

84-
fn samples<'py>(&self, _py: Python<'py>, n: Option<usize>) -> PyResult<PyIter> {
85-
Ok(PyIter{port: data::Device::new(self.proxy.device_full(self.route.clone()).unwrap()), n: n})
89+
#[pyo3(signature = (n=1, columns=None))]
90+
fn samples<'py>(&self, _py: Python<'py>, n: Option<usize>, columns: Option<Vec<String>>) -> PyResult<PyIter> {
91+
Ok(PyIter{port: data::Device::new(self.proxy.device_full(self.route.clone()).unwrap()), n: n, columns: columns.unwrap_or_default()})
8692
}
87-
8893
}
8994

9095
/// A Python module implemented in Rust. The name of this function must match
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#!/usr/bin/env python3
22

33
import twinleaf
4-
import IPython
54

65
dev = twinleaf.Device()
76

8-
#dev.scan_rpcs()
9-
#IPython.embed()
10-
11-
for sample in dev.samples(5):
7+
# columns = ["accel.x", "accel.y", "accel.z"]
8+
columns = []
9+
for sample in dev.samples(5, columns):
1210
print(sample)

0 commit comments

Comments
 (0)