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.

:floppy_disk: Installation

YamlAttributes can be installed by running pip install yamlattributes

:technologist: 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.

:monocle_face: API

Special 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.
yaml_file_path str Instead of providing the path to the YAML file through the special attribute it can also be passed in the init method. The path passed to the init method overrides the special attribute
yaml_section str Instead of providing the YAML section through the special attribute it can also be passed in the init method. The section passed to the init method overrides the special attribute

to_dict Method

The config object can be converted to a dictionary by calling the to_dict method

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.

:rocket: 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

:hammer_and_wrench: Contributing

Interested in contributing? Great, thanks for your interest!

To fix a bug or add a feature, follow these steps:

  • Fork the repo
  • Create a new branch (git checkout -b your-branch)
  • Add your changes and new tests if necessary
  • Make sure all tests pass
  • Commit your changes (git commit -am 'feat: fantastic feature')
  • Push the branch (git push origin improve-feature)
  • Create a Pull Request

Developing

The required packages to start development can be installed with

pipenv install --dev

The tests can be run with

pipenv run pytest

To build the package run

python3 -m build

You can then install it locally with

pip install .

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.2.0.tar.gz (7.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.2.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: yamlattributes-0.2.0.tar.gz
  • Upload date:
  • Size: 7.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.2.0.tar.gz
Algorithm Hash digest
SHA256 97d27d73ee598787dabab2281ddd4e38ba6ccb53f38a0244e592d7008ad83ea0
MD5 d34ad76ed0bed6401dc11605115fb2f5
BLAKE2b-256 c30145ebe3d2d38db6e3d57f5830ad5b2014110c1de9f7d40339420783d05a36

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamlattributes-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 966e3dde1972d6eee97e6dcd62e9b62ae6d09aca072b5a17aeb121b441192a2e
MD5 91a6d7f1454272aeb677e67d0c02a776
BLAKE2b-256 800c728bac6f2662d15d075b6e55c9b709ed9d727626de37d0ca53b3140b246f

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