Skip to main content

Utility class to assign YAML file entries to class attributes

Project description

YamlAttributes Tests Actions Status

YamlAttributes is a utility class to load yaml files and assign the entries to associated class attributes. This can be used to create a type safe and dynamic configuration class.

Installation

YamlAttributes can be installed by running pip install yamlattributes

Usage

The abstract YamlAttributes class provides the init method which loads a YAML file and assigns its values to the corresponding class attributes. A class which inherits from YamlAttributes simply has to list the desired configuration as class attributes and two additional special attributes yaml_file_path and yaml_section. After calling the init method the all class attributes wil be assigned the values of the corresponding fields of the YAML file.

API

Attributes

Attribute Default Description
yaml_file_path './' Sets the path to the YAML file which should be loaded.
yaml_section 'config' Sets YAML section which contains the desired config fields.

init method

Args values Description
mode 'sync', 'soft_config' Selects the the mode of how the YAML values should update the config class. The sync mode keeps the YAML file and the config class in sync. Each attribute in the config class (except of the two special ones) have to be in the YAML file and vis versa. The soft_config mode allows the YAML file to have additional fields which the config class does not have.

Example

First create your desired config class and set the destination of the YAML file to be loaded through the special yaml_file_path attribute.

# ./config.py

class MyConfig(YamlAttributes):
    yaml_file_path = './configs/my-config.yaml'
    batch_size: int
    num_classes: int
    optimizer: str

Make ure the YAML file exists and has all entries the config class MyConfig needs.

# ./configs/my-config.yaml

config:
    batch_size: 32
    num_classes: 10
    optimizer: 'adam'

Finally you can initialize your config class and enjoy accessing config values with autocomplete.

# ./main.py

from config import MyConfig

MyConfig.init()

# Access config values with autocomplete
MyConfig.batch_size

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

yamlattributes-0.0.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

yamlattributes-0.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file yamlattributes-0.0.1.tar.gz.

File metadata

  • Download URL: yamlattributes-0.0.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for yamlattributes-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a33849e2eaefb32be8682c81684c42351da18e17e0cc47788e8908367b4e5591
MD5 8321664c2e97af5160d6c34e42b81d0e
BLAKE2b-256 38756da0e45fe4cd1df1a8844d1d90e61f5b9c7afa0e1659d2ecf21bab1ac970

See more details on using hashes here.

File details

Details for the file yamlattributes-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: yamlattributes-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for yamlattributes-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8745f48490292b54d44dd6e0adfdbf3fa4257d5090f82c80be587f4490696570
MD5 f8cca5c013d06b850b5e8987d5bfe439
BLAKE2b-256 e51943d207cf0e2ee5e9b9addc18a60f265a5d7b06d2e9571b022c626659a876

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