Skip to main content

Manage your hyperparameters more easily.

Project description

Eafig

Manage your hyperparameters from the outside.

Installation

pip install eafig

Requires Python ≥ 3.12.

Quick Start

import eafig
from eafig import rootconfig, configclass


@rootconfig
class MyConfig:
    a: int
    c: float = 1.0


@configclass(name="sub_config")
class MySubConfig:
    x: str = "hello"
    y: str = "world"


# Load from file, then CLI — later calls win
eafig.load("config/default.yaml")
eafig.from_cli()

# Constructor args overridden by loaded values
config = MyConfig(a=5)
sub = MySubConfig()

# Save to file
eafig.save("config/saved_config.yaml")

Config Loading Order

defaults  <  constructor args  <  file (load)  <  CLI (from_cli)

Each layer overrides the one before it. Among load() / from_cli() calls, later calls win.

keep_cli

A later load() normally overrides CLI values. Pass keep_cli=True to lock CLI on top:

eafig.from_cli()
eafig.load("config.yaml", keep_cli=True)  # CLI stays above file

Nested Configs

@configclass supports dot-separated names for deep nesting:

@configclass(name="model")
class ModelConfig:
    hidden_dim: int = 256
    num_layers: int = 3


@configclass(name="model.optimizer")
class OptimizerConfig:
    lr: float = 1e-3


@configclass(name="training")
class TrainingConfig:
    batch_size: int = 32
    epochs: int = 100


@rootconfig
class Root:
    seed: int = 42

CLI override: --model.hidden_dim 1024 --model.optimizer.lr 1e-3

Strict Mode

Enabled by default. Unknown keys raise KeyError at load time:

@rootconfig(strict=True)   # default
class MyConfig:
    a: int = 1

If a YAML file contains typo_key: oops, loading raises:

KeyError: Unknown key 'typo_key' in configuration file 'config.yaml'.

Set strict=False to allow extra keys. Each config group controls its own strict mode independently.

Frozen Configs

Not recursive — each config group has its own frozen flag.

@rootconfig(frozen=True)
class MyConfig:
    a: int = 42

config = MyConfig()        # OK — uses defaults
config = MyConfig(a=5)     # TypeError: cannot override frozen config
config.a = 100             # FrozenInstanceError

Frozen also rejects values loaded from files or CLI.

Hidden Config Groups

@configclass(name="api", hidden=True)
class ApiConfig:
    secret_key: str = "..."
  • eafig.config, from_cli(), load() exclude hidden groups
  • eafig.save() includes them (full persistence)

Runtime set / get

eafig.set("model.hidden_dim", 1024)
value = eafig.get("model.hidden_dim")          # 1024
value = eafig.get("missing.key", default=0)    # 0

set() enforces schema: raises ValueError on config groups or frozen parents, KeyError on unknown keys in strict mode.

Dynamic eafig.config

import eafig
eafig.load("config.yaml")
print(eafig.config)  # full config dict (hidden groups excluded)

API Reference

API Description
@rootconfig(frozen=False, strict=True) Decorate a dataclass as root config
@configclass(*, name, frozen=False, hidden=False, strict=True) Decorate a dataclass as child config
eafig.load(path, keep_cli=False) Load YAML file or file-like object. Returns root dict
eafig.from_cli(args=None) Parse CLI args (default: sys.argv[1:]). Returns root dict
eafig.save(path, sort_keys=True) Save full config to YAML file or file-like object
eafig.set(key, value) Set a single value (dot-notation, schema-enforced)
eafig.get(key, default=None) Get a single value (dot-notation)
eafig.config Current full config dict (hidden excluded)

Examples

See examples/.

License

MIT

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

eafig-1.2.3.tar.gz (27.7 kB view details)

Uploaded Source

Built Distribution

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

eafig-1.2.3-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file eafig-1.2.3.tar.gz.

File metadata

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

File hashes

Hashes for eafig-1.2.3.tar.gz
Algorithm Hash digest
SHA256 a8fbceee100bf9d5db5189b99cdfb455324e1fa1aeb72b2890c3fe5037a7621b
MD5 686712d97172bea15095420b4db0ff1d
BLAKE2b-256 b030ba139009892ad228f220ff7ed8d37b2816e7fea5c4f0a17e41ad932d3827

See more details on using hashes here.

Provenance

The following attestation bundles were made for eafig-1.2.3.tar.gz:

Publisher: publish.yml on MugeTong/eafig

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

File details

Details for the file eafig-1.2.3-py3-none-any.whl.

File metadata

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

File hashes

Hashes for eafig-1.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 88edc7f375dcc76d1a1c40b51c97268103417f7eca5504aaee123023f9118f4b
MD5 0c3f7a1dd55bf3fa723947bd8bcf25fc
BLAKE2b-256 7dd817780fb22996fc9736fca9f982f0205a61638dd1bb33195ec88f89e1c084

See more details on using hashes here.

Provenance

The following attestation bundles were made for eafig-1.2.3-py3-none-any.whl:

Publisher: publish.yml on MugeTong/eafig

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