Skip to main content

tomlparse

Release Build status example workflow codecov License code style

tomlparse is a Python library and command-line tool that allows you to use TOML configuration files in conjunction with the argparse module. It provides a simple and convenient way to handle your python projects, leveraging the strengths of both TOML and argparse.

📝 Table of Contents

  1. Installation
  2. Usage
    1. Basic Example
    2. Extended Example
  3. Contributing

💻 How to install

You can install the library using pip

pip install tomlparse

🔍 How to use

Using tomlparse is straightforward and requires only a few extra steps compared to using argparse alone.

🐍 Basic Example

You first define your configuration options in a TOML file. TOML files are highly flexible and include a lot of native types. Have look here for an extensive list. TOML files usually come in the following form:

# This is a very basic TOML file
foo = 10
bar = "hello"

At the core of this module is the TOML ArgumentParser, a simple wrapper of the original argparse module. To use the TOML arguments for our project, we we would create an ArgumentParser as usual:

import tomlparse
parser = tomlparse.ArgumentParser()
parser.add_argument("--foo", type=int, default=0)
parser.add_argument("--bar", type=str, default="")
parser.parse_args()

This is just a simple example with two arguments. But for larger projects with many hyperparameters, the number of arguments can quickly grow, and the TOML file provides an easy way to collect and store different hyperparameter configurations. Every TOML ArgumentParser has a toml argument defined that we can pass using the following command-line syntax:

python experiment.py --toml "example.toml"

This will replace the default values from the ArgumentParser with the TOML values.

🐍 Extended Example

TOML files have the ability to separate arguments into different sections (called tables), which are represented by nested dictionaries:

# This is a TOML File

# Parameters without a prededing [] are not part of a table (called root-table)
foo = 10
bar = "hello"

# These arguments are part of the table [general]
[general]
foo = 20

# These arguments are part of the table [root]
[root]
bar = "hey"

If we would load this TOML file as usual this would return a dict {"foo": 10, "bar": "hello", "general": {"foo": 20}, "root" : {"bar": "hey"}}. Note that foo and bar are overloaded and defined twice. To specify the values we wish to take each TOML ArgumentParser has two arguments defined:

  1. table
  2. root-table

We can use these directly from the command-line:

python experiment.py --toml "example.toml" --table "general"

Here the root-table is not defined. In this case the arguments at the top of the file without a table are taken and parsing would return the following dict {"foo": 20, "bar": "hello"}. Note that table arguments override arguments from the root-table.

We can also specify the root-table:

python experiment.py --toml "example.toml" --table "general" --root-table "root"

which would return the following dict {"foo: 20", "bar": "hey"} and override the arguments from the top of the TOML file.

In general, we have the following hierarchy of arguments:

  1. Arguments passed through the command line are selected over TOML arguments, even if both are passed
  2. Arguments from the TOML file are preferred over the default arguments
  3. Arguments from the TOML with a section override the arguments without a section

This means that we can also override arguments in the TOML file from the command-line:

python experiment.py --toml "example.toml" --table "general" --foo 100

🌸 Contributing

Please have a look at the contribution guidlines in Contributing.rst.


Repository initiated with fpgmaas/cookiecutter-poetry.

Metadata

Release files for tomlparse 1.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tomlparse 1.1.3
File Size Uploaded Provenance
tomlparse-1.1.3.tar.gz 6.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tomlparse 1.1.3
File Interpreter ABI Platform
tomlparse-1.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / tomlparse-1.1.3.tar.gz

Download URL tomlparse-1.1.3.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a857555634524b5ae11777066a181d4610caddbc734e032294963e3c2ce2f222
BLAKE2b-256 checksum
How to use checksums
60a8fbb1918c7bfae12b1036e8158a63c9546790a5fc420d9acab8905f060d53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.13 Linux/6.11.0-1018-azure

Release files / tomlparse-1.1.3-py3-none-any.whl

Download URL tomlparse-1.1.3-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51691c28963f1f4ae6980f7ff6b46a5d9e7204d384d986ad0c108167518e3e52
BLAKE2b-256 checksum
How to use checksums
c4f98a272d6c0a00907ec1391e14a2128cd38b3564fa229cac1b75a36adcc37c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.13 Linux/6.11.0-1018-azure

Security for tomlparse 1.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page