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://karimknaebel.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.5.0.tar.gz (6.7 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.5.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cfgx-0.5.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","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.5.0.tar.gz
Algorithm Hash digest
SHA256 cba44a96a65690114bd42cd43b628c32f4ca14e85b2167cef4de554b23d65aa3
MD5 0b8131a824ec0a40a23a925f88b9d5c4
BLAKE2b-256 c5d42e5164203c7a977f0f47022678d0d56587b75a5c962da88aa4155322e96f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cfgx-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","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.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6375ea7fe679a3b695345882dff4c1348ce39e70d0c54a9b054b6ebb75c5322
MD5 51fc06226f77925e4f3d2e4f10ea1c7d
BLAKE2b-256 fd21cfec93e8d36dfddaa68c7e6105d7b3ee791a688f4ebb0027fe2dd28817f8

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