METADATA 10 KB

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