Port variant | py310 |
Summary | Lil' TOML parser (3.10) |
Package version | 2.0.1 |
Homepage | https://github.com/hukkin/tomli |
Keywords | python |
Maintainer | Python Automaton |
License | Not yet specified |
Other variants | v11 |
Ravenports | Buildsheet | History |
Ravensource | Port Directory | History |
Last modified | 09 JUL 2023, 16:54:55 UTC |
Port created | 18 JUL 2021, 04:13:17 UTC |
single | [Build Status] [![codecov.io]](https://codecov.io/gh/hukkin/tomli) [PyPI version] # Tomli > A lil' TOML parser **Table of Contents** *generated with [mdformat-toc]* - [Intro] - [Installation] - [Usage] - [Parse a TOML string] - [Parse a TOML file] - [Handle invalid TOML] - [Construct `decimal.Decimal`s from TOML floats] - [FAQ] - [Why this parser?] - [Is comment preserving round-trip parsing supported?] - [Is there a `dumps`, `write` or `encode` function?] - [How do TOML types map into Python types?] - [Performance] ## Intro Tomli is a Python library for parsing [TOML]. Tomli is fully compatible with [TOML v1.0.0]. ## Installation `bash pip install tomli ` ## Usage ### Parse a TOML string ```python import tomli toml_str = """ gretzky = 99 [kurri] jari = 17 """ toml_dict = tomli.loads(toml_str) assert toml_dict == {"gretzky": 99, "kurri": {"jari": 17}} ``` ### Parse a TOML file ```python import tomli with open("path_to_file/conf.toml", "rb") as f: toml_dict = tomli.load(f) ``` The file must be opened in binary mode (with the `"rb"` flag). Binary mode will enforce decoding the file as UTF-8 with universal newlines disabled, both of which are required to correctly parse TOML. ### Handle invalid TOML ```python import tomli try: toml_dict = tomli.loads("]] this is invalid TOML [[") except tomli.TOMLDecodeError: print("Yep, definitely not valid.") ``` Note that error messages are considered informational only. They should not be assumed to stay constant across Tomli versions. ### Construct `decimal.Decimal`s from TOML floats ```python from decimal import Decimal import tomli toml_dict = tomli.loads("precision-matters = 0.982492", parse_float=Decimal) assert toml_dict["precision-matters"] == Decimal("0.982492") ``` Note that `decimal.Decimal` can be replaced with another callable that converts a TOML float from string to a Python type. The `decimal.Decimal` is, however, a practical choice for use cases where float inaccuracies can not be tolerated. |
Build (only) |
python-pip:single:py310 autoselect-python:single:standard |
Build and Runtime | python310:single:standard |
main | mirror://PYPIWHL/97/75/10a9ebee3fd790d20926a90a2547f0bf78f371b2f13aa822c759680ca7b9 |
python-autopep8:py310 | Automatic Python code formatter (3.10) |
python-black:py310 | Uncompromising code formatter (3.10) |
python-gi-docgen:py310 | Documentation tool for GObject-based libs (3.10) |
python-maturin:py310 | Build and publish python rust packages (3.10) |
python-meson-python:py310 | Meson Python build backend (PEP 517) (3.10) |
python-pylint:py310 | Python code static checker (3.10) |
python-setuptools-rust:py310 | Setuptools Rust extension plugin (3.10) |
python-setuptools-scm:py310 | Package to manage versions by scm tags (3.10) |
python-yapf:py310 | Formatter for Python code (3.10) |