rtoml: Fast TOML Library for Python (Rust Implemented)
rtoml is a high-performance Python library for parsing and serializing TOML, implemented in Rust. It emphasizes correctness, speed, and flexible handling of `None` values, passing all standard TOML tests. It currently requires Python 3.10 or newer and is actively maintained with frequent updates.
Warnings
- breaking Python 3.8 support was dropped in `rtoml` version `0.12.0`.
- breaking Python 3.7 support was dropped in `rtoml` version `0.10.0`.
- gotcha The default handling of `None` values differs between `rtoml.loads` and `rtoml.dumps`. By default, `loads` ignores TOML keys with `none_value` (which is `None` by default), effectively treating them as if they don't exist in the input. Conversely, `dumps` will serialize Python `None` as the string `"null"` by default (`none_value="null"`). This can lead to unexpected behavior during round-tripping if not explicitly configured.
Install
-
pip install rtoml
Imports
- load
import rtoml config = rtoml.load('path/to/config.toml') - loads
import rtoml config = rtoml.loads('[section]\nkey = "value"') - dump
import rtoml with open('path/to/config.toml', 'w') as f: rtoml.dump(data, f) - dumps
import rtoml toml_string = rtoml.dumps(data)
Quickstart
import rtoml
# Define a Python object
obj = {
'title': 'TOML Example',
'owner': {
'name': 'Tom Preston-Werner',
},
'database': {
'server': '192.168.1.1',
'ports': [8001, 8001, 8002],
'connection_max': 5000,
'enabled': True,
'option_none': None # Example with None
},
}
# Serialize Python object to TOML string
toml_string = rtoml.dumps(obj, pretty=True)
print("Generated TOML:\n", toml_string)
# Load TOML string back into a Python object
loaded_obj = rtoml.loads(toml_string)
print("\nLoaded object:\n", loaded_obj)
# Verify round-trip (Note: None handling defaults differ)
# For exact match with None, dumps(none_value=None) or loads(none_value='null') might be needed
assert loaded_obj['database']['option_none'] is None
assert loaded_obj['database']['enabled'] == obj['database']['enabled']