No project description provided
Project description
Installation
pip install hydralette
# OR
poetry add hydralette
Changelog
- v0.1.5
- Add yaml overrides
- Add end-to-end tests with pytest
- v0.1.4
- Add documentation at https://valeknappich.github.io/hydralette
- v0.1.3
- Add automatic generation of configs from signatures via
from_signature
andconfig_from_signature
- Add automatic generation of configs from signatures via
- v0.1.2
- Add support for
config groups
,references
,validation
andtype conversion
- Add CLI help pages
- Add support for
Example
from dataclasses import dataclass
from pathlib import Path
import tempfile
from typing import Union
import yaml
from hydralette import ConfigBase, field
# lets assume this is defined in some library, i.e. you cant change the code
@dataclass
class LibraryConfig:
a: int = 1
b: float = 4.0
class LibraryConfigHydralette(LibraryConfig, ConfigBase): # <-- turn it into a hydralette config by subclassing
pass
def some_function(a: int, b: float = 4.0): # <-- arbitrary interface that we can turn into a hydralette config
pass
class TransformerModelConfig(ConfigBase):
n_layers: int = field(
default=32,
metadata=dict(help="Number of Transformer Layers"),
validate=lambda value: value > 0 # <-- validate config value
)
num_attention_heads: int = field(
default=8,
metadata=dict(help="Number of Attention Heads per Layer")
)
class RNNModelConfig(ConfigBase):
n_layers: int = field(
default=4,
metadata=dict(help="Number of RNN Layers")
)
bidirectional: bool = field(
default=True,
metadata=dict(help="Bidirectional or Unidirectional RNN Layers")
)
class Config(ConfigBase):
output_dir: Path = field(
default=Path("output"),
metadata=dict(help="Output directory to save all results to"),
convert=lambda path: Path(path).expanduser(), # <-- custom conversion lambda, otherwise type hint is tried
)
model: Union[TransformerModelConfig, RNNModelConfig] = field(
default=RNNModelConfig, # <-- default group
metadata=dict(help="Config for model"),
groups=dict(transformer=TransformerModelConfig, rnn=RNNModelConfig), # <-- define groups
)
library: LibraryConfigHydralette = field(
default_factory=LibraryConfigHydralette, # <-- regular hierarchical config
metadata=dict(help="Config for library")
)
interface = field(
from_signature=some_function, # <-- use function signature to derive config
metadata=dict(help="Config for some_function")
)
if __name__ == "__main__":
# you can print it explicitly, otherwise this is called
# automatically with .create() if -h or --help are in the overrides
Config.print_help_page()
# instantiate the config with .create()!
# there are 2 main ways of overriding values via CLI:
# 1. load overrides from yaml file
# 2. override individual values via key1.key2.key3=value
# Save some overrides to YAML file
yaml_overrides = {"output_dir": "myoutput", "model": {"n_layers": 64}}
s = yaml.dump(yaml_overrides)
f = tempfile.NamedTemporaryFile("w")
f.write(s)
f.flush()
overrides = f"interface.a=3 --overrides {f.name}"
config = Config.create(
overrides=overrides.split(" "), # splitting by space mimics the format of sys.argv
yaml_overrides=None # alternatively to specifying the yaml path to --overrides, you can also pass the dict here
)
config.print_yaml()
config.save(".") # saves 3 files to the specified directory: config.yaml, defaults.yaml and overrides.yaml
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
hydralette-0.1.5.tar.gz
(11.7 kB
view hashes)
Built Distribution
hydralette-0.1.5-py3-none-any.whl
(12.1 kB
view hashes)
Close
Hashes for hydralette-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 342daf0f338f10454557678010adf10d0532bdcf231cbd1d88118427dca240a8 |
|
MD5 | 2bd8dc7125a77a095be82e6fbe47f7da |
|
BLAKE2b-256 | 97e029c7b04a7cd1cf6c6a2b6ff783cf7cb58d03155c552d0179e68898919540 |