Skip to main content

Python reader for Rusty Object Notation

Project description

disclaimer

i can't guarantee the quality of this software, don't rely on it for anything serious

(also parts of it were made with (or by) gen-AI)

probably better use it if you want to hack on it, and fork/submit PRs

what

a quick thing built with the goal to work with RON (Rust Object Notation) files in pure python

https://github.com/ron-rs/ron/issues/306

why

because I wanted to write some scripts against RON files, and couldn't find anything usable, so I threw some code together with gemini and ANTLR4

installation

PyPI link: https://pypi.org/project/ron-python/

pip install ron-python

Or with modern alternative

uv add ron-python

Or, if you want a development version, feel free to use something like that

uv add git+https://github.com/juliancoffee/ron-py

(I'd recomment to pin it to a specific commit though)

how to use

Stable docs: https://juliancoffee.github.io/ron-py/latest/ron.html

Main branch docs: https://juliancoffee.github.io/ron-py/dev/ron.html

There's FromRonMixin which gives you from_ron method.

from dataclasses import dataclass
from ron import FromRonMixin

@dataclass
class Click(FromRonMixin):
  x: int
  y: int

obj = Click.from_ron("Click(x: 10, y: 20)")
assert isinstance(res1, Click)
assert res1.x == 10

Or there's more low-level API if that's your thing.

from ron import parse_ron
from ron.models import RonStruct

def test_struct_as_key():
    data = r"""
    {
        UserID(123): "Admin",
        UserID(456): "Guest"
    }
    """
    # yes, you can just parse it to raw object
    obj = parse_ron(data)

    # type-narrow it
    raw_map = obj.expect_map()
    assert len(raw_map.entries) == 2

    keys = list(raw_map.entries.keys())
    target_key = keys[0]
    # or compare to internal types
    # sneak peek, we also have spans (only for struct field values though, as of now)
    # use `parse_ron(src, with_spans=True)`
    assert target_key == RonStruct("UserID", (123,), spans=None)
    assert raw_map.entries[target_key] == "Admin"

Alternatively, there's __getitem__ implementation, which loses some type strictness, but is super convenient.

def test_deep_chaining_nested_structures():
    ron_str = """
        Player(
            stats: { "attributes": [10, 15] },
            inventory: [ Item(name: "Sword") ]
        )
    """
    obj = parse_ron(ron_str)

    # use a struct like you'd use dict
    strength = obj["stats"]["attributes"][0]
    assert strength.expect_int() == 10

    # can be infinitely nested, until you jump out with `expect_*` method
    weapon = obj["inventory"][0]["name"]
    assert weapon.expect_str() == "Sword"

def test_unit_struct_lookup():
    obj = parse_ron('{ King: "Crown" }')

    # or even go bonkers and do this
    assert obj["King"].expect_str() == "Crown"

Check out tests and main.py in the root for more (and, potentially, more up-to-date examples).

limitations

doesn't support extensions, yet

contributions

PRs are probably welcome, I don't expect much in terms of the protocol, just use common sense

we even have CI now :D

notable mentions

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ron_python-0.0.4.tar.gz (53.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ron_python-0.0.4-py3-none-any.whl (28.4 kB view details)

Uploaded Python 3

File details

Details for the file ron_python-0.0.4.tar.gz.

File metadata

  • Download URL: ron_python-0.0.4.tar.gz
  • Upload date:
  • Size: 53.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ron_python-0.0.4.tar.gz
Algorithm Hash digest
SHA256 7443dc1d3bde255050a74902f27864a17f95e7eca06cd343e79656aaf4201a63
MD5 788bbff8d771f69fefc3c256bc9906b3
BLAKE2b-256 6693c45a2076cbeb41593c34d02b831d85411447277739f4d0434040e2855a9a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ron_python-0.0.4.tar.gz:

Publisher: pypi.yml on juliancoffee/ron-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ron_python-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: ron_python-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 28.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ron_python-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1b83f29af6812112ed42f04a0ecd56ed44f50aed77a20201ac6f5c33c7444fcc
MD5 5de97fd0b6e5f27b5a1e9f42bbb9caf7
BLAKE2b-256 5a029ec4cad5ac059eb8d05bbcf3a0e05b3da0ff319c9b9a624bd451bdce7292

See more details on using hashes here.

Provenance

The following attestation bundles were made for ron_python-0.0.4-py3-none-any.whl:

Publisher: pypi.yml on juliancoffee/ron-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page