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 groupseafig.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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file eafig-1.2.7.tar.gz.
File metadata
- Download URL: eafig-1.2.7.tar.gz
- Upload date:
- Size: 29.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e2def7a0e83b24ef0a10c60aa91ccbe63522ba413072301cc4cf1a5dc1e524d
|
|
| MD5 |
95b5533dfbc44126eee45d62bf431bbf
|
|
| BLAKE2b-256 |
bdb880d424ffefd085f4c77e9d109834066723704cfcb6e7f10edd1939630b5a
|
Provenance
The following attestation bundles were made for eafig-1.2.7.tar.gz:
Publisher:
publish.yml on MugeTong/eafig
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
eafig-1.2.7.tar.gz -
Subject digest:
5e2def7a0e83b24ef0a10c60aa91ccbe63522ba413072301cc4cf1a5dc1e524d - Sigstore transparency entry: 2036649480
- Sigstore integration time:
-
Permalink:
MugeTong/eafig@ea7762e4958362c1399b9211dec5946e00801ccc -
Branch / Tag:
refs/tags/v1.2.7 - Owner: https://github.com/MugeTong
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ea7762e4958362c1399b9211dec5946e00801ccc -
Trigger Event:
release
-
Statement type:
File details
Details for the file eafig-1.2.7-py3-none-any.whl.
File metadata
- Download URL: eafig-1.2.7-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3279ae19ca995349990bc58c29a09b0d3dc149b371d66b219f4f8fd9a86d988
|
|
| MD5 |
f4f282d40f4b85512dfc32aca67b6c8c
|
|
| BLAKE2b-256 |
772cea49faf6dcb6bd1350b3fd239d0917be8ae52a85baef8ba136ad84e8b385
|
Provenance
The following attestation bundles were made for eafig-1.2.7-py3-none-any.whl:
Publisher:
publish.yml on MugeTong/eafig
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
eafig-1.2.7-py3-none-any.whl -
Subject digest:
d3279ae19ca995349990bc58c29a09b0d3dc149b371d66b219f4f8fd9a86d988 - Sigstore transparency entry: 2036649576
- Sigstore integration time:
-
Permalink:
MugeTong/eafig@ea7762e4958362c1399b9211dec5946e00801ccc -
Branch / Tag:
refs/tags/v1.2.7 - Owner: https://github.com/MugeTong
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ea7762e4958362c1399b9211dec5946e00801ccc -
Trigger Event:
release
-
Statement type: