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))
dump(cfg, "runs/finetune_config.py")

Assignments create intermediate dicts and extend lists with None as needed. List indices follow Python semantics in overrides: negative indices are allowed when the list exists and are in range (otherwise IndexError). Deletes and list removals are forgiving no-ops when the path is missing or out of range.

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.2.0.tar.gz (5.5 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.2.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cfgx-0.2.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","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.2.0.tar.gz
Algorithm Hash digest
SHA256 4165504b0c7a953972f5ee64e755b6a88a681628e85d7d14b065a3c9ed13b5a8
MD5 27f6c4915d8216a9d56032d9796e232f
BLAKE2b-256 9bd8d62c2e730e8cfd2231657eb359b8ff662d51bfff1c7996ec89d65e3f821b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cfgx-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 075f54faf20ae38517ba646ce7703d2a0b2f9dbc1b4514b5abdccff3bf78c82b
MD5 f0ed4427aff3ceb247cfc1df27add6c2
BLAKE2b-256 39a5f1ab451dfd5202c9904348ec9f54784c5c619333b3b81c056b2caab2acf7

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