Simple config supporting CLI modification
Project description
sconf: Simple config system supporting CLI modification
sconf is yaml-based simple config library.
Features
- Supports merging multiple configs
- Supports CLI modification by argparse-like interface
- Supports coloring modified key-values
- Supports global access to config objects
Install
$ pip install sconf
Usage
Quickstart
A Minimal Example
from sconf import Config
cfg = Config(default="configs/defaults.yaml")
cfg.argv_update() # apply CLI modification
You can modify cfg by CLI in the runtime, by argparse-like interface.
Init with argparse and multiple configs
import argparse
from sconf import Config
parser = argparse.ArgumentParser()
parser.add_argument("name")
parser.add_argument("config_paths", nargs="*")
parser.add_argument("--show", action="store_true", default=False)
args, left_argv = parser.parse_known_args()
# merging multiple configs if given
cfg = Config(*args.config_paths, default="configs/defaults.yaml")
cfg.argv_update(left_argv)
Run:
python train.py example configs/exp.yaml --lr 0.1
The resulting cfg is based on configs/defaults.yaml, merged with configs/exp.yaml, and updated by --lr 0.1.
Dumps
sconf dumps contents with coloring modified items.
print(cfg.dumps())
# If you do not want to colorize:
print(cfg.dumps(modified_color=None))
Access
- Item access with dictionary-like interfaces:
# access
print(cfg['key'])
print(cfg['key1']['key2'])
# get
print(cfg.get('non-key', 'default-value'))
# unpacking
function(**cfg['model'])
- Attribute access:
print(cfg.key)
print(cfg.key1.key2)
- Note that the attribute access returns method object for the duplicated key, unlike the item access.
cfg = Config({'get': 2})
print(cfg['get']) # 2
print(cfg.get) # method object
CLI modification
sconf supports CLI modification like argparse. Also you can access to the child key using dot.
# yaml example
batch_size: 64
model:
encoder:
n_channels: 64
decoder:
n_channels: 64
- CLI modification:
> python train.py --batch_size 128 --model.encoder.n_channels 32
- Accessing via partial key is also available:
> python train.py --encoder.n_channels 32
- Use triple dashs
---if you want to modify multiple keys:
# modifying encoder.n_channels and decoder.n_channels both.
> python train.py ---n_channels 32
Global access to config object
Global access is useful in ML project, even though it can be anti-pattern in SW engineering.
# main.py
cfg = Config({'weight_decay': 0.001}) # first config is automatically registered to 'default' key
# train.py
cfg = Config.get_default() # get 'default' config
print(cfg.weight_decay) # 0.001
Note from_registry helps global access to multiple configs.
Note
- sconf use utf-8 as a default encoding. If you want different encoding, use file pointer (
openfunction) instead of file path as a key.
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 Distributions
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 sconf-0.2.5-py3-none-any.whl.
File metadata
- Download URL: sconf-0.2.5-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c557b5a87d39dd7d5334a1236112873d2e0b1b71b386d24e2f01726aec1d957a
|
|
| MD5 |
1483451ca5e6883ab01257f19cebc05f
|
|
| BLAKE2b-256 |
9125e668d70f3482f615b046e34ef31ec2801cd3d4c2acf6a655eab409cae40b
|