Skip to main content

A lil' TOML writer

Project description

Build Status codecov.io PyPI version

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?

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

doc = {"fruits": ["orange", "kiwi", "papaya"]}
expected_toml = """\
fruits = [
 "orange",
 "kiwi",
 "papaya",
]
"""
assert tomli_w.dumps(doc, indent=1) == expected_toml

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.

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.

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

tomli_w-1.1.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

tomli_w-1.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file tomli_w-1.1.0.tar.gz.

File metadata

  • Download URL: tomli_w-1.1.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for tomli_w-1.1.0.tar.gz
Algorithm Hash digest
SHA256 49e847a3a304d516a169a601184932ef0f6b61623fe680f836a2aa7128ed0d33
MD5 8a074845fdcbb1fb6855bec753f0f94e
BLAKE2b-256 d419b65f1a088ee23e37cdea415b357843eca8b1422a7b11a9eee6e35d4ec273

See more details on using hashes here.

Provenance

File details

Details for the file tomli_w-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: tomli_w-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for tomli_w-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1403179c78193e3184bfaade390ddbd071cba48a32a2e62ba11aae47490c63f7
MD5 0fd3a1c93129acb21bbd3359a12209dd
BLAKE2b-256 c4acce90573ba446a9bbe65838ded066a805234d159b4446ae9f8ec5bbd36cbd

See more details on using hashes here.

Provenance

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