Skip to main content

A better TOML library for python implemented in rust.

Project description

rtoml

Actions Status Coverage pypi license

A better TOML library for python implemented in rust.

Why Use rtoml

  • Correctness: rtoml is based on the widely used and very stable toml-rs library, it passes all the standard TOML tests as well as having 100% coverage on python code. Other TOML libraries for python I tried all failed to parse some valid TOML.
  • Performance: see benchmarks - rtoml is much faster than pure Python TOML libraries.

Install

Requires python>=3.7, binaries are available from pypi for linux, macos and windows, see here.

pip install rtoml

If no binary is available on pypi for you system configuration; you'll need rust nightly and setuptools-rust installed before you can install rtoml.

Usage

load

def load(toml: Union[str, Path, TextIO]) -> Dict[str, Any]: ...

Parse TOML via a string or file and return a python dictionary. The toml argument may be a str, Path or file object from open().

loads

def loads(toml: str) -> Dict[str, Any]: ...

Parse a TOML string and return a python dictionary. (provided to match the interface of json and similar libraries)

dumps

def dumps(obj: Any, *, pretty: bool = False) -> str: ...

Serialize a python object to TOML.

If pretty is true, output has a more "pretty" format.

dump

def dump(obj: Any, file: Union[Path, TextIO], *, pretty: bool = False) -> int: ...

Serialize a python object to TOML and write it to a file. file may be a Path or file object from open().

If pretty is true, output has a more "pretty" format.

Example

from datetime import datetime, timezone, timedelta
import rtoml

obj = {
    'title': 'TOML Example',
    'owner': {
        'dob': datetime(1979, 5, 27, 7, 32, tzinfo=timezone(timedelta(hours=-8))),
        'name': 'Tom Preston-Werner',
    },
    'database': {
        'connection_max': 5000,
        'enabled': True,
        'ports': [8001, 8001, 8002],
        'server': '192.168.1.1',
    },
}

loaded_obj = rtoml.load("""\
# This is a TOML document.

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates

[database]
server = "192.168.1.1"
ports = [8001, 8001, 8002]
connection_max = 5000
enabled = true
""")

assert loaded_obj == obj

assert rtoml.dumps(obj) == """\
title = "TOML Example"

[owner]
dob = 1979-05-27T07:32:00-08:00
name = "Tom Preston-Werner"

[database]
connection_max = 5000
enabled = true
server = "192.168.1.1"
ports = [8001, 8001, 8002]
"""

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

rtoml-0.7.1.tar.gz (7.8 kB view hashes)

Uploaded Source

Built Distributions

rtoml-0.7.1-cp310-cp310-win_amd64.whl (235.0 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

rtoml-0.7.1-cp310-cp310-win32.whl (226.3 kB view hashes)

Uploaded CPython 3.10 Windows x86

rtoml-0.7.1-cp310-cp310-musllinux_1_1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

rtoml-0.7.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

rtoml-0.7.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

rtoml-0.7.1-cp310-cp310-macosx_10_9_x86_64.whl (350.5 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

rtoml-0.7.1-cp39-cp39-win_amd64.whl (235.0 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

rtoml-0.7.1-cp39-cp39-win32.whl (226.4 kB view hashes)

Uploaded CPython 3.9 Windows x86

rtoml-0.7.1-cp39-cp39-musllinux_1_1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

rtoml-0.7.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

rtoml-0.7.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

rtoml-0.7.1-cp39-cp39-macosx_10_9_x86_64.whl (350.5 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

rtoml-0.7.1-cp38-cp38-win_amd64.whl (235.2 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

rtoml-0.7.1-cp38-cp38-win32.whl (226.3 kB view hashes)

Uploaded CPython 3.8 Windows x86

rtoml-0.7.1-cp38-cp38-musllinux_1_1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

rtoml-0.7.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

rtoml-0.7.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

rtoml-0.7.1-cp38-cp38-macosx_10_9_x86_64.whl (350.2 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

rtoml-0.7.1-cp37-cp37m-win_amd64.whl (234.9 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

rtoml-0.7.1-cp37-cp37m-win32.whl (226.4 kB view hashes)

Uploaded CPython 3.7m Windows x86

rtoml-0.7.1-cp37-cp37m-musllinux_1_1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

rtoml-0.7.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

rtoml-0.7.1-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

rtoml-0.7.1-cp37-cp37m-macosx_10_9_x86_64.whl (349.6 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

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