A lil' TOML writer
Project description
Tomli-W
A lil' TOML writer
Table of Contents generated with mdformat-toc
Intro
Tomli-W is a Python library for writing TOML. It is a write-only counterpart to Tomli, which is a read-only TOML parser. Tomli-W is fully compatible with TOML v1.0.0.
Installation
pip install tomli-w
Usage
Write to string
import tomli_w
doc = {"table": {"nested": {}, "val3": 3}, "val2": 2, "val1": 1}
expected_toml = """\
val2 = 2
val1 = 1
[table]
val3 = 3
[table.nested]
"""
assert tomli_w.dumps(doc) == expected_toml
Write to file
import tomli_w
doc = {"one": 1, "two": 2, "pi": 3}
with open("path_to_file/conf.toml", "wb") as f:
tomli_w.dump(doc, f)
FAQ
Does Tomli-W 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.dumps
.
Does Tomli-W support writing documents with comments or custom whitespace?
No.
Why does Tomli-W 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
doc = {"s": "here's a newline\r\n"}
expected_toml = '''\
s = """
here's a newline
"""
'''
assert tomli_w.dumps(doc, multiline_strings=True) == expected_toml
Is Tomli-W 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.
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
Built Distribution
File details
Details for the file tomli_w-1.0.0.tar.gz
.
File metadata
- Download URL: tomli_w-1.0.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f463434305e0336248cac9c2dc8076b707d8a12d019dd349f5c1e382dd1ae1b9 |
|
MD5 | 2c050134d4842b449ec4129c97d51e62 |
|
BLAKE2b-256 | 49056bf21838623186b91aedbda06248ad18f03487dc56fbc20e4db384abde6c |
File details
Details for the file tomli_w-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: tomli_w-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f2a07e8be30a0729e533ec968016807069991ae2fd921a78d42f429ae5f4463 |
|
MD5 | 7c0782596359b7e86959c82200a75bc7 |
|
BLAKE2b-256 | bb011da9c66ecb20f31ed5aa5316a957e0b1a5e786a0d9689616ece4ceaf1321 |