Skip to main content

Python-first config loader with composition and CLI overrides.

Project description

cfgx

PyPI version

Python-first config loader with parent chaining, lazy computed values, and CLI-style overrides.

Docs: https://kabouzeid.github.io/cfgx/

Install

pip install cfgx

Quick start

Example config file:

# configs/model.py
config = {
    "model": {"name": "resnet18"},
    "optimizer": {"lr": 3e-4},
}
from cfgx import load

cfg = load("configs/model.py", overrides=["optimizer.lr=1e-3"])

Works well with specbuild when you want to build your model and other classes from config dictionaries.

Advanced example

Base config:

# configs/base.py
from cfgx import Lazy

config = {
    "model": {"depth": 8, "width": 512},
    "optimizer": {
        "lr": 3e-4,
        "weight_decay": 0.01,
        "schedule": {"type": "linear", "warmup_steps": 1_000},
    },
    "trainer": {
        "max_steps": 50_000,
        "hooks": ["progress", "checkpoint"],
        "stages": [{"name": "warmup", "max_steps": 5_000}],
        "log_every": Lazy("c.trainer.max_steps // 100"),
    },
}

Derived config:

# configs/finetune.py
from cfgx import Delete, Lazy, Replace

parents = ["base.py"]

config = {
    "model": {"depth": 12},
    "optimizer": {
        "weight_decay": Delete(),
        "schedule": Replace({"type": "cosine", "t_max": 40_000}),
    },
    "trainer": {"max_steps": 10_000},
    "scheduler": {
        "warmup_steps": 500,
        "decay_steps": Lazy(lambda c: c.trainer.max_steps - c.scheduler.warmup_steps),
    },
}

Load, override, and snapshot:

from cfgx import dump, format, load

cfg = load(
    "configs/finetune.py",
    overrides=[
        "optimizer.lr=1e-4",
        "trainer.hooks+=wandb",
        "trainer.hooks-='checkpoint'",
        "trainer.stages[0].max_steps=2_000",
        "scheduler.warmup_steps=lazy:c.trainer.max_steps * 0.1",
    ],
)

print(format(cfg))
with open("runs/finetune_config.py", "w") as f:
    dump(cfg, f, format="ruff")

Project details


Download files

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

Source Distribution

cfgx-0.4.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cfgx-0.4.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file cfgx-0.4.0.tar.gz.

File metadata

  • Download URL: cfgx-0.4.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for cfgx-0.4.0.tar.gz
Algorithm Hash digest
SHA256 71cfd5021b3f67cf103837f44c74dd0f5444a80fac67acb21974f32001260c1e
MD5 fcefb387fd44e3c8a3aa5c3ac4bcfd84
BLAKE2b-256 31664a6ff412d913ea7b259a696b99da9bd28788f8cc76d6e7d3e609a690336d

See more details on using hashes here.

File details

Details for the file cfgx-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: cfgx-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for cfgx-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b23f1f1b7ba64433dd53ea2d661b5f8197661aa99c7b075b30beb15e4a701e30
MD5 8c02c292701644cb4c5fb8d737d9109a
BLAKE2b-256 b50b1c1425cda45e473478e05fade026ac7bca238284b1000f3e01174cd2f7f0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page