MCPcopy Index your code
hub / github.com/davidhewitt/pythonize

github.com/davidhewitt/pythonize @v0.29.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.29.0 ↗ · + Follow
259 symbols 471 edges 9 files 14 documented · 5%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Pythonize

This is a serializer for Rust's serde ecosystem, which can convert Rust objects to Python values and back.

At the moment the Python structures it produces should be very similar to those which are produced by serde_json; i.e. calling Python's json.loads() on a value encoded by serde_json should produce an identical structure to that which is produced directly by pythonize.

Usage

This crate converts Rust types which implement the Serde serialization traits into Python objects using the PyO3 library.

Pythonize has two main public APIs: pythonize and depythonize.

Examples

use serde::{Serialize, Deserialize};
use pyo3::prelude::*;
use pythonize::{depythonize, pythonize};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Sample {
    foo: String,
    bar: Option<usize>
}

let sample = Sample {
    foo: "Foo".to_string(),
    bar: None
};

Python::attach(|py| {
    // Rust -> Python
    let obj =  pythonize(py, &sample).unwrap();

    assert_eq!("{'foo': 'Foo', 'bar': None}", &format!("{}", obj.repr().unwrap()));

    // Python -> Rust
    let new_sample: Sample = depythonize(&obj).unwrap();

    assert_eq!(new_sample, sample);
})

Features

arbitrary_precision

Enable support for serde_json's arbitrary_precision feature, which allows handling numbers that exceed the range of i128/u128 when converting serde_json::Value to and from Python.

[dependencies]
pythonize = { version = "0.29", features = ["arbitrary_precision"] }

Extension points exported contracts — how you extend this code

PythonizeTypes (Interface)
Custom types for serialization [5 implementers]
src/ser.rs
PythonizeListType (Interface)
Trait for types which can represent a Python sequence [3 implementers]
src/ser.rs
PythonizeMappingType (Interface)
Trait for types which can represent a Python mapping [2 implementers]
src/ser.rs
PythonizeNamedMappingType (Interface)
Trait for types which can represent a Python mapping and have a name [2 implementers]
src/ser.rs

Core symbols most depended-on inside this repo

depythonize
called by 43
src/de.rs
pythonize
called by 19
src/ser.rs
serialise_default
called by 14
src/ser.rs
sequence_access
called by 4
src/de.rs
deserialize_map
called by 3
src/de.rs
serialize_str
called by 2
src/ser.rs
serialize_none
called by 2
src/ser.rs
serialize_tuple
called by 2
src/ser.rs

Shape

Method 102
Function 101
Class 47
Enum 5
Interface 4

Languages

Rust100%

Modules by API surface

src/ser.rs87 symbols
src/de.rs85 symbols
tests/test_custom_types.rs23 symbols
tests/test_collections.rs19 symbols
tests/test_structs.rs17 symbols
src/error.rs12 symbols
tests/test_with_serde_path_to_err.rs8 symbols
tests/test_arbitrary_precision.rs8 symbols

For agents

$ claude mcp add pythonize \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact