Skip to main content

No project description provided

Project description

Hydralette

Create complex configurations in a simple, pythonic way!

python black isort license

Documentation

Installation

pip install hydralette
# OR
poetry add hydralette

Changelog

  • v0.2.4
    • bugfix: allow overriding fields with reference
  • v0.2.3
    • change version of dill dependency
  • v0.2.2
    • support mixing _fields, _groups and _from_signature
    • support literal values as groups
    • add boolean flags as --flag or --no-flag
  • v0.2.1
    • fix yaml representation
    • support setting value to None in automatic conversion
  • v0.2.0
    • complete re-design --> breaking changes!
    • easier creation of hierarchical configs
  • v0.1.5
    • Add yaml overrides
    • Add end-to-end tests with pytest
  • v0.1.4
  • v0.1.3
    • Add automatic generation of configs from signatures via from_signature and config_from_signature
  • v0.1.2
    • Add support for config groups, references, validation and type conversion
    • Add CLI help pages

Features

Basics

Create a config using the Config class, passing the config fields as keyword arguments.

from hydralette import Config
cfg = Config(a=1, b=2, c="abc")

Config objects can be create from and exported to dicts.

from hydralette import Config
cfg = Config.from_dict({"a": 1, "b": 2, "c": "abc"})
print(cfg.to_dict())

Every hydralette Config be overriden via CLI:

from hydralette import Config, Field
cfg = Config(
    a=Field(default=1, help="Lorem ipsum"),
    b=Config(
        c=Field(default=2, help="Lorem ipsum")
    )
)
cfg.apply(["--help"]) # or cfg.print_help()

# prints:

# Usage python script.py [OPTIONS]
# --a 1                   Lorem ipsum
# --b.c 1                 Lorem ipsum

After creation, you can override, resolve references, validate and check for missing required arguments.

cfg.override(["--a", "1"]) # overrides defaults to sys.argv[1:]
cfg.resolve_references()
cfg.validate()
cfg.check_required_args()
# OR
cfg.apply(["--a", "1"]) # shorthand for the above

Groups

Config groups can be very handy to make components interchangeable without requiring them to share a config.

from hydralette import Config, Field
cfg = Config(
    model=Config(
        _groups={
            "_default": "rnn",
            "rnn": Config(
                n_layers=2,
                bidirectional=False
            ),
            "transformer": Config(
                n_layers=16,
                num_attention_heads=8
            )
        }
    )
)
cfg.apply(["--model", "transformer"])

From signature / existing config

Often times, part of your configuration is already implemented somewhere else and duplicating this information creates a source of failure. Instead, you can automatically generate your hydralette Config based on an existing interface

def calc(a: int, b=2):
    pass

from hydralette import Config, Field
cfg = Config(_from_signature=calc)
# equivalent to
cfg = Config(a=Field(type=int), b=Field(default=2, type=int))

Fields

When you directly pass a value to Configs constructor, hydralette will create a Field under the hood. To use additional features, you can create it explicitly

convert: Specify how command-line overrides should be converted to the target type. If not explicitly specified, hydralette tries to use the field's type as conversion function (type is either explicitly specified or automatically derived from default / default_factory).

import json
from hydralette import Config, Field
cfg = Config(my_dict=Field(default={"a": 1, "b": {"c": 2}}, convert=json.loads))
cfg.apply(['--my_dict', r'{"a": 2, "b": {"c": 3}}'])

validate: Constrain what values are valid for your field. If the validity of a value depends on the rest of the config, use _validate in the Config constructor instead.

from hydralette import Config, Field
cfg = Config(n=Field(default=1, validate=lambda n: n > 0))
cfg.apply(['--n', '-1'])
# throws: ValidationError: Field validation failed for -1
cfg = Config(_validate=lambda cfg: cfg.a > cfg.b, a=1, b=2)
cfg.apply() # or cfg.validate()
# throws: ValidationError: Config validation failed for {'a': 1, 'b': 2}

reference / reference_root: Refer to any other value in the config or root config

[!NOTE]
Hydralette will not automatically infer the type from the reference lambda. To get automatic conversion of CLI arguments, pass type or convert explicitly.

from hydralette import Config, Field
from pathlib import Path
cfg = Config(
    dir=Path("outputs"),
    train=Config(
        checkpoint_dir=Field(reference_root=lambda cfg: cfg.dir / "checkpoints", type=Path), # relative to current config
        metrics_dir=Field(reference=lambda cfg: cfg.checkpoint_dir.parent / "metrics", type=Path) # relative to root config
    )
)
cfg.resolve_references()

Backlog

  • CLI
  • groups
  • from signatures
  • validation
  • conversion
  • references
  • yaml representation
  • pickle serialization
  • allow combining _groups, _fields and _from_signature
  • special support for boolean flags in CLI

Dev Info

Steps on new release:

  1. Run tests pytest
  2. Edit docs
  3. Increment version in pyproject.toml
  4. Add changelog to README.md
  5. Push increment to GitHub
  6. Publish to PyPI poetry publish --build
  7. Publish docs mkdocs gh-deploy
  8. Create release and tag on GitHub

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

hydralette-0.2.4.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

hydralette-0.2.4-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file hydralette-0.2.4.tar.gz.

File metadata

  • Download URL: hydralette-0.2.4.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.14 Linux/4.18.0-553.16.1.el8_10.x86_64

File hashes

Hashes for hydralette-0.2.4.tar.gz
Algorithm Hash digest
SHA256 6d3e215f6be839e15576efb142268a5962590da5f532eb061af1a44a7e6a0af8
MD5 3750bd5b783066534c89a8c259a6faab
BLAKE2b-256 4e32b678a46c60536fbb71ade15e7a41cea43e06a0fd9c2e3e65f487d6d069a7

See more details on using hashes here.

File details

Details for the file hydralette-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: hydralette-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.14 Linux/4.18.0-553.16.1.el8_10.x86_64

File hashes

Hashes for hydralette-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3e9b39a1024e77b95f02e042b28b9c736b0b1e0675a39d37952ec36f674b79b3
MD5 78b81ee643b97aae71457131095e713f
BLAKE2b-256 e9a7640ad9c87e0acaab3b78fd6f680ac590c0bf7fc07bfc0d9117e2e74b3cdb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page