From 26db54860117ecf8b69c7b283e01a25305c4f3f4 Mon Sep 17 00:00:00 2001 From: Chris Sellers Date: Thu, 18 Apr 2024 21:37:26 +1000 Subject: [PATCH] Move Bar FromPyObject impl --- nautilus_core/model/src/data/bar.rs | 50 ---------------------- nautilus_core/model/src/python/data/bar.rs | 46 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 50 deletions(-) diff --git a/nautilus_core/model/src/data/bar.rs b/nautilus_core/model/src/data/bar.rs index 7a4dc2201d70..e5be5fb8397a 100644 --- a/nautilus_core/model/src/data/bar.rs +++ b/nautilus_core/model/src/data/bar.rs @@ -22,8 +22,6 @@ use std::{ use indexmap::IndexMap; use nautilus_core::{nanos::UnixNanos, serialization::Serializable}; -#[cfg(feature = "python")] -use pyo3::{PyAny, PyResult}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::{ @@ -281,54 +279,6 @@ impl Bar { metadata.insert("ts_init".to_string(), "UInt64".to_string()); metadata } - - /// Create a new [`Bar`] extracted from the given [`PyAny`]. - #[cfg(feature = "python")] - pub fn from_pyobject(obj: &PyAny) -> PyResult { - use nautilus_core::python::to_pyvalue_err; - - let bar_type_obj: &PyAny = obj.getattr("bar_type")?.extract()?; - let bar_type_str = bar_type_obj.call_method0("__str__")?.extract()?; - let bar_type = BarType::from_str(bar_type_str) - .map_err(to_pyvalue_err) - .unwrap(); - - let open_py: &PyAny = obj.getattr("open")?; - let price_prec: u8 = open_py.getattr("precision")?.extract()?; - let open_raw: i64 = open_py.getattr("raw")?.extract()?; - let open = Price::from_raw(open_raw, price_prec).map_err(to_pyvalue_err)?; - - let high_py: &PyAny = obj.getattr("high")?; - let high_raw: i64 = high_py.getattr("raw")?.extract()?; - let high = Price::from_raw(high_raw, price_prec).map_err(to_pyvalue_err)?; - - let low_py: &PyAny = obj.getattr("low")?; - let low_raw: i64 = low_py.getattr("raw")?.extract()?; - let low = Price::from_raw(low_raw, price_prec).map_err(to_pyvalue_err)?; - - let close_py: &PyAny = obj.getattr("close")?; - let close_raw: i64 = close_py.getattr("raw")?.extract()?; - let close = Price::from_raw(close_raw, price_prec).map_err(to_pyvalue_err)?; - - let volume_py: &PyAny = obj.getattr("volume")?; - let volume_raw: u64 = volume_py.getattr("raw")?.extract()?; - let volume_prec: u8 = volume_py.getattr("precision")?.extract()?; - let volume = Quantity::from_raw(volume_raw, volume_prec).map_err(to_pyvalue_err)?; - - let ts_event: u64 = obj.getattr("ts_event")?.extract()?; - let ts_init: u64 = obj.getattr("ts_init")?.extract()?; - - Ok(Self::new( - bar_type, - open, - high, - low, - close, - volume, - ts_event.into(), - ts_init.into(), - )) - } } impl Serializable for Bar {} diff --git a/nautilus_core/model/src/python/data/bar.rs b/nautilus_core/model/src/python/data/bar.rs index bb24e9506b89..5ad073b7f201 100644 --- a/nautilus_core/model/src/python/data/bar.rs +++ b/nautilus_core/model/src/python/data/bar.rs @@ -128,6 +128,52 @@ impl BarType { } } +impl Bar { + pub fn from_pyobject(obj: &PyAny) -> PyResult { + let bar_type_obj: &PyAny = obj.getattr("bar_type")?.extract()?; + let bar_type_str = bar_type_obj.call_method0("__str__")?.extract()?; + let bar_type = BarType::from_str(bar_type_str) + .map_err(to_pyvalue_err) + .unwrap(); + + let open_py: &PyAny = obj.getattr("open")?; + let price_prec: u8 = open_py.getattr("precision")?.extract()?; + let open_raw: i64 = open_py.getattr("raw")?.extract()?; + let open = Price::from_raw(open_raw, price_prec).map_err(to_pyvalue_err)?; + + let high_py: &PyAny = obj.getattr("high")?; + let high_raw: i64 = high_py.getattr("raw")?.extract()?; + let high = Price::from_raw(high_raw, price_prec).map_err(to_pyvalue_err)?; + + let low_py: &PyAny = obj.getattr("low")?; + let low_raw: i64 = low_py.getattr("raw")?.extract()?; + let low = Price::from_raw(low_raw, price_prec).map_err(to_pyvalue_err)?; + + let close_py: &PyAny = obj.getattr("close")?; + let close_raw: i64 = close_py.getattr("raw")?.extract()?; + let close = Price::from_raw(close_raw, price_prec).map_err(to_pyvalue_err)?; + + let volume_py: &PyAny = obj.getattr("volume")?; + let volume_raw: u64 = volume_py.getattr("raw")?.extract()?; + let volume_prec: u8 = volume_py.getattr("precision")?.extract()?; + let volume = Quantity::from_raw(volume_raw, volume_prec).map_err(to_pyvalue_err)?; + + let ts_event: u64 = obj.getattr("ts_event")?.extract()?; + let ts_init: u64 = obj.getattr("ts_init")?.extract()?; + + Ok(Self::new( + bar_type, + open, + high, + low, + close, + volume, + ts_event.into(), + ts_init.into(), + )) + } +} + #[pymethods] #[allow(clippy::too_many_arguments)] impl Bar {