nautilus_model/python/data/
deltas.rs1use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19 ops::Deref,
20};
21
22use nautilus_core::python::to_pyvalue_err;
23use pyo3::{prelude::*, pyclass::CompareOp, types::PyCapsule};
24
25use super::data_to_pycapsule;
26use crate::{
27 data::{Data, OrderBookDelta, OrderBookDeltas, OrderBookDeltas_API},
28 identifiers::InstrumentId,
29 python::common::PY_MODULE_MODEL,
30};
31
32#[pymethods]
33impl OrderBookDeltas {
34 #[new]
35 fn py_new(instrument_id: InstrumentId, deltas: Vec<OrderBookDelta>) -> PyResult<Self> {
36 Self::new_checked(instrument_id, deltas).map_err(to_pyvalue_err)
37 }
38
39 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
40 match op {
41 CompareOp::Eq => self.eq(other).into_py(py),
42 CompareOp::Ne => self.ne(other).into_py(py),
43 _ => py.NotImplemented(),
44 }
45 }
46
47 fn __hash__(&self) -> isize {
48 let mut h = DefaultHasher::new();
49 self.hash(&mut h);
50 h.finish() as isize
51 }
52
53 fn __repr__(&self) -> String {
54 format!("{self:?}")
55 }
56
57 fn __str__(&self) -> String {
58 self.to_string()
59 }
60
61 #[getter]
62 #[pyo3(name = "instrument_id")]
63 fn py_instrument_id(&self) -> InstrumentId {
64 self.instrument_id
65 }
66
67 #[getter]
68 #[pyo3(name = "deltas")]
69 fn py_deltas(&self) -> Vec<OrderBookDelta> {
70 self.deltas.clone()
72 }
73
74 #[getter]
75 #[pyo3(name = "flags")]
76 fn py_flags(&self) -> u8 {
77 self.flags
78 }
79
80 #[getter]
81 #[pyo3(name = "sequence")]
82 fn py_sequence(&self) -> u64 {
83 self.sequence
84 }
85
86 #[getter]
87 #[pyo3(name = "ts_event")]
88 fn py_ts_event(&self) -> u64 {
89 self.ts_event.as_u64()
90 }
91
92 #[getter]
93 #[pyo3(name = "ts_init")]
94 fn py_ts_init(&self) -> u64 {
95 self.ts_init.as_u64()
96 }
97
98 #[staticmethod]
99 #[pyo3(name = "fully_qualified_name")]
100 fn py_fully_qualified_name() -> String {
101 format!("{}:{}", PY_MODULE_MODEL, stringify!(OrderBookDeltas))
102 }
103
104 #[staticmethod]
105 #[pyo3(name = "from_pycapsule")]
106 pub fn py_from_pycapsule(capsule: Bound<'_, PyAny>) -> Self {
107 let capsule: &Bound<'_, PyCapsule> = capsule
108 .downcast::<PyCapsule>()
109 .expect("Error on downcast to `&PyCapsule`");
110 let data: &OrderBookDeltas_API =
111 unsafe { &*(capsule.pointer() as *const OrderBookDeltas_API) };
112 data.deref().clone()
113 }
114
115 #[pyo3(name = "as_pycapsule")]
131 fn py_as_pycapsule(&self, py: Python<'_>) -> PyObject {
132 let deltas = OrderBookDeltas_API::new(self.clone());
133 data_to_pycapsule(py, Data::Deltas(deltas))
134 }
135
136 }