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.1.tar.gz (18.1 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.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: eafig-1.1.1.tar.gz
  • Upload date:
  • Size: 18.1 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.1.tar.gz
Algorithm Hash digest
SHA256 0b328afd7577e74a0be01c740d194f84e9d6924dd9bf91bc537dcaa5af664b39
MD5 e8221a0cf4d9eb979d71630e0c38b029
BLAKE2b-256 8859484faeb37b7cd74f6e6c6241dd825de0b37e8765d59e908d8e2d139b34f3

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: eafig-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c0dc1981ba88783aa830a845bbd964cc572d11b1c3189a9c20796bf11c3b492a
MD5 a5aa8ec09006b9582976e5d0d5357a17
BLAKE2b-256 e9ddb6f76ac0e3b115333c0195f572dc98f452b78d98a3e219e161ef1e1087d4

See more details on using hashes here.

Provenance

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