Getting Started¶
Installation¶
The PyPI distribution name is molcrafts-molcfg; the import name is molcfg.
For local development:
First Config¶
from molcfg import Config
cfg = Config({"app": {"name": "demo", "debug": False}})
# attribute access
assert cfg.app.name == "demo"
# dotted-path access
assert cfg["app.name"] == "demo"
# containment check
assert "app.debug" in cfg
Mutating a Config¶
Nested dicts are automatically wrapped into child Config objects.
Freezing and rollback¶
cfg.freeze()
# cfg.app.name = "x" # raises FrozenConfigError
cfg.unfreeze()
cfg.snapshot()
cfg.app.name = "temporary"
cfg.rollback()
assert cfg.app.name == "updated"
Layered loading¶
Later sources win using DEEP_MERGE by default:
from molcfg import CliSource, ConfigLoader, DictSource, EnvSource
cfg = ConfigLoader([
DictSource({"db": {"host": "localhost", "port": 5432}}, name="defaults"),
EnvSource(prefix="APP", name="env"),
CliSource(["--db.port=6432"], name="cli"),
]).load()
assert cfg["db.port"] == 6432
Source tracking¶
source is the last writer. history lists every source that touched the key, in order.