Skip to main content

No project description provided

Project description

Expedantic

pypi Python 3.10

Installation

pip install expedantic

Basic Usage

from expedantic import ConfigBase

# Define a config model
class MyConfig(ConfigBase):
    device: str = 'cuda:0'
    learning_rate: float = 1.0e-3
    num_epochs: int = 100

# Save and load from yaml files
my_config = MyConfig()
my_config.save_as_yaml("config.yaml")
my_config = MyConfig.load_from_yaml("config.yaml")


# For a given function or class,
def learn(device: str, learning_rate: float, num_epochs: int):
    ...

# Consume the config by getting kwargs automatically.
learn(**my_config.compatible_args(learn))


# Or pass manually
learn(my_config.device, my_config.learning_rate, my_config.num_epochs)

Examples

Find some examples here.

Features

  • Type validation using pydantic.

  • JSON schema generation for autocompletion on yaml files. You can facilitate efficient yaml editing on IDEs such as VS Code.

    For the VS Code usage, the following steps enable the autocompletion on yaml files for configuration models.

    1. Generate a schema.
    from pathlib import Path
    from typing import Annotated, Literal
    from expedantic import ConfigBase, Field
    
    
    class Config(ConfigBase):
        algorithm: Literal["TRPO", "PPO", "SAC", "TD3"] = "TRPO"
        target_kl: Annotated[
            float,
            Field(
                title="Target Kullback-Leibler divergence between updates.",
                description="Should be small for stability. Values like 0.01, 0.05.",
                gt=0.0,
            ),
        ] = 0.01
    
    
    config = Config()
    Path("configs").mkdir(exist_ok=True)
    config.save_as_yaml("configs/config.yaml")
    Config.generate_schema("schemas/config_schema.json")
    
    1. Install the yaml language extension.

    2. Associating the schema

     .vscode/settings.json
    yaml.schemas: {
        "schemas/config_schema.json": "configs/config.yaml",
    }
    

    Check the further rules for association in the description of the extension.

    1. Enjoy! description autocompletion
  • Integrated argument parser with supporting nested key access:

    # run.py
    class MyInnerConfig(ConfigBase):
        inner_key: str = "inner_value"
    class MyConfig(ConfigBase):
        inner_config: MyInnerConfig = MyInnerConfig()
        outer_key: int = 10
    
    
    my_config = MyConfig.parse_args()
    
    python run.py --inner_config.inner_key "another inner value" --outer_key 20
    
  • !include directive support for yaml files:

    # base.yaml
    learning_rate: 3.0e-5
    num_epochs: 10
    device: cpu
    
    # derived.yaml
    <<: !include base.yaml
    device: cuda
    extra: True
    

    The content of derived.yaml is equivalent to the following:

    learning_rate: 3.0e-5
    num_epochs: 10
    device: cuda
    extra: True
    
  • Mutually exclusive configuration groups:

    from pydantic import ValidationError
    
    
    class Config(ConfigBase):
        _mutually_exclusive_sets = [{"make_algorithm_A_obsolete", "use_algorithm_A"}]
    
        make_algorithm_A_obsolete: bool = True
        use_algorithm_A: bool = False
    
    try:
        config = Config(make_algorithm_A_obsolete=True, use_algorithm_A=True)
    except ValidationError as e:
        print(e)
        """
        1 validation error for Config
            Value error, Mutual exclusivity has broken. (set: {'make_algorithm_A_obsolete', 'use_algorithm_A'}) [type=value_error, input_value={'make_algorithm_A_obsolete': True, 'use_algorithm_A': True}, input_type=dict]
        """
    

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

expedantic-0.1.8.tar.gz (55.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

expedantic-0.1.8-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file expedantic-0.1.8.tar.gz.

File metadata

  • Download URL: expedantic-0.1.8.tar.gz
  • Upload date:
  • Size: 55.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.10

File hashes

Hashes for expedantic-0.1.8.tar.gz
Algorithm Hash digest
SHA256 050a888742a31b79570882adda7194391618d207172213fa399301023df1040c
MD5 d9cd7a78d0b64c22dfffb76211aeeaf6
BLAKE2b-256 e06a349ba3bd8c0585a92555a7509e7b5a08c0181ce7f52c6496db6c9179db9c

See more details on using hashes here.

File details

Details for the file expedantic-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: expedantic-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.10

File hashes

Hashes for expedantic-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 9f26e6b04ef63bd857c4d1746dbd0b4bcfdfea3ac5dce406fcd543ec5a5b81dd
MD5 ab958ccb5e40f7b2405e276547ab8a88
BLAKE2b-256 2836152b38912943e82c9a2a7ebbcf85e0aefb53a04e7b46ec578cc4f0bbcdb2

See more details on using hashes here.

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