Skip to content

Commit 26db548

Browse files
committed
Move Bar FromPyObject impl
1 parent 5a311db commit 26db548

File tree

2 files changed

+46
-50
lines changed

2 files changed

+46
-50
lines changed

nautilus_core/model/src/data/bar.rs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ use std::{
2222

2323
use indexmap::IndexMap;
2424
use nautilus_core::{nanos::UnixNanos, serialization::Serializable};
25-
#[cfg(feature = "python")]
26-
use pyo3::{PyAny, PyResult};
2725
use serde::{Deserialize, Deserializer, Serialize, Serializer};
2826

2927
use crate::{
@@ -281,54 +279,6 @@ impl Bar {
281279
metadata.insert("ts_init".to_string(), "UInt64".to_string());
282280
metadata
283281
}
284-
285-
/// Create a new [`Bar`] extracted from the given [`PyAny`].
286-
#[cfg(feature = "python")]
287-
pub fn from_pyobject(obj: &PyAny) -> PyResult<Self> {
288-
use nautilus_core::python::to_pyvalue_err;
289-
290-
let bar_type_obj: &PyAny = obj.getattr("bar_type")?.extract()?;
291-
let bar_type_str = bar_type_obj.call_method0("__str__")?.extract()?;
292-
let bar_type = BarType::from_str(bar_type_str)
293-
.map_err(to_pyvalue_err)
294-
.unwrap();
295-
296-
let open_py: &PyAny = obj.getattr("open")?;
297-
let price_prec: u8 = open_py.getattr("precision")?.extract()?;
298-
let open_raw: i64 = open_py.getattr("raw")?.extract()?;
299-
let open = Price::from_raw(open_raw, price_prec).map_err(to_pyvalue_err)?;
300-
301-
let high_py: &PyAny = obj.getattr("high")?;
302-
let high_raw: i64 = high_py.getattr("raw")?.extract()?;
303-
let high = Price::from_raw(high_raw, price_prec).map_err(to_pyvalue_err)?;
304-
305-
let low_py: &PyAny = obj.getattr("low")?;
306-
let low_raw: i64 = low_py.getattr("raw")?.extract()?;
307-
let low = Price::from_raw(low_raw, price_prec).map_err(to_pyvalue_err)?;
308-
309-
let close_py: &PyAny = obj.getattr("close")?;
310-
let close_raw: i64 = close_py.getattr("raw")?.extract()?;
311-
let close = Price::from_raw(close_raw, price_prec).map_err(to_pyvalue_err)?;
312-
313-
let volume_py: &PyAny = obj.getattr("volume")?;
314-
let volume_raw: u64 = volume_py.getattr("raw")?.extract()?;
315-
let volume_prec: u8 = volume_py.getattr("precision")?.extract()?;
316-
let volume = Quantity::from_raw(volume_raw, volume_prec).map_err(to_pyvalue_err)?;
317-
318-
let ts_event: u64 = obj.getattr("ts_event")?.extract()?;
319-
let ts_init: u64 = obj.getattr("ts_init")?.extract()?;
320-
321-
Ok(Self::new(
322-
bar_type,
323-
open,
324-
high,
325-
low,
326-
close,
327-
volume,
328-
ts_event.into(),
329-
ts_init.into(),
330-
))
331-
}
332282
}
333283

334284
impl Serializable for Bar {}

nautilus_core/model/src/python/data/bar.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,52 @@ impl BarType {
128128
}
129129
}
130130

131+
impl Bar {
132+
pub fn from_pyobject(obj: &PyAny) -> PyResult<Self> {
133+
let bar_type_obj: &PyAny = obj.getattr("bar_type")?.extract()?;
134+
let bar_type_str = bar_type_obj.call_method0("__str__")?.extract()?;
135+
let bar_type = BarType::from_str(bar_type_str)
136+
.map_err(to_pyvalue_err)
137+
.unwrap();
138+
139+
let open_py: &PyAny = obj.getattr("open")?;
140+
let price_prec: u8 = open_py.getattr("precision")?.extract()?;
141+
let open_raw: i64 = open_py.getattr("raw")?.extract()?;
142+
let open = Price::from_raw(open_raw, price_prec).map_err(to_pyvalue_err)?;
143+
144+
let high_py: &PyAny = obj.getattr("high")?;
145+
let high_raw: i64 = high_py.getattr("raw")?.extract()?;
146+
let high = Price::from_raw(high_raw, price_prec).map_err(to_pyvalue_err)?;
147+
148+
let low_py: &PyAny = obj.getattr("low")?;
149+
let low_raw: i64 = low_py.getattr("raw")?.extract()?;
150+
let low = Price::from_raw(low_raw, price_prec).map_err(to_pyvalue_err)?;
151+
152+
let close_py: &PyAny = obj.getattr("close")?;
153+
let close_raw: i64 = close_py.getattr("raw")?.extract()?;
154+
let close = Price::from_raw(close_raw, price_prec).map_err(to_pyvalue_err)?;
155+
156+
let volume_py: &PyAny = obj.getattr("volume")?;
157+
let volume_raw: u64 = volume_py.getattr("raw")?.extract()?;
158+
let volume_prec: u8 = volume_py.getattr("precision")?.extract()?;
159+
let volume = Quantity::from_raw(volume_raw, volume_prec).map_err(to_pyvalue_err)?;
160+
161+
let ts_event: u64 = obj.getattr("ts_event")?.extract()?;
162+
let ts_init: u64 = obj.getattr("ts_init")?.extract()?;
163+
164+
Ok(Self::new(
165+
bar_type,
166+
open,
167+
high,
168+
low,
169+
close,
170+
volume,
171+
ts_event.into(),
172+
ts_init.into(),
173+
))
174+
}
175+
}
176+
131177
#[pymethods]
132178
#[allow(clippy::too_many_arguments)]
133179
impl Bar {

0 commit comments

Comments
 (0)