mercurial/thirdparty/tomli/README.md
author Raphaël Gomès <rgomes@octobus.net>
Mon, 23 Jan 2023 17:11:42 +0100
changeset 50761 2c34c9b61a4f
permissions -rw-r--r--
thirdparty: vendor tomli The next commit will introduce a .toml file to abstract configitems away from Python. Python 3.11 has a toml read-only library (`tomllib`), which gives us a way out of vendoring eventually. For now, we vendor the backport, specifically version 1.2.3 which is still compatible with Python 3.6.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50761
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
[![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)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
[![codecov.io](https://codecov.io/gh/hukkin/tomli/branch/master/graph/badge.svg)](https://codecov.io/gh/hukkin/tomli)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
[![PyPI version](https://img.shields.io/pypi/v/tomli)](https://pypi.org/project/tomli)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
# Tomli
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
> A lil' TOML parser
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     9
**Table of Contents**  *generated with [mdformat-toc](https://github.com/hukkin/mdformat-toc)*
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    10
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    11
<!-- mdformat-toc start --slug=github --maxlevel=6 --minlevel=2 -->
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    12
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    13
- [Intro](#intro)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    14
- [Installation](#installation)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    15
- [Usage](#usage)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    16
  - [Parse a TOML string](#parse-a-toml-string)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    17
  - [Parse a TOML file](#parse-a-toml-file)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    18
  - [Handle invalid TOML](#handle-invalid-toml)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    19
  - [Construct `decimal.Decimal`s from TOML floats](#construct-decimaldecimals-from-toml-floats)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    20
- [FAQ](#faq)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
  - [Why this parser?](#why-this-parser)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    22
  - [Is comment preserving round-trip parsing supported?](#is-comment-preserving-round-trip-parsing-supported)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    23
  - [Is there a `dumps`, `write` or `encode` function?](#is-there-a-dumps-write-or-encode-function)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    24
  - [How do TOML types map into Python types?](#how-do-toml-types-map-into-python-types)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
- [Performance](#performance)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    27
<!-- mdformat-toc end -->
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    28
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    29
## Intro<a name="intro"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    30
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    31
Tomli is a Python library for parsing [TOML](https://toml.io).
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    32
Tomli is fully compatible with [TOML v1.0.0](https://toml.io/en/v1.0.0).
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    33
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    34
## Installation<a name="installation"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    35
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    36
```bash
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    37
pip install tomli
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    38
```
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    39
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    40
## Usage<a name="usage"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    42
### Parse a TOML string<a name="parse-a-toml-string"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
```python
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    45
import tomli
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    47
toml_str = """
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    48
           gretzky = 99
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    49
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
           [kurri]
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
           jari = 17
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
           """
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
toml_dict = tomli.loads(toml_str)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
assert toml_dict == {"gretzky": 99, "kurri": {"jari": 17}}
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    56
```
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
### Parse a TOML file<a name="parse-a-toml-file"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    59
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
```python
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
import tomli
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    62
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    63
with open("path_to_file/conf.toml", "rb") as f:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    64
    toml_dict = tomli.load(f)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    65
```
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    66
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    67
The file must be opened in binary mode (with the `"rb"` flag).
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    68
Binary mode will enforce decoding the file as UTF-8 with universal newlines disabled,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    69
both of which are required to correctly parse TOML.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    70
Support for text file objects is deprecated for removal in the next major release.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    71
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    72
### Handle invalid TOML<a name="handle-invalid-toml"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    73
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
```python
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    75
import tomli
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    76
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    77
try:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    78
    toml_dict = tomli.loads("]] this is invalid TOML [[")
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    79
except tomli.TOMLDecodeError:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    80
    print("Yep, definitely not valid.")
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    81
```
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    82
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    83
Note that while the `TOMLDecodeError` type is public API, error messages of raised instances of it are not.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    84
Error messages should not be assumed to stay constant across Tomli versions.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    85
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    86
### Construct `decimal.Decimal`s from TOML floats<a name="construct-decimaldecimals-from-toml-floats"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    87
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    88
```python
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    89
from decimal import Decimal
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    90
import tomli
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    91
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    92
toml_dict = tomli.loads("precision-matters = 0.982492", parse_float=Decimal)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    93
assert toml_dict["precision-matters"] == Decimal("0.982492")
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    94
```
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    95
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    96
Note that `decimal.Decimal` can be replaced with another callable that converts a TOML float from string to a Python type.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    97
The `decimal.Decimal` is, however, a practical choice for use cases where float inaccuracies can not be tolerated.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    98
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    99
Illegal types include `dict`, `list`, and anything that has the `append` attribute.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   100
Parsing floats into an illegal type results in undefined behavior.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   101
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   102
## FAQ<a name="faq"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   103
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   104
### Why this parser?<a name="why-this-parser"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   105
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   106
- it's lil'
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   107
- pure Python with zero dependencies
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   108
- the fastest pure Python parser [\*](#performance):
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   109
  15x as fast as [tomlkit](https://pypi.org/project/tomlkit/),
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   110
  2.4x as fast as [toml](https://pypi.org/project/toml/)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   111
- outputs [basic data types](#how-do-toml-types-map-into-python-types) only
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   112
- 100% spec compliant: passes all tests in
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   113
  [a test set](https://github.com/toml-lang/compliance/pull/8)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   114
  soon to be merged to the official
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   115
  [compliance tests for TOML](https://github.com/toml-lang/compliance)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   116
  repository
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   117
- thoroughly tested: 100% branch coverage
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   118
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   119
### Is comment preserving round-trip parsing supported?<a name="is-comment-preserving-round-trip-parsing-supported"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   120
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   121
No.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   122
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   123
The `tomli.loads` function returns a plain `dict` that is populated with builtin types and types from the standard library only.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   124
Preserving comments requires a custom type to be returned so will not be supported,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   125
at least not by the `tomli.loads` and `tomli.load` functions.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   126
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   127
Look into [TOML Kit](https://github.com/sdispater/tomlkit) if preservation of style is what you need.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   128
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   129
### Is there a `dumps`, `write` or `encode` function?<a name="is-there-a-dumps-write-or-encode-function"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   130
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   131
[Tomli-W](https://github.com/hukkin/tomli-w) is the write-only counterpart of Tomli, providing `dump` and `dumps` functions.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   132
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   133
The core library does not include write capability, as most TOML use cases are read-only, and Tomli intends to be minimal.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   134
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   135
### How do TOML types map into Python types?<a name="how-do-toml-types-map-into-python-types"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   136
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   137
| TOML type        | Python type         | Details                                                      |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   138
| ---------------- | ------------------- | ------------------------------------------------------------ |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   139
| Document Root    | `dict`              |                                                              |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   140
| Key              | `str`               |                                                              |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   141
| String           | `str`               |                                                              |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   142
| Integer          | `int`               |                                                              |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   143
| Float            | `float`             |                                                              |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   144
| Boolean          | `bool`              |                                                              |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   145
| Offset Date-Time | `datetime.datetime` | `tzinfo` attribute set to an instance of `datetime.timezone` |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   146
| Local Date-Time  | `datetime.datetime` | `tzinfo` attribute set to `None`                             |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   147
| Local Date       | `datetime.date`     |                                                              |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   148
| Local Time       | `datetime.time`     |                                                              |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   149
| Array            | `list`              |                                                              |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   150
| Table            | `dict`              |                                                              |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   151
| Inline Table     | `dict`              |                                                              |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   152
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   153
## Performance<a name="performance"></a>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   154
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   155
The `benchmark/` folder in this repository contains a performance benchmark for comparing the various Python TOML parsers.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   156
The benchmark can be run with `tox -e benchmark-pypi`.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   157
Running the benchmark on my personal computer output the following:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   158
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   159
```console
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   160
foo@bar:~/dev/tomli$ tox -e benchmark-pypi
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   161
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
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   162
benchmark-pypi run-test-pre: PYTHONHASHSEED='2658546909'
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   163
benchmark-pypi run-test: commands[0] | python -c 'import datetime; print(datetime.date.today())'
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   164
2021-07-23
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   165
benchmark-pypi run-test: commands[1] | python --version
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   166
Python 3.8.10
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   167
benchmark-pypi run-test: commands[2] | python benchmark/run.py
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   168
Parsing data.toml 5000 times:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   169
------------------------------------------------------
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   170
    parser |  exec time | performance (more is better)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   171
-----------+------------+-----------------------------
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   172
     rtoml |    0.901 s | baseline (100%)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   173
  pytomlpp |     1.08 s | 83.15%
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   174
     tomli |     3.89 s | 23.15%
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   175
      toml |     9.36 s | 9.63%
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   176
     qtoml |     11.5 s | 7.82%
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   177
   tomlkit |     56.8 s | 1.59%
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   178
```
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   179
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   180
The parsers are ordered from fastest to slowest, using the fastest parser as baseline.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   181
Tomli performed the best out of all pure Python TOML parsers,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   182
losing only to pytomlpp (wraps C++) and rtoml (wraps Rust).