This is a template repository for Python projects that use Poetry for their dependency management.
Project description
toml-argparse
toml-argparse is a python library and command-line-tool that allows you to use TOML configuration files with the argparse module. It provides a simple and convenient way to handle configuration for your python scripts, leveraging the strengths of both TOML and argparse.
Installation
You can install the library using pip
pip install toml-argparse
Usage
Using toml-argparse is straightforward and requires only a few extra steps compared to using argparse alone. You first define your configuration options in a TOML file, then use the TOML ArgumentParser.
Basic Example
TOML files usually come in the following form:
# This is a very basic TOML file without a section
foo = 10
bar = "hello"
The TOML ArgumentParser is a simple wrapper of the original argparse module. It therefore provides the exact same fumctionality. To use the TOML arguments for our project, we we would create an ArgumentParser
as usual:
from toml_argparse import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--foo", type-int, default=0)
parser.add_argumetn("--bar", type=str, default="")
parser.parse_args()
This is just a very simple example with two arguments. However, for large projects with a lot of hyperparameters the number of arguments usually increases quickly and the TOML file provides an easy way to collect and store different hyperparameter configurations. We can do this by parsing parameters from the TOML file from the command-line:
python experiment.py --config "example.toml"
Extended Example
TOML files have the power to separate arguments into different sections that are represented by nested dictionaries:
# This is a TOML File
# These are parameters not part of a section
foo = 10
bar = "hello"
[general]
foo = 20
If we would load this TOML file as usual this would return a dict {"foo": 10, "bar": "hello", "general": {"foo": 20}. Note that foo is overloaded and defined twice. We can also load arguments from a specific section through the corresponding keyword section
:
python experiment.py --config "example.toml" --section "general"
This would return the following dict {"foo": 20, "bar": "hello"}. Note that section arguments override arguments without a section.
In general, we have the following hierarchy of arguments:
- Arguments passed through the command line are selected over TOML arguments, even if both are passed
- Arguments from the TOML file are preferred over the default arguments
- 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 --config "example.toml" --section "general" --foo 100
Contributing
Please have a look at the contribution guidlines in Contributing.rst
.
Repository initiated with fpgmaas/cookiecutter-poetry.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for toml_argparse-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f15341e91dce431b448b14a9158e88e0e146d16985a86b1faa65814d6658fb5e |
|
MD5 | 9b9d781013c29a3f25ea01804fbeeace |
|
BLAKE2b-256 | c6c4905a50d590ce10c57236b059b0595152d56ecefceb5358786905cdc290e2 |