YamlAttributes 
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
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.
🚀 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
🛠️ 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 .
Metadata
Release files for yamlattributes 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| yamlattributes-0.2.0.tar.gz | 7.1 kB | Without provenance | Details for yamlattributes-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| yamlattributes-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for yamlattributes-0.2.0-py3-none-any.whl |
Total release size: 13.4 kB
Release files / yamlattributes-0.2.0.tar.gz
| Download URL | yamlattributes-0.2.0.tar.gz |
|---|---|
| Size | 7.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
97d27d73ee598787dabab2281ddd4e38ba6ccb53f38a0244e592d7008ad83ea0
|
|
BLAKE2b-256 checksum How to use checksums |
c30145ebe3d2d38db6e3d57f5830ad5b2014110c1de9f7d40339420783d05a36
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / yamlattributes-0.2.0-py3-none-any.whl
| Download URL | yamlattributes-0.2.0-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
966e3dde1972d6eee97e6dcd62e9b62ae6d09aca072b5a17aeb121b441192a2e
|
|
BLAKE2b-256 checksum How to use checksums |
800c728bac6f2662d15d075b6e55c9b709ed9d727626de37d0ca53b3140b246f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for yamlattributes 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.