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 have higher priority)
eafig.load("config/default.yaml")
eafig.from_cli()

# Constructor args take highest priority
config = MyConfig(a=5)
sub = MySubConfig()

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

Config Loading Order

Later sources win:

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

Load from YAML

# config/default.yaml
a: 12
sub_config:
  x: from_file
  y: from_file

Override via CLI

python main.py --a 42 --sub_config.x cli_value

Override via constructor

config = MyConfig(a=5)  # 5 wins over both file and CLI

Nested Configs

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

@configclass(name="training")
class TrainingConfig:
    lr: float = 1e-3
    batch_size: int = 32

@rootconfig
class Root:
    seed: int = 42

Corresponding YAML:

seed: 42
model:
  hidden_dim: 512
  num_layers: 6
training:
  lr: 5e-4
  batch_size: 64

CLI override with dot notation: --model.hidden_dim 1024 --training.lr 1e-3

Strict Mode

Enabled by default. Unknown keys in the loaded config raise KeyError at instantiation time.

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

If a YAML file contains typo_key: oops, then MyConfig() raises:

KeyError: Unknown configuration key(s) in root config: ['typo_key']. Known keys: ['a'].

Set strict=False to allow extra keys:

@rootconfig(strict=False)
class MyConfig:
    a: int = 1

Each config class controls its own strict mode independently.

Frozen Configs

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

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

API

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 config from YAML file
eafig.from_cli(args=None) Parse CLI arguments (default: sys.argv[1:])
eafig.save(path) Save current config to YAML

Examples

See the examples/ directory.

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.1.0.tar.gz (17.9 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.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for eafig-1.1.0.tar.gz
Algorithm Hash digest
SHA256 62d4630891b796ab3d975bd167df21cc0812bb93271ec2868bebabe2c6d264bd
MD5 f469bb713e30371a3cbeea7bd8f115fb
BLAKE2b-256 2e778e60ebe2b979d22e7dfe9454223321c0e9c0e030411c7c273330ac8f468f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: eafig-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8dcd4844f99a35b566bcfb610915d627c963b91aa0c8ba59570ce7af31d1dcd
MD5 922b7c8948bfd450b021f0d2c1c5a17b
BLAKE2b-256 dbf58f4b7eee553ce9a1ebc413d5ecb544e6b58f8dc2dacd36f2ae4efdfbd8e6

See more details on using hashes here.

Provenance

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