Skip to main content

Simple config supporting CLI modification

Project description

codecov PyPI - Python Version PyPI

sconf: Simple config system supporting CLI modification

sconf is yaml-based simple config library.

Features

  • Supports merging multiple configs
  • Supports CLI modification
  • Supports coloring modified key-values

Install

$ pip install sconf

Usage

Initialize

Basic init

from sconf import Config

cfg = Config(default="configs/defaults.yaml")
cfg.argv_update()  # apply CLI modification

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)

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 object method for the duplicated key, unlike the item access.
cfg = Config({'get': 2})

print(cfg['get'])  # 2
print(cfg.get)  # method

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Built Distribution

sconf-0.2.1-py3-none-any.whl (7.7 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page