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()

# Instantiate — values come from file/CLI or defaults
config = MyConfig()
sub = MySubConfig()

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

Config Loading Order

defaults  <  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(seed=999) # TypeError: does not accept constructor arguments
config.a = 100             # FrozenInstanceError

Frozen also rejects values loaded from files or CLI.

Default Values

Dataclass field defaults are automatically included in output even before instantiation — no need to construct every config class just to see defaults:

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

# model.hidden_dim and model.num_layers appear via defaults
print(eafig.config)  # {"model": {"hidden_dim": 256, "num_layers": 3}}

Explicitly set values always override defaults.

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.5.tar.gz (28.4 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.5-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: eafig-1.2.5.tar.gz
  • Upload date:
  • Size: 28.4 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.5.tar.gz
Algorithm Hash digest
SHA256 69526574345458fc8455cc24c59c5489828189db7562e62bd736c139a95eaa71
MD5 f094ed967dbd53868c5dbbb691805c4e
BLAKE2b-256 7a71f00ae4b3cbd226a134252b0097bdb2c766dc801b79bf853d6c94c11cb93a

See more details on using hashes here.

Provenance

The following attestation bundles were made for eafig-1.2.5.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.5-py3-none-any.whl.

File metadata

  • Download URL: eafig-1.2.5-py3-none-any.whl
  • Upload date:
  • Size: 10.5 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c984380a5267b221b79a770b683a175cabba06ce64e0bcd69f00fe56c168b7f4
MD5 d28aaa1b31601677eb9e407a81ea331c
BLAKE2b-256 46ab8448883a8e88a256b9b798a66b88cc6fb108878eb8e7fc067abc2af68c77

See more details on using hashes here.

Provenance

The following attestation bundles were made for eafig-1.2.5-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