nautilus_core/python/
mod.rs1#![expect(clippy::doc_markdown, reason = "Python docstrings")]
17
18#![allow(
21 deprecated,
22 reason = "pyo3-stub-gen currently relies on PyO3 initialization helpers marked as deprecated"
23)]
24#![expect(
25 clippy::missing_errors_doc,
26 reason = "errors documented on underlying Rust methods"
27)]
28pub mod casing;
35pub mod datetime;
36pub mod enums;
37pub mod params;
38pub mod parsing;
39pub mod serialization;
40pub mod string;
42pub mod uuid;
43pub mod version;
44
45use std::fmt::Display;
46
47use pyo3::{
48 Py,
49 conversion::IntoPyObjectExt,
50 exceptions::{
51 PyException, PyKeyError, PyNotImplementedError, PyRuntimeError, PyTypeError, PyValueError,
52 },
53 prelude::*,
54 types::PyString,
55 wrap_pyfunction,
56};
57use pyo3_stub_gen::derive::gen_stub_pyfunction;
58
59use crate::{
60 UUID4,
61 consts::{NAUTILUS_USER_AGENT, NAUTILUS_VERSION},
62 datetime::{
63 MILLISECONDS_IN_SECOND, NANOSECONDS_IN_MICROSECOND, NANOSECONDS_IN_MILLISECOND,
64 NANOSECONDS_IN_SECOND,
65 },
66};
67
68#[must_use]
83pub fn clone_py_object(obj: &Py<PyAny>) -> Py<PyAny> {
84 Python::attach(|py| obj.clone_ref(py))
85}
86
87pub fn call_python(py: Python, callback: &Py<PyAny>, py_obj: Py<PyAny>) {
89 if let Err(e) = callback.call1(py, (py_obj,)) {
90 log::error!("Error calling Python: {e}");
91 }
92}
93
94pub fn call_python_threadsafe(
100 py: Python,
101 call_soon: &Py<PyAny>,
102 callback: &Py<PyAny>,
103 py_obj: Py<PyAny>,
104) {
105 if let Err(e) = call_soon.call1(py, (callback, py_obj)) {
106 log::error!("Error scheduling Python callback on event loop: {e}");
107 }
108}
109
110pub trait IntoPyObjectNautilusExt<'py>: IntoPyObjectExt<'py> {
112 #[inline]
119 fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny> {
120 self.into_py_any(py)
121 .expect("Failed to convert type to Py<PyAny>")
122 }
123}
124
125impl<'py, T> IntoPyObjectNautilusExt<'py> for T where T: IntoPyObjectExt<'py> {}
126
127pub fn get_pytype_name<'py>(obj: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyString>> {
133 obj.get_type().name()
134}
135
136pub fn to_pyvalue_err(e: impl Display) -> PyErr {
138 PyValueError::new_err(e.to_string())
139}
140
141pub fn to_pytype_err(e: impl Display) -> PyErr {
143 PyTypeError::new_err(e.to_string())
144}
145
146pub fn to_pyruntime_err(e: impl Display) -> PyErr {
148 PyRuntimeError::new_err(e.to_string())
149}
150
151pub fn to_pykey_err(e: impl Display) -> PyErr {
153 PyKeyError::new_err(e.to_string())
154}
155
156pub fn to_pyexception(e: impl Display) -> PyErr {
158 PyException::new_err(e.to_string())
159}
160
161pub fn to_pynotimplemented_err(e: impl Display) -> PyErr {
163 PyNotImplementedError::new_err(e.to_string())
164}
165
166#[pyfunction(name = "is_pycapsule")]
177#[gen_stub_pyfunction(module = "nautilus_trader.core")]
178#[expect(
179 clippy::needless_pass_by_value,
180 reason = "Python FFI requires owned types"
181)]
182#[allow(unsafe_code)]
183fn py_is_pycapsule(obj: Py<PyAny>) -> bool {
184 unsafe {
186 pyo3::ffi::PyCapsule_CheckExact(obj.as_ptr()) != 0
188 }
189}
190
191#[pymodule]
197#[rustfmt::skip]
198pub fn core(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
199 m.add(stringify!(NAUTILUS_VERSION), NAUTILUS_VERSION)?;
200 m.add(stringify!(NAUTILUS_USER_AGENT), NAUTILUS_USER_AGENT)?;
201 m.add(stringify!(MILLISECONDS_IN_SECOND), MILLISECONDS_IN_SECOND)?;
202 m.add(stringify!(NANOSECONDS_IN_SECOND), NANOSECONDS_IN_SECOND)?;
203 m.add(stringify!(NANOSECONDS_IN_MILLISECOND), NANOSECONDS_IN_MILLISECOND)?;
204 m.add(stringify!(NANOSECONDS_IN_MICROSECOND), NANOSECONDS_IN_MICROSECOND)?;
205 m.add_class::<UUID4>()?;
206 m.add_function(wrap_pyfunction!(py_is_pycapsule, m)?)?;
207 m.add_function(wrap_pyfunction!(casing::py_convert_to_snake_case, m)?)?;
208 m.add_function(wrap_pyfunction!(string::py_mask_api_key, m)?)?;
209 m.add_function(wrap_pyfunction!(datetime::py_secs_to_nanos, m)?)?;
210 m.add_function(wrap_pyfunction!(datetime::py_secs_to_millis, m)?)?;
211 m.add_function(wrap_pyfunction!(datetime::py_millis_to_nanos, m)?)?;
212 m.add_function(wrap_pyfunction!(datetime::py_micros_to_nanos, m)?)?;
213 m.add_function(wrap_pyfunction!(datetime::py_nanos_to_secs, m)?)?;
214 m.add_function(wrap_pyfunction!(datetime::py_nanos_to_millis, m)?)?;
215 m.add_function(wrap_pyfunction!(datetime::py_nanos_to_micros, m)?)?;
216 m.add_function(wrap_pyfunction!(datetime::py_unix_nanos_to_iso8601, m)?)?;
217 m.add_function(wrap_pyfunction!(datetime::py_last_weekday_nanos, m)?)?;
218 m.add_function(wrap_pyfunction!(datetime::py_is_within_last_24_hours, m)?)?;
219 Ok(())
220}