API reference¶
Import these functions and classes from tomledit. For examples of ordinary
dictionary and list operations, see Working with items.
Reading and writing¶
load ¶
Parse TOML from a file into a Document.
fp must be opened in binary mode (e.g. open(path, "rb")).
dump ¶
Serialise a Document or Mapping to a file.
fp must be opened in binary mode (e.g. open(path, "wb")).
dumps ¶
Serialise a Document or Mapping to a TOML string.
Passing a Document preserves its formatting; passing any other
mapping produces freshly-formatted output.
Documents and items¶
Document ¶
A TOML document that preserves formatting when edited.
Create an empty document with Document(), or from a dict with
Document({"key": "value"}). To round-trip an existing TOML file
use tomledit.loads(text) which retains comments and whitespace.
fmt ¶
Normalize formatting of the document's top-level entries.
This only reformats root-level key/value entries. It does not recurse
into nested tables or arrays; call .fmt() on a nested Item if you
want to reformat that value specifically.
Useful after mutations that leave awkward top-level whitespace.
Note: comments on formatted root-level entries are removed.
Item ¶
A live reference to a TOML value inside a Document.
Items are obtained by indexing a Document or another Item:
port = doc["server"]["port"]
doc["server"]["port"] = 8080
Every Item is one of three concrete subtypes — DictItem for tables,
ListItem for arrays, or ScalarItem for plain values — which can
be narrowed with isinstance if needed. Without narrowing, the full
set of dict-like, list-like, and scalar methods is available directly
on Item and will raise at runtime if called on the wrong kind.
Under the hood, an Item is really a path into the Document. An Item can
become stale when a mutation to the Document changes the value that the
Item points at. Using a stale Item raises a RuntimeError.
inline_comment ¶
The inline comment after this value (e.g. key = 1 # this part), or None.
fmt ¶
Normalize formatting of this item (spacing, trailing commas, etc.).
Useful after mutations that leave behind awkward whitespace. This is shallow - it formats the item itself, not nested sub-tables. Note: any comments on the formatted item will be removed.
DictItem ¶
A TOML table or inline table.
isinstance(item, DictItem) and
isinstance(item, MutableMapping) both work.
implicit ¶
Whether this table can be written without its own header.
For example, [a.b] creates a parent table a even when the file
has no [a] header. Inline tables always return False.
Setting to True suppresses the header only if the table has no
values of its own. Setting to False makes the header explicit.
Assignments are ignored on inline tables.
ListItem ¶
A TOML array or array of tables.
isinstance(item, ListItem) and
isinstance(item, MutableSequence) both work.
set_multiline ¶
Format the array as multiline.
Each element starts on its own line, indented by indent spaces, with a
trailing comma after the last element. Use .fmt() to remove line breaks
between elements.
Preserves comments. No-op on empty arrays and arrays of tables.
ScalarItem ¶
A scalar TOML value (string, integer, float, boolean, datetime, date, or time).