Home

Awesome

codecov PyPI - Python Version PyPI

sconf: Simple config system supporting CLI modification

sconf is yaml-based simple config library.

Features

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

# access
print(cfg['key'])
print(cfg['key1']['key2'])

# get
print(cfg.get('non-key', 'default-value'))

# unpacking
function(**cfg['model'])
print(cfg.key)
print(cfg.key1.key2)
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
> python train.py --batch_size 128 --model.encoder.n_channels 32
> python train.py --encoder.n_channels 32
# 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