Skip to main content

Lightweight self-documenting configuration classes via Python descriptors.

Project description

cfx

cfg

Declare configuration fields next to the classes that use them. Each field carries its own default value, type checking, and documentation.

from cfg import Config, Float, Int, Options, Bool

class RunConfig(Config):
    """Configuration for a data-processing run."""
    confid = "run"
    iterations = Int(100, "Number of processing iterations", minval=1)
    threshold = Float(0.5, "Acceptance threshold", minval=0.0, maxval=1.0)
    mode = Options(("fast", "balanced", "thorough"), "Processing mode")
    verbose = Bool(False, "Enable verbose logging")

cfg = RunConfig()
print(cfg)
RunConfig:
Configuration for a data-processing run.
Key        | Value | Description
-----------+-------+----------------------------------
iterations | 100   | Number of processing iterations
threshold  | 0.5   | Acceptance threshold
mode       | fast  | Processing mode
verbose    | False | Enable verbose logging

What you get

  • Validated fields — typos and bad values raise immediately at the point of assignment, not silently hours later.
  • Self-documentingprint(cfg) and Jupyter repr show a table of every field with its current value and description.
  • Composable — assemble configs from multiple subsystem configs, either flat (all fields in one namespace) or nested (sub-objects by name).
  • Serializable — round-trip to/from dict, YAML, and TOML with one method call.
  • Extensible — subclass ConfigField to add your own field types with custom validation and normalization.
  • Zero hard dependencies — YAML and TOML support are optional soft dependencies.

Installation

pip install cfg

With optional serialization backends:

pip install "cfg[yaml]"   # adds PyYAML
pip install "cfg[toml]"   # adds tomli-w
pip install "cfg[all]"    # both

Quick start

from cfg import Config, Int, Float, String, Options, Bool, Path

class ProcessingConfig(Config):
    confid = "processing"
    iterations = Int(100, "Number of iterations", minval=1)
    threshold = Float(0.5, "Acceptance threshold", minval=0.0, maxval=1.0)
    label = String("run_01", "Human-readable run label")
    mode = Options(("fast", "balanced", "thorough"), "Processing mode")
    verbose = Bool(False, "Enable verbose logging")

cfg = ProcessingConfig()

# Dot access and dict-style access are interchangeable
cfg.iterations = 200
cfg["mode"] = "thorough"

# Bad values raise immediately
cfg.threshold = 1.5   # ValueError: Expected value <= 1.0, got 1.5

# Serialize to dict, YAML, or TOML
d = cfg.to_dict()
cfg2 = ProcessingConfig.from_dict(d)

# Copy with overrides, diff two instances
modified = cfg.copy(iterations=500)
cfg.diff(modified)   # {'iterations': (200, 500)}

Composition

Combine configs into nested sub-objects or a flat merged namespace:

from cfg import Config, Int, String

class FormatConfig(Config):
    confid = "format"
    precision = Int(6, "Decimal places in numeric output")
    encoding = String("utf-8", "Output file encoding")

class PipelineConfig(Config, components=[ProcessingConfig, FormatConfig]):
    pass

cfg = PipelineConfig()
cfg.processing.iterations = 500
cfg.format.precision = 3

License

GPL-3.0-only — see LICENSE.

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

cfx-0.1.0.post1.tar.gz (256.7 kB view details)

Uploaded Source

Built Distribution

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

cfx-0.1.0.post1-py3-none-any.whl (32.4 kB view details)

Uploaded Python 3

File details

Details for the file cfx-0.1.0.post1.tar.gz.

File metadata

  • Download URL: cfx-0.1.0.post1.tar.gz
  • Upload date:
  • Size: 256.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for cfx-0.1.0.post1.tar.gz
Algorithm Hash digest
SHA256 4e5bb156d879445163e5831bc588f009575d1424ebaa2e93a5951c1f89a92256
MD5 1c5e0365aca610a7e8476ac9eec997ee
BLAKE2b-256 a9dff73f29efa16bdb78ca3b4d97a3b0cd194e1ff753f576a889c8124a328edb

See more details on using hashes here.

File details

Details for the file cfx-0.1.0.post1-py3-none-any.whl.

File metadata

  • Download URL: cfx-0.1.0.post1-py3-none-any.whl
  • Upload date:
  • Size: 32.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for cfx-0.1.0.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 4b9437e247dcd7d984e4f8705a31e85f55e5b66e8e56187811eec0bfb189bec0
MD5 60594428b7bceb6bda38032cb04aae8e
BLAKE2b-256 25d443479f074ce0c24e44cc10f0a054704a2858e2451ef458de0906b5ea8a69

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