Skip to main content

Lightweight self-documenting configuration classes via Python descriptors.

Project description

cfx

CI Docs PyPI

cfx

Declare configuration fields next to the classes that use them. Each field carries its own default value, type checking, and documentation. Compose flat and nested configs freely — the display shows a unified tree and table.

from cfx import Config, Float, String, Bool

class CalibConfig(Config):
    """Photometric calibration parameters."""
    confid = "calib"
    scale = Float(1.0, "Flux scale factor")
    zero_point = Float(25.0, "Photometric zero-point")

class SourceConfig(Config, components=[CalibConfig]):
    """Source detection and measurement."""
    confid = "source"
    n_sigma = Float(3.0, "Detection threshold in sigma")

class PipelineConfig(Config, components=[SourceConfig]):
    """Image analysis pipeline."""
    confid = "pipeline"
    run_id = String("run_01", "Run identifier")
    dry_run = Bool(False, "Validate only; skip writes")

cfg = PipelineConfig()
print(cfg)
PipelineConfig: Image analysis pipeline.
└─ SourceConfig: Source detection and measurement.
    └─ CalibConfig: Photometric calibration parameters.
Config         | Key        | Value  | Description
---------------+------------+--------+-----------------------------
PipelineConfig | run_id     | run_01 | Run identifier
PipelineConfig | dry_run    | False  | Validate only; skip writes
SourceConfig   | n_sigma    | 3.0    | Detection threshold in sigma
CalibConfig    | scale      | 1.0    | Flux scale factor
CalibConfig    | zero_point | 25.0   | Photometric zero-point

Installation

pip install cfx

With optional serialization and CLI backends:

pip install "cfx[yaml]"   # adds PyYAML
pip install "cfx[toml]"   # adds tomli-w
pip install "cfx[click]"  # adds Click
pip install "cfx[all]"    # everything

Quick start

from cfx 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)}

Environment variables

Back any field with an environment variable — the value is read lazily, so the same class works across environments without subclassing:

class ServiceConfig(Config):
    host = String("localhost", "Database host", env="DB_HOST")
    port = Int(5432, "Port", env="DB_PORT")

# DB_HOST=prod.example.com python run.py
cfg = ServiceConfig()
cfg.host   # 'prod.example.com' — from env, no code change needed

CLI

Every config exposes add_arguments / from_argparse for argparse and click_options / from_click for Click. Nested sub-configs use dot-notation flags automatically:

import argparse

parser = argparse.ArgumentParser()
ProcessingConfig.add_arguments(parser)
cfg = ProcessingConfig.from_argparse(parser.parse_args())
# python run.py --iterations 200 --mode thorough --no-verbose

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.2.1.tar.gz (278.9 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.2.1-py3-none-any.whl (37.8 kB view details)

Uploaded Python 3

File details

Details for the file cfx-0.2.1.tar.gz.

File metadata

  • Download URL: cfx-0.2.1.tar.gz
  • Upload date:
  • Size: 278.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cfx-0.2.1.tar.gz
Algorithm Hash digest
SHA256 da8d0010b49a79e8fb142145e1d9765b731802189f2b416b050bc12468ef069c
MD5 67aed1f200d7940c08fec75af359c81a
BLAKE2b-256 716e9be12d79c2b0a14149da85d93c754eab02a79beec1f773a6f8c80fe31cec

See more details on using hashes here.

Provenance

The following attestation bundles were made for cfx-0.2.1.tar.gz:

Publisher: publish.yml on DinoBektesevic/cfx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cfx-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: cfx-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 37.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cfx-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1ef905b0c82005ca7c3a3be983754eb9215ae7797f5eaece4c03f71eec12d65f
MD5 281e27f3ee77794a443de22015c4fdee
BLAKE2b-256 4211e46e34c79d5c7f7818f4ff98c25fd3b4f2d3beb4751e7523c20c9ec350f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for cfx-0.2.1-py3-none-any.whl:

Publisher: publish.yml on DinoBektesevic/cfx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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