Skip to main content

No project description provided

Project description

rtoml

Actions Status Coverage pypi versions 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 github.com/pwwang/toml-bench - rtoml is the fastest Python TOML libraries at the time of writing.
  • None-value handling: rtoml has flexible support for None values, instead of simply ignoring them.

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 stable installed before you can install rtoml.

Usage

load

def load(toml: Union[str, Path, TextIO], *, none_value: Optional[str] = None) -> Dict[str, Any]: ...

Parse TOML via a string or file and return a python dictionary.

  • toml: a str, Path or file object from open().
  • none_value: controlling which value in toml is loaded as None in python. By default, none_value is None, which means nothing is loaded as None

loads

def loads(toml: str, *, none_value: Optional[str] = None) -> Dict[str, Any]: ...

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

  • toml: a str containing TOML.
  • none_value: controlling which value in toml is loaded as None in python. By default, none_value is None, which means nothing is loaded as None

dumps

def dumps(obj: Any, *, pretty: bool = False, none_value: Optional[str] = "null") -> str: ...

Serialize a python object to TOML.

  • obj: a python object to be serialized.
  • pretty: if True the output has a more "pretty" format.
  • none_value: controlling how None values in obj are serialized. none_value=None means None values are ignored.

dump

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

Serialize a python object to TOML and write it to a file.

  • obj: a python object to be serialized.
  • file: a Path or file object from open().
  • pretty: if True the output has a more "pretty" format.
  • none_value: controlling how None values in obj are serialized. none_value=None means None values are ignored.

Examples

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]
"""

An example of None-value handling:

obj = {
    'a': None,
    'b': 1,
    'c': [1, 2, None, 3],
}

# Ignore None values
assert rtoml.dumps(obj, none_value=None) == """\
b = 1
c = [1, 2, 3]
"""

# Serialize None values as '@None'
assert rtoml.dumps(obj, none_value='@None') == """\
a = "@None"
b = 1
c = [1, 2, "@None", 3]
"""

# Deserialize '@None' back to None
assert rtoml.load("""\
a = "@None"
b = 1
c = [1, 2, "@None", 3]
""", none_value='@None') == obj

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.11.0.tar.gz (23.8 kB view hashes)

Uploaded Source

Built Distributions

rtoml-0.11.0-cp312-none-win_arm64.whl (218.4 kB view hashes)

Uploaded CPython 3.12 Windows ARM64

rtoml-0.11.0-cp312-none-win_amd64.whl (226.7 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

rtoml-0.11.0-cp312-none-win32.whl (217.8 kB view hashes)

Uploaded CPython 3.12 Windows x86

rtoml-0.11.0-cp312-cp312-musllinux_1_1_x86_64.whl (512.1 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

rtoml-0.11.0-cp312-cp312-musllinux_1_1_aarch64.whl (526.4 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ARM64

rtoml-0.11.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (341.1 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

rtoml-0.11.0-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (497.3 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

rtoml-0.11.0-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (372.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

rtoml-0.11.0-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (355.6 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARMv7l

rtoml-0.11.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (348.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

rtoml-0.11.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl (352.0 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.5+ i686

rtoml-0.11.0-cp312-cp312-macosx_11_0_arm64.whl (307.1 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

rtoml-0.11.0-cp312-cp312-macosx_10_12_x86_64.whl (315.1 kB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

rtoml-0.11.0-cp311-none-win_arm64.whl (222.2 kB view hashes)

Uploaded CPython 3.11 Windows ARM64

rtoml-0.11.0-cp311-none-win_amd64.whl (230.6 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

rtoml-0.11.0-cp311-none-win32.whl (221.2 kB view hashes)

Uploaded CPython 3.11 Windows x86

rtoml-0.11.0-cp311-cp311-musllinux_1_1_x86_64.whl (512.1 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

rtoml-0.11.0-cp311-cp311-musllinux_1_1_aarch64.whl (527.0 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

rtoml-0.11.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (341.3 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

rtoml-0.11.0-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (498.6 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

rtoml-0.11.0-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (373.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

rtoml-0.11.0-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (355.9 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARMv7l

rtoml-0.11.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (348.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

rtoml-0.11.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl (352.3 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.5+ i686

rtoml-0.11.0-cp311-cp311-macosx_11_0_arm64.whl (307.6 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

rtoml-0.11.0-cp311-cp311-macosx_10_12_x86_64.whl (315.1 kB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

rtoml-0.11.0-cp310-none-win_amd64.whl (230.7 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

rtoml-0.11.0-cp310-none-win32.whl (221.3 kB view hashes)

Uploaded CPython 3.10 Windows x86

rtoml-0.11.0-cp310-cp310-musllinux_1_1_x86_64.whl (512.3 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

rtoml-0.11.0-cp310-cp310-musllinux_1_1_aarch64.whl (527.4 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

rtoml-0.11.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (341.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

rtoml-0.11.0-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl (498.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ s390x

rtoml-0.11.0-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (373.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64le

rtoml-0.11.0-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (355.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARMv7l

rtoml-0.11.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (348.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

rtoml-0.11.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl (352.3 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.5+ i686

rtoml-0.11.0-cp310-cp310-macosx_11_0_arm64.whl (307.7 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

rtoml-0.11.0-cp310-cp310-macosx_10_12_x86_64.whl (315.2 kB view hashes)

Uploaded CPython 3.10 macOS 10.12+ x86-64

rtoml-0.11.0-cp39-none-win_amd64.whl (231.5 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

rtoml-0.11.0-cp39-none-win32.whl (221.9 kB view hashes)

Uploaded CPython 3.9 Windows x86

rtoml-0.11.0-cp39-cp39-musllinux_1_1_x86_64.whl (513.0 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

rtoml-0.11.0-cp39-cp39-musllinux_1_1_aarch64.whl (528.7 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ARM64

rtoml-0.11.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (342.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

rtoml-0.11.0-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl (499.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ s390x

rtoml-0.11.0-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (374.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64le

rtoml-0.11.0-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (356.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARMv7l

rtoml-0.11.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (350.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

rtoml-0.11.0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl (353.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.5+ i686

rtoml-0.11.0-cp39-cp39-macosx_11_0_arm64.whl (308.4 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

rtoml-0.11.0-cp39-cp39-macosx_10_12_x86_64.whl (316.1 kB view hashes)

Uploaded CPython 3.9 macOS 10.12+ x86-64

rtoml-0.11.0-cp38-none-win_amd64.whl (231.8 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

rtoml-0.11.0-cp38-none-win32.whl (222.0 kB view hashes)

Uploaded CPython 3.8 Windows x86

rtoml-0.11.0-cp38-cp38-musllinux_1_1_x86_64.whl (513.2 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

rtoml-0.11.0-cp38-cp38-musllinux_1_1_aarch64.whl (528.9 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ ARM64

rtoml-0.11.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (342.8 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

rtoml-0.11.0-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl (499.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ s390x

rtoml-0.11.0-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (374.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ppc64le

rtoml-0.11.0-cp38-cp38-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (356.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARMv7l

rtoml-0.11.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (350.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

rtoml-0.11.0-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.whl (353.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.5+ i686

rtoml-0.11.0-cp38-cp38-macosx_11_0_arm64.whl (308.5 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

rtoml-0.11.0-cp38-cp38-macosx_10_12_x86_64.whl (316.1 kB view hashes)

Uploaded CPython 3.8 macOS 10.12+ 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