Easy dict-based script configuration with CLI support
Project description
Write simple configs and update from CLI, kwargs, and/or json.
The scriptconfig provides a simple way to make configurable scripts using a combination of config files, command line arguments, and simple Python keyword arguments. A script config object is defined by creating a subclass of Config with a default dict class attribute. An instance of a custom Config object will behave similar a dictionary, but with a few conveniences.
To get started lets consider some example usage:
>>> import scriptconfig as scfg
>>> # In its simplest incarnation, the config class specifies default values.
>>> # For each configuration parameter.
>>> class ExampleConfig(scfg.Config):
>>> default = {
>>> 'num': 1,
>>> 'mode': 'bar',
>>> 'ignore': ['baz', 'biz'],
>>> }
>>> # Creating an instance, starts using the defaults
>>> config = ExampleConfig()
>>> # Typically you will want to update default from a dict or file. By
>>> # specifying cmdline=True you denote that it is ok for the contents of
>>> # `sys.argv` to override config values. Here we pass a dict to `load`.
>>> kwargs = {'num': 2}
>>> config.load(kwargs, cmdline=False)
>>> assert config['num'] == 2
>>> # The `load` method can also be passed a json/yaml file/path.
>>> config_fpath = '/tmp/foo'
>>> open(config_fpath, 'w').write('{"num": 3}')
>>> config.load(config_fpath, cmdline=False)
>>> assert config['num'] == 3
>>> # It is possbile to load only from CLI by setting cmdline=True
>>> # or by setting it to a custom sys.argv
>>> config.load(cmdline=['--num=4'])
>>> assert config['num'] == 4
>>> # Note that using `config.load(cmdline=True)` will just use the
>>> # contents of sys.argv
Features
Serializes to json
Dict-like interface. By default a Config object operates independent of config files or the command line.
Can create command line interfaces
Can directly create an independent argparse object
Can use special command line loading using self.load(cmdline=True). This extends the basic argparse interface with:
Can specify options as either --option value or --option=value
Default config options allow for “smartcasting” values like lists and paths
Automatically add --config, --dumps, and --dump CLI options when reading cmdline via load.
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
File details
Details for the file scriptconfig-0.5.2.tar.gz
.
File metadata
- Download URL: scriptconfig-0.5.2.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d914b091d7f019517bf2d51d15aab22c4c777c4f7e9113a3c4b752ba7c63e95 |
|
MD5 | c0e3825dc4a7359ae96171c786ab74c1 |
|
BLAKE2b-256 | 1ad85348e4fb47953f47e34b47b5f640ce633cd51c0046ef2bcbd810b2605b90 |
File details
Details for the file scriptconfig-0.5.2-py2.py3-none-any.whl
.
File metadata
- Download URL: scriptconfig-0.5.2-py2.py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ab1c9d4a7aceaa91edec6a57c43f86d127506a4911af7c8b51c7ba664c92cba |
|
MD5 | 92738e1a112cd554eb856efc4d3e8ce9 |
|
BLAKE2b-256 | 7e86ce73c2c74bd08130c552daa5cd3e0e878d922ae54d709bd7456a271bcc24 |