Configurations#

Installation#

Basic Examples#

Loading a class#

Consider you have any class, and you want to configure and load it though a yaml file. For example a dataclass of a user:

from dataclasses import dataclass
from typing import Any

@dataclass
class User:
    age: int
    name: str

To load it from yaml, you first have to define its configuration.

import yaloader
    
@yaloader.loads(User)
class UserConfig(yaloader.YAMLBaseConfig):
    age: int
    name: str

Now a loader can be used to load a string or file holding the classes’ configuration.

loader = yaloader.ConfigLoader()
user_config = loader.construct_from_string("!User {age: 42, name: Alice}")
print(type(user_config))
print(user_config)
<class '__main__.UserConfig'>
age=42 name='Alice'

user_config now holds the configuration of the user. The user itself can be loaded using the user_config.load() method.

user = user_config.load()
print(type(user))
print(user)
<class '__main__.User'>
User(age=42, name='Alice')

The loading is not limited to single configurations. Every valid YAML file, containing !User tags is fine.

loader = yaloader.ConfigLoader()
all_user_configs = loader.construct_from_string(
    """
    - !User {age: 42, name: Alice}
    - !User {age: 20, name: Bob}
    - !User {age: 12, name: Peter}
    """
)
print(all_user_configs)
[UserConfig(age=42, name='Alice'), UserConfig(age=20, name='Bob'), UserConfig(age=12, name='Peter')]

Adding multiple configurations to the loader#

The configuration loader allows you to add multiple configurations for the same tag which will be layered while construction.

Error messages#

Thanks to pydantic you also get nice error messages when an incorrect configuration will be loaded.

try:
    loader.construct_from_string("!User {age: 'Not an int', name: Alice}")
except yaloader.YAMLValueError as e:
    raise e from None
An exception has occurred, use %tb to see the full traceback.

YAMLValueError: Could not validate the configuration for the tag !User
  in "<unicode string>", line 1, column 1:
    !User {age: 'Not an int', name:  ... 
    ^
  1 validation error for UserConfig
  age
    Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='Not an int', input_type=str]
      For further information visit https://errors.pydantic.dev/2.6/v/int_parsing