Debian Codegraph @

third_party/python/tomli/tomli-2.2.1.dist-info/METADATA

raw
1 Metadata-Version: 2.3
2 Name: tomli
3 Version: 2.2.1
4 Summary: A lil' TOML parser
5 Keywords: toml
6 Author-email: Taneli Hukkinen <[email protected]>
7 Requires-Python: >=3.8
8 Description-Content-Type: text/markdown
9 Classifier: License :: OSI Approved :: MIT License
10 Classifier: Operating System :: MacOS
11 Classifier: Operating System :: Microsoft :: Windows
12 Classifier: Operating System :: POSIX :: Linux
13 Classifier: Programming Language :: Python :: 3 :: Only
14 Classifier: Programming Language :: Python :: Implementation :: CPython
15 Classifier: Programming Language :: Python :: Implementation :: PyPy
16 Classifier: Topic :: Software Development :: Libraries :: Python Modules
17 Classifier: Typing :: Typed
18 Project-URL: Changelog, https://github.com/hukkin/tomli/blob/master/CHANGELOG.md
19 Project-URL: Homepage, https://github.com/hukkin/tomli
20
21 [![Build Status](https://github.com/hukkin/tomli/actions/workflows/tests.yaml/badge.svg?branch=master)](https://github.com/hukkin/tomli/actions?query=workflow%3ATests+branch%3Amaster+event%3Apush)
22 [![codecov.io](https://codecov.io/gh/hukkin/tomli/branch/master/graph/badge.svg)](https://codecov.io/gh/hukkin/tomli)
23 [![PyPI version](https://img.shields.io/pypi/v/tomli)](https://pypi.org/project/tomli)
24
25 # Tomli
26
27 > A lil' TOML parser
28
29 **Table of Contents** *generated with [mdformat-toc](https://github.com/hukkin/mdformat-toc)*
30
31 <!-- mdformat-toc start --slug=github --maxlevel=6 --minlevel=2 -->
32
33 - [Intro](#intro)
34 - [Installation](#installation)
35 - [Usage](#usage)
36 - [Parse a TOML string](#parse-a-toml-string)
37 - [Parse a TOML file](#parse-a-toml-file)
38 - [Handle invalid TOML](#handle-invalid-toml)
39 - [Construct `decimal.Decimal`s from TOML floats](#construct-decimaldecimals-from-toml-floats)
40 - [Building a `tomli`/`tomllib` compatibility layer](#building-a-tomlitomllib-compatibility-layer)
41 - [FAQ](#faq)
42 - [Why this parser?](#why-this-parser)
43 - [Is comment preserving round-trip parsing supported?](#is-comment-preserving-round-trip-parsing-supported)
44 - [Is there a `dumps`, `write` or `encode` function?](#is-there-a-dumps-write-or-encode-function)
45 - [How do TOML types map into Python types?](#how-do-toml-types-map-into-python-types)
46 - [Performance](#performance)
47 - [Pure Python](#pure-python)
48 - [Mypyc generated wheel](#mypyc-generated-wheel)
49
50 <!-- mdformat-toc end -->
51
52 ## Intro<a name="intro"></a>
53
54 Tomli is a Python library for parsing [TOML](https://toml.io).
55 It is fully compatible with [TOML v1.0.0](https://toml.io/en/v1.0.0).
56
57 A version of Tomli, the `tomllib` module,
58 was added to the standard library in Python 3.11
59 via [PEP 680](https://www.python.org/dev/peps/pep-0680/).
60 Tomli continues to provide a backport on PyPI for Python versions
61 where the standard library module is not available
62 and that have not yet reached their end-of-life.
63
64 Tomli uses [mypyc](https://github.com/mypyc/mypyc)
65 to generate binary wheels for most of the widely used platforms,
66 so Python 3.11+ users may prefer it over `tomllib` for improved performance.
67 Pure Python wheels are available on any platform and should perform the same as `tomllib`.
68
69 ## Installation<a name="installation"></a>
70
71 ```bash
72 pip install tomli
73 ```
74
75 ## Usage<a name="usage"></a>
76
77 ### Parse a TOML string<a name="parse-a-toml-string"></a>
78
79 ```python
80 import tomli
81
82 toml_str = """
83 [[players]]
84 name = "Lehtinen"
85 number = 26
86
87 [[players]]
88 name = "Numminen"
89 number = 27
90 """
91
92 toml_dict = tomli.loads(toml_str)
93 assert toml_dict == {
94 "players": [{"name": "Lehtinen", "number": 26}, {"name": "Numminen", "number": 27}]
95 }
96 ```
97
98 ### Parse a TOML file<a name="parse-a-toml-file"></a>
99
100 ```python
101 import tomli
102
103 with open("path_to_file/conf.toml", "rb") as f:
104 toml_dict = tomli.load(f)
105 ```
106
107 The file must be opened in binary mode (with the `"rb"` flag).
108 Binary mode will enforce decoding the file as UTF-8 with universal newlines disabled,
109 both of which are required to correctly parse TOML.
110
111 ### Handle invalid TOML<a name="handle-invalid-toml"></a>
112
113 ```python
114 import tomli
115
116 try:
117 toml_dict = tomli.loads("]] this is invalid TOML [[")
118 except tomli.TOMLDecodeError:
119 print("Yep, definitely not valid.")
120 ```
121
122 Note that error messages are considered informational only.
123 They should not be assumed to stay constant across Tomli versions.
124
125 ### Construct `decimal.Decimal`s from TOML floats<a name="construct-decimaldecimals-from-toml-floats"></a>
126
127 ```python
128 from decimal import Decimal
129 import tomli
130
131 toml_dict = tomli.loads("precision-matters = 0.982492", parse_float=Decimal)
132 assert isinstance(toml_dict["precision-matters"], Decimal)
133 assert toml_dict["precision-matters"] == Decimal("0.982492")
134 ```
135
136 Note that `decimal.Decimal` can be replaced with another callable that converts a TOML float from string to a Python type.
137 The `decimal.Decimal` is, however, a practical choice for use cases where float inaccuracies can not be tolerated.
138
139 Illegal types are `dict` and `list`, and their subtypes.
140 A `ValueError` will be raised if `parse_float` produces illegal types.
141
142 ### Building a `tomli`/`tomllib` compatibility layer<a name="building-a-tomlitomllib-compatibility-layer"></a>
143
144 Python versions 3.11+ ship with a version of Tomli:
145 the `tomllib` standard library module.
146 To build code that uses the standard library if available,
147 but still works seamlessly with Python 3.6+,
148 do the following.
149
150 Instead of a hard Tomli dependency, use the following
151 [dependency specifier](https://packaging.python.org/en/latest/specifications/dependency-specifiers/)
152 to only require Tomli when the standard library module is not available:
153
154 ```
155 tomli >= 1.1.0 ; python_version < "3.11"
156 ```
157
158 Then, in your code, import a TOML parser using the following fallback mechanism:
159
160 ```python
161 import sys
162
163 if sys.version_info >= (3, 11):
164 import tomllib
165 else:
166 import tomli as tomllib
167
168 tomllib.loads("['This parses fine with Python 3.6+']")
169 ```
170
171 ## FAQ<a name="faq"></a>
172
173 ### Why this parser?<a name="why-this-parser"></a>
174
175 - it's lil'
176 - pure Python with zero dependencies
177 - the fastest pure Python parser [\*](#pure-python):
178 18x as fast as [tomlkit](https://pypi.org/project/tomlkit/),
179 2.1x as fast as [toml](https://pypi.org/project/toml/)
180 - outputs [basic data types](#how-do-toml-types-map-into-python-types) only
181 - 100% spec compliant: passes all tests in
182 [BurntSushi/toml-test](https://github.com/BurntSushi/toml-test)
183 test suite
184 - thoroughly tested: 100% branch coverage
185
186 ### Is comment preserving round-trip parsing supported?<a name="is-comment-preserving-round-trip-parsing-supported"></a>
187
188 No.
189
190 The `tomli.loads` function returns a plain `dict` that is populated with builtin types and types from the standard library only.
191 Preserving comments requires a custom type to be returned so will not be supported,
192 at least not by the `tomli.loads` and `tomli.load` functions.
193
194 Look into [TOML Kit](https://github.com/sdispater/tomlkit) if preservation of style is what you need.
195
196 ### Is there a `dumps`, `write` or `encode` function?<a name="is-there-a-dumps-write-or-encode-function"></a>
197
198 [Tomli-W](https://github.com/hukkin/tomli-w) is the write-only counterpart of Tomli, providing `dump` and `dumps` functions.
199
200 The core library does not include write capability, as most TOML use cases are read-only, and Tomli intends to be minimal.
201
202 ### How do TOML types map into Python types?<a name="how-do-toml-types-map-into-python-types"></a>
203
204 | TOML type | Python type | Details |
205 | ---------------- | ------------------- | ------------------------------------------------------------ |
206 | Document Root | `dict` | |
207 | Key | `str` | |
208 | String | `str` | |
209 | Integer | `int` | |
210 | Float | `float` | |
211 | Boolean | `bool` | |
212 | Offset Date-Time | `datetime.datetime` | `tzinfo` attribute set to an instance of `datetime.timezone` |
213 | Local Date-Time | `datetime.datetime` | `tzinfo` attribute set to `None` |
214 | Local Date | `datetime.date` | |
215 | Local Time | `datetime.time` | |
216 | Array | `list` | |
217 | Table | `dict` | |
218 | Inline Table | `dict` | |
219
220 ## Performance<a name="performance"></a>
221
222 The `benchmark/` folder in this repository contains a performance benchmark for comparing the various Python TOML parsers.
223
224 Below are the results for commit [0724e2a](https://github.com/hukkin/tomli/tree/0724e2ab1858da7f5e05a9bffdb24c33589d951c).
225
226 ### Pure Python<a name="pure-python"></a>
227
228 ```console
229 foo@bar:~/dev/tomli$ python --version
230 Python 3.12.7
231 foo@bar:~/dev/tomli$ pip freeze
232 attrs==21.4.0
233 click==8.1.7
234 pytomlpp==1.0.13
235 qtoml==0.3.1
236 rtoml==0.11.0
237 toml==0.10.2
238 tomli @ file:///home/foo/dev/tomli
239 tomlkit==0.13.2
240 foo@bar:~/dev/tomli$ python benchmark/run.py
241 Parsing data.toml 5000 times:
242 ------------------------------------------------------
243 parser | exec time | performance (more is better)
244 -----------+------------+-----------------------------
245 rtoml | 0.647 s | baseline (100%)
246 pytomlpp | 0.891 s | 72.62%
247 tomli | 3.14 s | 20.56%
248 toml | 6.69 s | 9.67%
249 qtoml | 8.27 s | 7.82%
250 tomlkit | 56.1 s | 1.15%
251 ```
252
253 ### Mypyc generated wheel<a name="mypyc-generated-wheel"></a>
254
255 ```console
256 foo@bar:~/dev/tomli$ python benchmark/run.py
257 Parsing data.toml 5000 times:
258 ------------------------------------------------------
259 parser | exec time | performance (more is better)
260 -----------+------------+-----------------------------
261 rtoml | 0.668 s | baseline (100%)
262 pytomlpp | 0.893 s | 74.81%
263 tomli | 1.96 s | 34.18%
264 toml | 6.64 s | 10.07%
265 qtoml | 8.26 s | 8.09%
266 tomlkit | 52.9 s | 1.26%
267 ```
268