nautilus_core/python/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Python bindings and interoperability helpers built on top of
17//! [`PyO3`](https://pyo3.rs).
18//!
19//! This sub-module groups together the Rust code that is *only* required when compiling the
20//! `python` feature flag. It provides thin adapters so that NautilusTrader functionality can be
21//! consumed from the `nautilus_trader` Python package without sacrificing type-safety or
22//! performance.
23
24pub mod casing;
25pub mod datetime;
26pub mod enums;
27pub mod parsing;
28pub mod serialization;
29pub mod uuid;
30pub mod version;
31
32use pyo3::{
33    conversion::IntoPyObjectExt,
34    exceptions::{PyRuntimeError, PyTypeError, PyValueError},
35    prelude::*,
36    types::PyString,
37    wrap_pyfunction,
38};
39
40use crate::{
41    UUID4,
42    consts::{NAUTILUS_USER_AGENT, NAUTILUS_VERSION},
43    datetime::{
44        MILLISECONDS_IN_SECOND, NANOSECONDS_IN_MICROSECOND, NANOSECONDS_IN_MILLISECOND,
45        NANOSECONDS_IN_SECOND,
46    },
47};
48
49/// Extend `IntoPyObjectExt` helper trait to unwrap `PyObject` after conversion.
50pub trait IntoPyObjectNautilusExt<'py>: IntoPyObjectExt<'py> {
51    /// Convert `self` into a [`PyObject`] while *panicking* if the conversion fails.
52    ///
53    /// This is a convenience wrapper around [`IntoPyObjectExt::into_py_any`] that avoids the
54    /// cumbersome `Result` handling when we are certain that the conversion cannot fail (for
55    /// instance when we are converting primitives or other types that already implement the
56    /// necessary PyO3 traits).
57    #[inline]
58    fn into_py_any_unwrap(self, py: Python<'py>) -> PyObject {
59        self.into_py_any(py)
60            .expect("Failed to convert type to PyObject")
61    }
62}
63
64impl<'py, T> IntoPyObjectNautilusExt<'py> for T where T: IntoPyObjectExt<'py> {}
65
66/// Gets the type name for the given Python `obj`.
67///
68/// # Errors
69///
70/// Returns a error if accessing the type name fails.
71pub fn get_pytype_name<'py>(obj: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyString>> {
72    obj.get_type().name()
73}
74
75/// Converts any type that implements `Display` to a Python `ValueError`.
76///
77/// # Errors
78///
79/// Returns a Python error with the error string.
80pub fn to_pyvalue_err(e: impl std::fmt::Display) -> PyErr {
81    PyValueError::new_err(e.to_string())
82}
83
84/// Converts any type that implements `Display` to a Python `TypeError`.
85///
86/// # Errors
87///
88/// Returns a Python error with the error string.
89pub fn to_pytype_err(e: impl std::fmt::Display) -> PyErr {
90    PyTypeError::new_err(e.to_string())
91}
92
93/// Converts any type that implements `Display` to a Python `RuntimeError`.
94///
95/// # Errors
96///
97/// Returns a Python error with the error string.
98pub fn to_pyruntime_err(e: impl std::fmt::Display) -> PyErr {
99    PyRuntimeError::new_err(e.to_string())
100}
101
102#[pyfunction]
103#[allow(clippy::needless_pass_by_value)]
104#[allow(unsafe_code)]
105fn is_pycapsule(obj: PyObject) -> bool {
106    unsafe {
107        // PyCapsule_CheckExact checks if the object is exactly a PyCapsule
108        pyo3::ffi::PyCapsule_CheckExact(obj.as_ptr()) != 0
109    }
110}
111
112/// Loaded as `nautilus_pyo3.core`.
113///
114/// # Errors
115///
116/// Returns a `PyErr` if registering any module components fails.
117#[pymodule]
118#[rustfmt::skip]
119pub fn core(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
120    m.add(stringify!(NAUTILUS_VERSION), NAUTILUS_VERSION)?;
121    m.add(stringify!(NAUTILUS_USER_AGENT), NAUTILUS_USER_AGENT)?;
122    m.add(stringify!(MILLISECONDS_IN_SECOND), MILLISECONDS_IN_SECOND)?;
123    m.add(stringify!(NANOSECONDS_IN_SECOND), NANOSECONDS_IN_SECOND)?;
124    m.add(stringify!(NANOSECONDS_IN_MILLISECOND), NANOSECONDS_IN_MILLISECOND)?;
125    m.add(stringify!(NANOSECONDS_IN_MICROSECOND), NANOSECONDS_IN_MICROSECOND)?;
126    m.add_class::<UUID4>()?;
127    m.add_function(wrap_pyfunction!(is_pycapsule, m)?)?;
128    m.add_function(wrap_pyfunction!(casing::py_convert_to_snake_case, m)?)?;
129    m.add_function(wrap_pyfunction!(datetime::py_secs_to_nanos, m)?)?;
130    m.add_function(wrap_pyfunction!(datetime::py_secs_to_millis, m)?)?;
131    m.add_function(wrap_pyfunction!(datetime::py_millis_to_nanos, m)?)?;
132    m.add_function(wrap_pyfunction!(datetime::py_micros_to_nanos, m)?)?;
133    m.add_function(wrap_pyfunction!(datetime::py_nanos_to_secs, m)?)?;
134    m.add_function(wrap_pyfunction!(datetime::py_nanos_to_millis, m)?)?;
135    m.add_function(wrap_pyfunction!(datetime::py_nanos_to_micros, m)?)?;
136    m.add_function(wrap_pyfunction!(datetime::py_unix_nanos_to_iso8601, m)?)?;
137    m.add_function(wrap_pyfunction!(datetime::py_last_weekday_nanos, m)?)?;
138    m.add_function(wrap_pyfunction!(datetime::py_is_within_last_24_hours, m)?)?;
139    Ok(())
140}