nautilus_binance/python/
arrow.rs1use std::io::Cursor;
17
18use arrow::ipc::reader::StreamReader;
19use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err};
20use nautilus_serialization::{
21 arrow::ArrowSchemaProvider, python::arrow::arrow_record_batch_to_pybytes,
22};
23use pyo3::{
24 conversion::IntoPyObjectExt,
25 prelude::*,
26 types::{PyBytes, PyType},
27};
28
29use crate::{
30 arrow::bar::{binance_bar_to_arrow_record_batch, decode_binance_bar_batch},
31 common::bar::BinanceBar,
32};
33
34#[pyfunction]
40#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.binance")]
41pub fn get_binance_arrow_schema_map(
42 py: Python<'_>,
43 cls: &Bound<'_, PyType>,
44) -> PyResult<Py<PyAny>> {
45 let cls_str: String = cls.getattr("__name__")?.extract()?;
46 let result_map = match cls_str.as_str() {
47 stringify!(BinanceBar) => BinanceBar::get_schema_map(),
48 _ => {
49 return Err(to_pyvalue_err(format!(
50 "Arrow schema for `{cls_str}` is not currently implemented"
51 )));
52 }
53 };
54
55 result_map.into_py_any(py)
56}
57
58#[pyfunction(name = "binance_bar_to_arrow_record_batch_bytes")]
64#[allow(clippy::needless_pass_by_value)]
65pub fn py_binance_bar_to_arrow_record_batch_bytes(
66 py: Python,
67 data: Vec<BinanceBar>,
68) -> PyResult<Py<PyBytes>> {
69 match binance_bar_to_arrow_record_batch(&data) {
70 Ok(batch) => arrow_record_batch_to_pybytes(py, &batch),
71 Err(e) => Err(to_pyvalue_err(e)),
72 }
73}
74
75#[pyfunction(name = "binance_bar_from_arrow_record_batch_bytes")]
81pub fn py_binance_bar_from_arrow_record_batch_bytes(
82 _py: Python,
83 data: Vec<u8>,
84) -> PyResult<Vec<BinanceBar>> {
85 let cursor = Cursor::new(data);
86 let reader = StreamReader::try_new(cursor, None).map_err(to_pyruntime_err)?;
87
88 let mut results = Vec::new();
89 for batch_result in reader {
90 let batch = batch_result.map_err(to_pyruntime_err)?;
91 let metadata = batch.schema().metadata().clone();
92 let decoded = decode_binance_bar_batch(&metadata, &batch).map_err(to_pyvalue_err)?;
93 results.extend(decoded);
94 }
95
96 Ok(results)
97}