Skip to main content

Crate rustpy_ml

Crate rustpy_ml 

Source
Expand description

§Rustpy - Seamless Python Integration for Rust

Rustpy provides a simple and intuitive interface for embedding Python code in Rust, with a focus on Machine Learning workflows. Built on top of PyO3, it offers a lower learning curve while maintaining type safety.

§Features

  • Easy Python code execution with the python! macro
  • Type-safe conversions between Rust and Python types
  • Support for ML libraries (NumPy, PyTorch, TensorFlow, etc.)
  • Comprehensive error handling
  • Global variable management
  • Seamless integration with Rust’s type system

§Quick Start

use rustpy_ml::prelude::*;

fn main() -> rustpy_ml::Result<()> {
    // Initialize the Python runtime
    rustpy_ml::init()?;

    // Execute Python code
    let result: i32 = python!(-> i32, "1 + 1")?;
    println!("Result: {}", result);

    // Use ML libraries
    py_exec!("
        import numpy as np
        arr = np.array([1, 2, 3, 4, 5])
        print(f'Array: {arr}')
        print(f'Mean: {arr.mean()}')
    ")?;

    Ok(())
}

Modules§

macro_embed
prelude
Prelude module for convenient imports

Macros§

py_eval
Macro to evaluate Python expression with type inference Usage: let result: i32 = py_eval!(“1 + 1”);
py_exec
Macro to execute Python code without returning a value Usage: py_exec!(“import numpy as np\nprint(np.version)”)
py_locals
Helper macro to create a locals HashMap Usage: py_locals!{“x” => 5, “y” => 10}
python
Simple macro to run Python code Usage: python!(“print(‘Hello from Python’)”)
wrap_pyfunction
Wraps a Rust function annotated with #[pyfunction].
wrap_pyfunction_boundDeprecated
Wraps a Rust function annotated with #[pyfunction].

Structs§

Borrowed
A borrowed equivalent to Bound.
Bound
A GIL-attached equivalent to Py<T>.
Py
A GIL-independent reference to an object allocated on the Python heap.
PyAny
Represents any Python object.
PyBool
Represents a Python bool.
PyClassInitializer
Initializer for our #[pyclass] system.
PyDict
Represents a Python dict.
PyErr
Represents a Python exception.
PyFloat
Represents a Python float object.
PyInt
Represents a Python int object.
PyList
Represents a Python list.
PyModule
Represents a Python module object.
PyRef
A wrapper type for an immutably borrowed value from a Bound<'py, T>.
PyRefMut
A wrapper type for a mutably borrowed value from a Bound<'py, T>.
PyString
Represents a Python string (a Unicode string object).
PyTuple
Represents a Python tuple object.
Python
A marker token that represents holding the GIL.

Enums§

Error
Main error type for Rustpy

Traits§

FromPyObject
Extract a type from a Python object.
FromPython
Trait for converting Python objects to Rust types
IntoPyDeprecated
Defines a conversion from a Rust type to a Python object.
IntoPyObject
Defines a conversion from a Rust type to a Python object, which may fail.
IntoPython
Trait for converting Rust types to Python objects
PyAnyMethods
This trait represents the Python APIs which are usable on all Python objects.
PyBoolMethods
Implementation of functionality for PyBool.
PyByteArrayMethods
Implementation of functionality for PyByteArray.
PyBytesMethods
Implementation of functionality for PyBytes.
PyCapsuleMethods
Implementation of functionality for PyCapsule.
PyComplexMethods
Implementation of functionality for PyComplex.
PyDictMethods
Implementation of functionality for PyDict.
PyFloatMethods
Implementation of functionality for PyFloat.
PyFrozenSetMethods
Implementation of functionality for PyFrozenSet.
PyListMethods
Implementation of functionality for PyList.
PyMappingMethods
Implementation of functionality for PyMapping.
PyMappingProxyMethods
Implementation of functionality for PyMappingProxy.
PyModuleMethods
Implementation of functionality for PyModule.
PySequenceMethods
Implementation of functionality for PySequence.
PySetMethods
Implementation of functionality for PySet.
PySliceMethods
Implementation of functionality for PySlice.
PyStringMethods
Implementation of functionality for PyString.
PyTracebackMethods
Implementation of functionality for PyTraceback.
PyTupleMethods
Implementation of functionality for PyTuple.
PyTypeMethods
Implementation of functionality for PyType.
PyWeakrefMethods
Implementation of functionality for PyWeakref.
ToPyObjectDeprecated
Conversion trait that allows various objects to be converted into PyObject.

Functions§

call
Call a Python function by module and function name
clear_globals
Clear all global variables
eval
Evaluate Python expression and return the result
eval_with_locals
Evaluate Python expression with local variables
exec
Execute Python code and return the result
exec_with_locals
Execute Python code with local variables
from_python
Helper function to convert Python object to Rust value
get_global
Get a global Python variable
import
Import a Python module
init
Initialize the Python runtime with optional ML library support This should be called before using any Python functionality
init_with_ml
Initialize with specific ML libraries
set_global
Store a global Python variable
to_python
Helper function to convert Rust value to Python

Type Aliases§

PyObject
A commonly-used alias for Py<PyAny>.
PyResult
Represents the result of a Python call.
Result
Result type alias for Rustpy operations

Attribute Macros§

pyclass
pyfunction
A proc macro used to expose Rust functions to Python.
pymethods
A proc macro used to expose methods to Python.
pymodule
A proc macro used to implement Python modules.

Derive Macros§

FromPyObject
IntoPyObject
IntoPyObjectRef