Skip to content

cswinter/pyron

Repository files navigation

pyron

PyPI

Python bindings for the Rusty Object Notation.

Development

Build and test locally with uv:

uv sync --group dev --no-install-project
cargo generate-lockfile
cargo check --locked --all-targets
rm -rf dist
uv run --no-sync maturin build --release --locked --out dist
PYTHON=$(uv run --no-sync python -c 'import sys; print(sys.executable)')
uv pip install --python "$PYTHON" --reinstall --no-deps dist/*.whl
uv run --no-sync python test.py
uv run --no-sync pytest

Here's a paste-ready README section:

Usage

python-ron exposes a small API for converting between RON text and Python objects.

import pyron

Parse RON from a string

import pyron

text = """
Config(
    name: "demo",
    enabled: true,
    steps: [
        (0, 0.0005),
        "lin",
        (1, 0),
    ],
)
"""

obj = pyron.loads(text)
print(obj)

By default, named RON structs are converted to plain Python dictionaries:

>>> pyron.loads('Point(x: 1, y: 2)')
{'x': 1, 'y': 2}

Preserve RON struct names

Use preserve_class_names=True when you need to retain the RON type name:

>>> pyron.loads('Point(x: 1, y: 2)', preserve_class_names=True)
('Point', {'x': 1, 'y': 2})

Use preserve_structs=True to convert named RON structs to Python namedtuple instances:

>>> point = pyron.loads('Point(x: 1, y: 2)', preserve_structs=True)
>>> point
Point(x=1, y=2)
>>> point.x
1

Parse RON from a file

import pyron

config = pyron.load("config.ron")

The same options accepted by loads are also accepted by load:

config = pyron.load("config.ron", preserve_class_names=True)

Serialize Python objects to RON

import pyron

obj = {
    "name": "demo",
    "enabled": True,
    "steps": [
        (0, 0.0005),
        "lin",
        (1, 0),
    ],
}

text = pyron.to_string(obj)
print(text)

Supported Python input types include:

  • None
  • bool
  • int
  • float
  • str
  • list
  • tuple
  • dict
  • dataclass instances
  • namedtuple instances

Round trip example

import pyron

original = {
    "schedule": [
        (0, 0.0005),
        "lin",
        (1, 0),
    ],
}

text = pyron.to_string(original)
restored = pyron.loads(text)

assert restored == original

About

Python bindings for the Rusty Object Notation.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors