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 and optiona attributes) 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.

Optional Attributes

By default the YAML file has to provide values for all attributes of the config file. By using the Optional type hint optional attributes can be added to a config class. For these optional attributes the YAML file is not required to provide values.

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
    device: Optional[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.1.0.tar.gz (4.9 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.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: yamlattributes-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 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.1.0.tar.gz
Algorithm Hash digest
SHA256 5e001d952148e4c3fc297d6bed57e896ae19270b5850425f6a2b045ce8996bc0
MD5 fb16ed046be6470d7d5ea7a8c7453fca
BLAKE2b-256 5f1756f3607551fcf3a68a2dfa16cd0254211c469a2fb705f3e37f32f3da0c60

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamlattributes-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c4fe0f1818e24fda450a8039f73899aa63193c2227dafd193560dcbb3917288e
MD5 e4118b221d451159203205fad2e5effc
BLAKE2b-256 35a67a0dce8a6209462c96040e633533eaf7725778a964582d6f69561a459012

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