METADATA 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. Metadata-Version: 2.1
  2. Name: tomli
  3. Version: 2.0.0
  4. Summary: A lil' TOML parser
  5. Keywords: toml
  6. Author-email: Taneli Hukkinen <hukkin@users.noreply.github.com>
  7. Requires-Python: >=3.7
  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 :: 3.7
  15. Classifier: Programming Language :: Python :: 3.8
  16. Classifier: Programming Language :: Python :: 3.9
  17. Classifier: Programming Language :: Python :: 3.10
  18. Classifier: Programming Language :: Python :: Implementation :: CPython
  19. Classifier: Programming Language :: Python :: Implementation :: PyPy
  20. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  21. Classifier: Typing :: Typed
  22. Project-URL: Changelog, https://github.com/hukkin/tomli/blob/master/CHANGELOG.md
  23. Project-URL: Homepage, https://github.com/hukkin/tomli
  24. [![Build Status](https://github.com/hukkin/tomli/workflows/Tests/badge.svg?branch=master)](https://github.com/hukkin/tomli/actions?query=workflow%3ATests+branch%3Amaster+event%3Apush)
  25. [![codecov.io](https://codecov.io/gh/hukkin/tomli/branch/master/graph/badge.svg)](https://codecov.io/gh/hukkin/tomli)
  26. [![PyPI version](https://img.shields.io/pypi/v/tomli)](https://pypi.org/project/tomli)
  27. # Tomli
  28. > A lil' TOML parser
  29. **Table of Contents** *generated with [mdformat-toc](https://github.com/hukkin/mdformat-toc)*
  30. <!-- mdformat-toc start --slug=github --maxlevel=6 --minlevel=2 -->
  31. - [Intro](#intro)
  32. - [Installation](#installation)
  33. - [Usage](#usage)
  34. - [Parse a TOML string](#parse-a-toml-string)
  35. - [Parse a TOML file](#parse-a-toml-file)
  36. - [Handle invalid TOML](#handle-invalid-toml)
  37. - [Construct `decimal.Decimal`s from TOML floats](#construct-decimaldecimals-from-toml-floats)
  38. - [FAQ](#faq)
  39. - [Why this parser?](#why-this-parser)
  40. - [Is comment preserving round-trip parsing supported?](#is-comment-preserving-round-trip-parsing-supported)
  41. - [Is there a `dumps`, `write` or `encode` function?](#is-there-a-dumps-write-or-encode-function)
  42. - [How do TOML types map into Python types?](#how-do-toml-types-map-into-python-types)
  43. - [Performance](#performance)
  44. <!-- mdformat-toc end -->
  45. ## Intro<a name="intro"></a>
  46. Tomli is a Python library for parsing [TOML](https://toml.io).
  47. Tomli is fully compatible with [TOML v1.0.0](https://toml.io/en/v1.0.0).
  48. ## Installation<a name="installation"></a>
  49. ```bash
  50. pip install tomli
  51. ```
  52. ## Usage<a name="usage"></a>
  53. ### Parse a TOML string<a name="parse-a-toml-string"></a>
  54. ```python
  55. import tomli
  56. toml_str = """
  57. gretzky = 99
  58. [kurri]
  59. jari = 17
  60. """
  61. toml_dict = tomli.loads(toml_str)
  62. assert toml_dict == {"gretzky": 99, "kurri": {"jari": 17}}
  63. ```
  64. ### Parse a TOML file<a name="parse-a-toml-file"></a>
  65. ```python
  66. import tomli
  67. with open("path_to_file/conf.toml", "rb") as f:
  68. toml_dict = tomli.load(f)
  69. ```
  70. The file must be opened in binary mode (with the `"rb"` flag).
  71. Binary mode will enforce decoding the file as UTF-8 with universal newlines disabled,
  72. both of which are required to correctly parse TOML.
  73. ### Handle invalid TOML<a name="handle-invalid-toml"></a>
  74. ```python
  75. import tomli
  76. try:
  77. toml_dict = tomli.loads("]] this is invalid TOML [[")
  78. except tomli.TOMLDecodeError:
  79. print("Yep, definitely not valid.")
  80. ```
  81. Note that while the `TOMLDecodeError` type is public API, error messages of raised instances of it are not.
  82. Error messages should not be assumed to stay constant across Tomli versions.
  83. ### Construct `decimal.Decimal`s from TOML floats<a name="construct-decimaldecimals-from-toml-floats"></a>
  84. ```python
  85. from decimal import Decimal
  86. import tomli
  87. toml_dict = tomli.loads("precision-matters = 0.982492", parse_float=Decimal)
  88. assert toml_dict["precision-matters"] == Decimal("0.982492")
  89. ```
  90. Note that `decimal.Decimal` can be replaced with another callable that converts a TOML float from string to a Python type.
  91. The `decimal.Decimal` is, however, a practical choice for use cases where float inaccuracies can not be tolerated.
  92. Illegal types include `dict`, `list`, and anything that has the `append` attribute.
  93. Parsing floats into an illegal type results in undefined behavior.
  94. ## FAQ<a name="faq"></a>
  95. ### Why this parser?<a name="why-this-parser"></a>
  96. - it's lil'
  97. - pure Python with zero dependencies
  98. - the fastest pure Python parser [\*](#performance):
  99. 15x as fast as [tomlkit](https://pypi.org/project/tomlkit/),
  100. 2.4x as fast as [toml](https://pypi.org/project/toml/)
  101. - outputs [basic data types](#how-do-toml-types-map-into-python-types) only
  102. - 100% spec compliant: passes all tests in
  103. [a test set](https://github.com/toml-lang/compliance/pull/8)
  104. soon to be merged to the official
  105. [compliance tests for TOML](https://github.com/toml-lang/compliance)
  106. repository
  107. - thoroughly tested: 100% branch coverage
  108. ### Is comment preserving round-trip parsing supported?<a name="is-comment-preserving-round-trip-parsing-supported"></a>
  109. No.
  110. The `tomli.loads` function returns a plain `dict` that is populated with builtin types and types from the standard library only.
  111. Preserving comments requires a custom type to be returned so will not be supported,
  112. at least not by the `tomli.loads` and `tomli.load` functions.
  113. Look into [TOML Kit](https://github.com/sdispater/tomlkit) if preservation of style is what you need.
  114. ### Is there a `dumps`, `write` or `encode` function?<a name="is-there-a-dumps-write-or-encode-function"></a>
  115. [Tomli-W](https://github.com/hukkin/tomli-w) is the write-only counterpart of Tomli, providing `dump` and `dumps` functions.
  116. The core library does not include write capability, as most TOML use cases are read-only, and Tomli intends to be minimal.
  117. ### How do TOML types map into Python types?<a name="how-do-toml-types-map-into-python-types"></a>
  118. | TOML type | Python type | Details |
  119. | ---------------- | ------------------- | ------------------------------------------------------------ |
  120. | Document Root | `dict` | |
  121. | Key | `str` | |
  122. | String | `str` | |
  123. | Integer | `int` | |
  124. | Float | `float` | |
  125. | Boolean | `bool` | |
  126. | Offset Date-Time | `datetime.datetime` | `tzinfo` attribute set to an instance of `datetime.timezone` |
  127. | Local Date-Time | `datetime.datetime` | `tzinfo` attribute set to `None` |
  128. | Local Date | `datetime.date` | |
  129. | Local Time | `datetime.time` | |
  130. | Array | `list` | |
  131. | Table | `dict` | |
  132. | Inline Table | `dict` | |
  133. ## Performance<a name="performance"></a>
  134. The `benchmark/` folder in this repository contains a performance benchmark for comparing the various Python TOML parsers.
  135. The benchmark can be run with `tox -e benchmark-pypi`.
  136. Running the benchmark on my personal computer output the following:
  137. ```console
  138. foo@bar:~/dev/tomli$ tox -e benchmark-pypi
  139. benchmark-pypi installed: attrs==19.3.0,click==7.1.2,pytomlpp==1.0.2,qtoml==0.3.0,rtoml==0.7.0,toml==0.10.2,tomli==1.1.0,tomlkit==0.7.2
  140. benchmark-pypi run-test-pre: PYTHONHASHSEED='2658546909'
  141. benchmark-pypi run-test: commands[0] | python -c 'import datetime; print(datetime.date.today())'
  142. 2021-07-23
  143. benchmark-pypi run-test: commands[1] | python --version
  144. Python 3.8.10
  145. benchmark-pypi run-test: commands[2] | python benchmark/run.py
  146. Parsing data.toml 5000 times:
  147. ------------------------------------------------------
  148. parser | exec time | performance (more is better)
  149. -----------+------------+-----------------------------
  150. rtoml | 0.901 s | baseline (100%)
  151. pytomlpp | 1.08 s | 83.15%
  152. tomli | 3.89 s | 23.15%
  153. toml | 9.36 s | 9.63%
  154. qtoml | 11.5 s | 7.82%
  155. tomlkit | 56.8 s | 1.59%
  156. ```
  157. The parsers are ordered from fastest to slowest, using the fastest parser as baseline.
  158. Tomli performed the best out of all pure Python TOML parsers,
  159. losing only to pytomlpp (wraps C++) and rtoml (wraps Rust).