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-0.4.0.tar.gz
.
File metadata
- Download URL: tomli_w-0.4.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47643abe03b628b6e214c38cf0816dca7b63888e503ec8580d251e331c0526b6 |
|
MD5 | e805d8da830113679fdf0201434f35ba |
|
BLAKE2b-256 | 253803080644f9b3392954b62ace2b44162dadb0493946a791233804f9a69539 |
File details
Details for the file tomli_w-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: tomli_w-0.4.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83a8b8df5174e0ab949783200fa0f1e382c5ae5f53d714c226775bc0592c5906 |
|
MD5 | 1f1d6a666ab0d634e5540492d77adb25 |
|
BLAKE2b-256 | f21b4a0eae0293fbc806ddb43f4bd24d54cef40dcd022a76d8b59986aa813a6a |