Takes a YAML config file and builds a parser for command line arguments around it. This allows you to easily override default settings by passing command line arguments to your program. Supports nested arguments and auto-enforces parameter types.
input_dir: data
logging:
file: output.log
level: 4
import yaml
import quickargs
with open("config.yaml") as f:
config = yaml.load(f, Loader=quickargs.YAMLArgsLoader)
usage: main.py [-h] [--input_dir INPUT_DIR] [--logging.file LOGGING.FILE]
[--logging.level LOGGING.LEVEL]
optional arguments:
-h, --help show this help message and exit
--input_dir INPUT_DIR
default: data
--logging.file LOGGING.FILE
default: output.log
--logging.level LOGGING.LEVEL
default: 4
python main.py --logging.file=other_log.txt
# exact same output format as normal yaml.load would produce
{'input_dir': 'data', 'logging': {'file': 'other_log.txt', 'level': 4}}
usage: main.py [-h] [--input_dir INPUT_DIR] [--logging.file LOGGING.FILE]
[--logging.level LOGGING.LEVEL]
main.py: error: argument --logging.level: invalid int value: 'WARNING'
{'input_dir': 'data', 'logging': {'file': 'output.log', 'level': 0}}
pip install quickargs
import yaml
import quickargs
with open("config.yaml") as f:
config = yaml.load(f, Loader=quickargs.YAMLArgsLoader)
key1:
key2:
key3:
key4: value
{'key1': {'key2': {'key3': {'key4': 'other_value'}}}}
{'key1': {'key2': {'key3': {'key4': 'value'}}}}
thresholds: [0.2, 0.4, 0.6, 0.8, 1.0]
(take care to use ' ' around your command line arguments if they include spaces)
{'thresholds': [0.0, 0.5, 1.0]}
thresholds: [0.2, 0.4, 0.6, 0.8, 1.0]
List of strings instead of list of floats does not give an error: python main.py --thresholds=[a,b,c]
{'thresholds': ['a', 'b', 'c']}
function_to_call: !!python/name:yaml.dump
{'function_to_call': <built-in function zip>}
an_int: 3
a_float: 3.0
a_bool: True
a_complex_number: 37-880j
a_date: 2016-12-11
sequences:
a_list: [a, b, c]
# for tuples you need to use square [] brackeds in the yaml and on the command line
# they will still be proper tuples in the result
a_tuple: !!python/tuple [a, b]
python:
a_function: !!python/name:yaml.load
a_class: !!python/name:yaml.loader.Loader
a_module: !!python/module:contextlib
# can be overwritten with any type
a_none: !!python/none
python main.py --an_int=4 --a_float=2.0 --a_bool=False --a_complex_number=42-111j --a_date=2017-01-01 \
--sequences.a_list=[c,b,c] --sequences.a_tuple=[b,a] --python.a_function=zip \
--python.a_class=yaml.parser.Parser --python.a_module=yaml --python.a_none=1234
{'a_bool': False,
'a_complex_number': '42-111j',
'a_date': datetime.date(2017, 1, 1),
'a_float': 2.0,
'an_int': 4,
'python': {'a_class': <class 'yaml.parser.Parser'>,
'a_function': <built-in function zip>,
'a_module': <module 'yaml' from ...>,
'a_none': None},
'sequences': {'a_list': ['c', 'b', 'c'], 'a_tuple': ('b', 'a')}}
Following types are not supported at all:
- !!python/dict (because it looks just like the rest of the yaml file)
- !!pairs
Following types are not enforced / objects will not be instantiated:
- !!python/object
- !!python/object/new
- !!python/object/apply
If the YAML file contains multiple documents, only the first document will be considered. The yaml.load_all
functionality is not supported.