Configuration file parsing with argparse.
Project description
Parse configuration files with argparse.
Supported file types
| type | reader | third-party | note | |
| ✅ | cfg/ini |
configparser |
no | Supports custom ConfigParser reader |
| ✅ | json |
json |
no | |
| ✅ | toml |
tomli/tomllib |
yes/no | tomllib is built-in from Python 3.11 and was based on tomli |
| ✅ | yaml |
pyyaml |
yes |
Installation
pip install cargparse
Basic usage
Given config.yaml:
text: hello world
number: 42
Use argparse as you normally would for command line arguments!
import argparse
import cargparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument('--text', type=str, required=True)
parser.add_argument('--number', type=int, required=True)
parser.add_argument('--decimal', type=float)
config = cargparse.Cargparse(parser).parse_file(sys.argv[1])
python test.py config.yaml
>> config
{'text': 'hello world', 'number': 42)
>> config.text
'hello world'
>> type(config.number)
<class 'int'>
⚠️ Read the documentation for more information about type validation.
Advanced usage
You are not restricted to a flat hierarchy.
model:
lstm:
input_size: 100
hidden_size:
- 128
- 64
summary: True
Define a helper function to parse each nested section args, which is interpreted as a dictionary str.
from __future__ import annotations
def parse_config(filename: Path | str) -> cargparse.Namespace:
def model_namespace(args: str) -> cargparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('--cnn', type=cnn_namespace)
parser.add_argument('--lstm', type=lstm_namespace)
parser.add_argument('--summary', type=cargparse.boolean)
return cargparse.Cargparse(parser).parse_dict(args)
def cnn_namespace(args: str) -> cargparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('--in_channels', type=int, required=True)
parser.add_argument('--out_channels', type=int, required=True)
parser.add_argument('--kernel_width', type=int, required=True)
return cargparse.Cargparse(parser).parse_dict(args)
def lstm_namespace(args: str) -> cargparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('--input_size', type=int, required=True)
parser.add_argument('--hidden_size', type=cargparse.list_int, required=True)
return cargparse.Cargparse(parser).parse_dict(args)
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=model_namespace, required=True)
return cargparse.Cargparse(parser).parse_file(filename)
if __name__ == '__main__':
config = parse_config(filename=sys.argv[1])
>> config.model.cnn
>> config.model.lstm.hidden_units
*** AttributeError: hidden_units not in namespace: ['hidden_size', 'input_size']
>> config.model.lstm.hidden_size
[128, 64]
⚠️ Read the documentation for more information about type validation.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cargparse-0.0.9.tar.gz.
File metadata
- Download URL: cargparse-0.0.9.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcd15fe58e8f094ac20d7194270cca35f3cc06975a486385947f51734b353216
|
|
| MD5 |
0706ba0675c4397d375334e396e1b90c
|
|
| BLAKE2b-256 |
482be9a674034d5b79cf3f12c70257eb80942e8f8a648abb8e4db732e5c164ca
|
File details
Details for the file cargparse-0.0.9-py3-none-any.whl.
File metadata
- Download URL: cargparse-0.0.9-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9dd4d0b62fb892399cfb7a8e906c425f853d9c17176aa5a493f5b72e3ec300d8
|
|
| MD5 |
5dd4792caefb0b239ccdc40235ca41d0
|
|
| BLAKE2b-256 |
6ced295e414bdfc3c61a149add6acbb80e2ec1e7a2cde450e0fb49445aac039e
|