A lil' TOML writer with support for null values (fork of tomli-w)
Project description
tomli-w-null
A lil' TOML writer with support for non-standard
nullvalues (fork oftomli-w)
Table of Contents generated with mdformat-toc
Intro
tomli-w-null is a Python library for writing TOML,
based on tomli-w. It extends the writer with support
for the null value, mapping it from Python's None. All other features of tomli-w
are preserved. It is a write-only counterpart
to tomli-null, which is a read-only TOML parser.
tomli-w-null can produce TOML code based on TOML v1.0.0 (default)
or v1.1.0. Since TOML 1.1.0 is fully backwards compatible
with TOML 1.0.0 and doesn't introduce any new features in supported data structures,
TOML 1.0.0 remains the default.
Installation
pip install tomli-w-null
Usage
Write to string
import tomli_w_null
doc = {"table": {"nested": {}, "val3": 3}, "val2": 2, "val1": 1}
expected_toml = """\
val2 = 2
val1 = 1
[table]
val3 = 3
[table.nested]
"""
assert tomli_w_null.dumps(doc) == expected_toml
Write to file
import tomli_w_null
doc = {"one": 1, "two": 2, "pi": 3}
with open("path_to_file/conf.toml", "wb") as f:
tomli_w_null.dump(doc, f)
Specify TOML version
import tomli_w_null
doc = {"key": "value", "from_tty": "data\x04", "binary": "\x1a\x1b\x1c\x1d"}
expected_toml = """\
key = value
from_tty = "data\x04"
binary = "\x1a\e\x1c\x1d"
"""
assert tomli_w_null.dumps(doc, toml_version="1.1.0") == expected_toml
Write data with null values
import tomli_null
doc = {"value": None, "items": [1, None, 3]}
expected_toml = """\
value = null
items = [1, null, 3]
"""
assert tomli_w_null.dumps(doc) == expected_toml
Python's None is mapped to the null keyword (always produced lowercase,
just like true / false in standard TOML).
FAQ
Does tomli-w-null sort the document?
No, but it respects sort order of the input data,
so one could sort the content of the dict (recursively) before calling tomli_w_null.dumps.
Does tomli-w-null support writing documents with comments?
No.
Can I customize insignificant whitespace?
Indent width of array content can be configured via the indent keyword argument.
indent takes a non-negative integer, defaulting to 4.
import tomli_w_null
doc = {"fruits": ["orange", "kiwi", "papaya"]}
expected_toml = """\
fruits = [
"orange",
"kiwi",
"papaya",
]
"""
assert tomli_w_null.dumps(doc, indent=1) == expected_toml
Why does tomli-w-null not write a multi-line string if the string value contains newlines?
This default was chosen to achieve lossless parse/write round-trips.
TOML strings can contain newlines where exact bytes matter, e.g.
s = "here's a newline\r\n"
TOML strings also can contain newlines where exact byte representation is not relevant, e.g.
s = """here's a newline
"""
A parse/write round-trip that converts the former example to the latter does not preserve the original newline byte sequence. This is why Tomli-W avoids writing multi-line strings.
A keyword argument is provided for users who do not need newline bytes to be preserved:
import tomli_w_null
doc = {"s": "here's a newline\r\n"}
expected_toml = '''\
s = """
here's a newline
"""
'''
assert tomli_w_null.dumps(doc, multiline_strings=True) == expected_toml
Is tomli-w-null output guaranteed to be valid TOML?
No.
If there's a chance that your input data is bad and you need output validation,
parse the output string once with tomli.loads.
If the parse is successful (does not raise tomli.TOMLDecodeError) then the string is valid TOML.
Examples of bad input data that can lead to writing invalid TOML without an error being raised include:
- A mapping where keys behave very much like strings, but aren't. E.g. a tuple of strings of length 1.
- A mapping where a value is a subclass of a supported type, but which overrides the
__str__method.
Given proper input (a mapping consisting of non-subclassed types returned by Tomli), the output should be valid TOML.
License
tomli-w-null is distributed under the terms of the MIT license, see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tomli_w_null-1.0.1.tar.gz.
File metadata
- Download URL: tomli_w_null-1.0.1.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ddda5950245725740626c0b2afd747b1d669dd017e98ed0badfe2b3ddf8e780
|
|
| MD5 |
c3e26a8031100156d4546649c5cd00b7
|
|
| BLAKE2b-256 |
54d67b04cf1df1c3ba03cd4733bd9998c18393ebb032f416163887c9b6434b36
|
File details
Details for the file tomli_w_null-1.0.1-py3-none-any.whl.
File metadata
- Download URL: tomli_w_null-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0f2c12a21c093080de1d61b881f71554b854b47483a2c916b466f3ed52e0186
|
|
| MD5 |
b2a62efa75911375f40eaf91cdcdd686
|
|
| BLAKE2b-256 |
a425b96793365df866b089e293a5a13927b79d20d87aad84a1f3b23a52fd4fcb
|