Simple config and argument system
Project description
hackerargs
hackerargs minimizes new lines of code to add a configurable argument anywhere in your codebase, so you can focus on coding.
# in any python file in your codebase
from hackerargs import args
class Model:
def __init__(self):
self.parameter = args.setdefault(key, default_value)
# run forth and code with confidence!
# value can be taken from CLI or yaml config if provided
- Hackerargs is a global, write-once-only dict. The first setting of each key is final, ensuring exact reproducibility when saving to yaml, and later loading it to rerun your script.
- We emphasize
val = setdefault(key, default_val)
as a primitive, which returns your key's value if it already exists, and setting it to default_val otherwise. No more fumbling with errors accessing missing keys. Initializing args from CLI or yaml config takes priority over runtime setdefault.
Features
- Values have python types inferred as floats, ints, strings, lists, etc. with PyYAML loader. YAML v1.1 except "on/off/yes/no" are not parsed as booleans
- Optional integration with argparse
- Works with wandb sweeps: log config using
wandb.config.update(args)
, and run sweep experiments in CLI--{key} {val}
format.
Installation
Hackerargs is a pip package and conda package:
pip install hackerargs
or
conda install mxwsn::hackerargs
conda install -c mxwsn hackerargs
Initialization
Initialize hackerargs in your driver script, just like argparse:
from hackerargs import args
if __name__ == '__main__':
args.parse_args()
main()
args.save_to_yaml(yaml_file)
parse_args can be called by itself, or with a YAML config file, or argparse.ArgumentParser:
args.parse_args()
args.parse_args('config.yaml')
args.parse_args(argparse.ArgumentParser())
args.parse_args(argparse.ArgumentParser(), 'config.yaml')
: The input order doesn't matter.
parse_args parses sys.argv
by default, but you can use a custom argv instead:
args.parse_args(..., argv = ['--string', 'text', '--int', '42'])
Priority
- (Highest priority) ArgumentParser options specified by user
- Unknown CLI options (not recognized by ArgumentParser) specified by user. These are parsed in
--{key} {val}
format. If no argparser is given, then all CLI options are parsed this way. - YAML config. If
--config {yaml_file}
CLI option is given, it is used instead of a yaml file given as input to parse_args() in python. As such,--config
is a protected CLI option when using hackerargs. - (Lowest priority) ArgumentParser default values for options not specified by user.
As a write-once-only dict, initialized values take priority over runtime values.
Access
Running python train.py --string text --int 42 --float 3.14 --list [1,2]
, we have:
>>> args
{'string': 'text', 'int': 42, 'float': 3.14, 'list': [1, 2]}
>>> args.setdefault('float', 1e-4)
3.14
>>> args.get('float')
3.14
>>> args['float']
3.14
>>> args.float
3.14
When a key might not be set, we recommend using setdefault for access. When you're sure the key is set already, hackerargs supports .get, bracket access, and dot access.
Compatibility with argparse
Build your own ArgumentParser, and pass it to hackerarg:
import argparse
from hackerargs import args
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--example', description = 'Example argument')
parser.add_argument('--flag', action = 'store_true')
# ...
args.parse_args(parser) # Uses parser on CLI args
hackerargs will first use your ArgumentParser to parse known arguments,
then parse unknown CLI arguments in --{key} {val}
format.
Running python example.py --example text --unknown 1
, we get:
args = {'example': 'text', 'flag': False, 'unknown': 1}
Supporting argparse lets you create help messages for your scripts easily.
Project details
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
File details
Details for the file hackerargs-1.1.0.tar.gz
.
File metadata
- Download URL: hackerargs-1.1.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b98e45e4186ccbafe1aaf46cc88c79ef4198ec70edc907057f53f92783dac376 |
|
MD5 | d0ca8ea2b22a0b423a4a352027256d11 |
|
BLAKE2b-256 | 25feb326d059d6059b9ab0be2605d4b8c38510853a6c181ae6319da8aa5e2524 |
File details
Details for the file hackerargs-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: hackerargs-1.1.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5654687e207e1299d7541815bd1d75f2c09701942a0d4a8275fe04e718b1014 |
|
MD5 | 59b65b762dbc4d77e988cd0d23e58e2b |
|
BLAKE2b-256 | db24fc481408abd73cc9fe9db553147f3248841f275af379aeb3e1f33493ea5e |