Skip to main content

rustpy_ml/
lib.rs

1//! # Rustpy - Seamless Python Integration for Rust
2//!
3//! Rustpy provides a simple and intuitive interface for embedding Python code in Rust,
4//! with a focus on Machine Learning workflows. Built on top of PyO3, it offers a lower
5//! learning curve while maintaining type safety.
6//!
7//! ## Features
8//!
9//! - Easy Python code execution with the `python!` macro
10//! - Type-safe conversions between Rust and Python types
11//! - Support for ML libraries (NumPy, PyTorch, TensorFlow, etc.)
12//! - Comprehensive error handling
13//! - Global variable management
14//! - Seamless integration with Rust's type system
15//!
16//! ## Quick Start
17//!
18//! ```rust,no_run
19//! use rustpy_ml::prelude::*;
20//!
21//! fn main() -> rustpy_ml::Result<()> {
22//!     // Initialize the Python runtime
23//!     rustpy_ml::init()?;
24//!
25//!     // Execute Python code
26//!     let result: i32 = python!(-> i32, "1 + 1")?;
27//!     println!("Result: {}", result);
28//!
29//!     // Use ML libraries
30//!     py_exec!("
31//!         import numpy as np
32//!         arr = np.array([1, 2, 3, 4, 5])
33//!         print(f'Array: {arr}')
34//!         print(f'Mean: {arr.mean()}')
35//!     ")?;
36//!
37//!     Ok(())
38//! }
39//! ```
40
41mod error;
42mod runtime;
43pub mod macro_embed;
44mod convert;
45
46pub use error::{Error, Result};
47pub use runtime::{
48    init, init_with_ml, exec, eval, exec_with_locals, eval_with_locals,
49    import, call, set_global, get_global, clear_globals
50};
51pub use convert::{IntoPython, FromPython, to_python, from_python};
52
53// Re-export PyO3 types for convenience
54pub use pyo3::{
55    prelude::*,
56    types::{PyDict, PyList, PyTuple, PyString, PyBool, PyFloat, PyInt, PyModule},
57};
58
59/// Prelude module for convenient imports
60pub mod prelude {
61    pub use crate::{
62        Error, Result,
63        init, init_with_ml, exec, eval,
64        IntoPython, FromPython,
65        python, py_exec, py_eval, py_locals,
66    };
67    pub use pyo3::prelude::*;
68}