nautilus_model/python/identifiers/
instrument_id.rs1use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19 str::FromStr,
20};
21
22use nautilus_core::python::{IntoPyObjectNautilusExt, correctness_error_to_pyvalue_err};
23use pyo3::{
24 IntoPyObjectExt,
25 prelude::*,
26 pyclass::CompareOp,
27 types::{PyString, PyTuple},
28};
29
30use crate::{
31 enums::InstrumentClass,
32 identifiers::{InstrumentId, Symbol, Venue},
33 python::instrument_id_error_to_pyvalue_err,
34};
35
36#[pymethods]
37#[pyo3_stub_gen::derive::gen_stub_pymethods]
38impl InstrumentId {
39 #[new]
43 fn py_new(symbol: Symbol, venue: Venue) -> Self {
44 Self::new(symbol, venue)
45 }
46
47 fn __setstate__(&mut self, state: &Bound<'_, PyAny>) -> PyResult<()> {
48 let py_tuple: &Bound<'_, PyTuple> = state.cast::<PyTuple>()?;
49 self.symbol = Symbol::new_checked(
50 py_tuple
51 .get_item(0)?
52 .cast::<PyString>()?
53 .extract::<&str>()?,
54 )
55 .map_err(correctness_error_to_pyvalue_err)?;
56 self.venue = Venue::new_checked(
57 py_tuple
58 .get_item(1)?
59 .cast::<PyString>()?
60 .extract::<&str>()?,
61 )
62 .map_err(correctness_error_to_pyvalue_err)?;
63 Ok(())
64 }
65
66 fn __getstate__(&self, py: Python) -> PyResult<Py<PyAny>> {
67 (self.symbol.to_string(), self.venue.to_string()).into_py_any(py)
68 }
69
70 fn __reduce__(&self, py: Python) -> PyResult<Py<PyAny>> {
71 let safe_constructor = py.get_type::<Self>().getattr("_safe_constructor")?;
72 let state = self.__getstate__(py)?;
73 (safe_constructor, PyTuple::empty(py), state).into_py_any(py)
74 }
75
76 #[staticmethod]
77 fn _safe_constructor() -> Self {
78 Self::from_str("NULL.NULL").unwrap() }
80
81 #[expect(clippy::needless_pass_by_value)]
82 fn __richcmp__(&self, other: Py<PyAny>, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
83 if let Ok(other) = other.extract::<Self>(py) {
84 match op {
85 CompareOp::Eq => self.eq(&other).into_py_any_unwrap(py),
86 CompareOp::Ne => self.ne(&other).into_py_any_unwrap(py),
87 CompareOp::Ge => self.ge(&other).into_py_any_unwrap(py),
88 CompareOp::Gt => self.gt(&other).into_py_any_unwrap(py),
89 CompareOp::Le => self.le(&other).into_py_any_unwrap(py),
90 CompareOp::Lt => self.lt(&other).into_py_any_unwrap(py),
91 }
92 } else {
93 py.NotImplemented()
94 }
95 }
96
97 fn __hash__(&self) -> isize {
98 let mut h = DefaultHasher::new();
99 self.hash(&mut h);
100 h.finish() as isize
101 }
102
103 fn __repr__(&self) -> String {
104 format!("{}('{}')", stringify!(InstrumentId), self)
105 }
106
107 fn __str__(&self) -> String {
108 self.to_string()
109 }
110
111 #[getter]
112 #[pyo3(name = "symbol")]
113 fn py_symbol(&self) -> Symbol {
114 self.symbol
115 }
116
117 #[getter]
118 #[pyo3(name = "venue")]
119 fn py_venue(&self) -> Venue {
120 self.venue
121 }
122
123 #[getter]
124 fn value(&self) -> String {
125 self.to_string()
126 }
127
128 #[staticmethod]
129 #[pyo3(name = "from_str")]
130 fn py_from_str(value: &str) -> PyResult<Self> {
131 Self::from_str(value).map_err(instrument_id_error_to_pyvalue_err)
132 }
133
134 #[pyo3(name = "is_synthetic")]
135 fn py_is_synthetic(&self) -> bool {
136 self.is_synthetic()
137 }
138
139 #[pyo3(name = "parse_parent_components")]
149 fn py_parse_parent_components(&self) -> Option<(String, InstrumentClass)> {
150 self.parse_parent_components()
151 .map(|(root, class)| (root.to_string(), class))
152 }
153}