BaseConfig#

class config_base.BaseConfig#

Bases: BaseModel

Base class for storing model parameters.

Contains functionality for reading / writing parameters to config files in the YAML format.

See also

[pydantic docs](https://pydantic-docs.helpmanual.io/):

for more information about using pydantic’s model classes.

pydantic.BaseModel: which handles converting data to Python types. pydantic.validator: which allows additional custom validation methods.

Examples

Example of creating a config class and initialising it with values, values will be validated and converted to correct type on initialisation.

>>> from pathlib import Path
>>> from caf.toolkit import BaseConfig
>>> class ExampleParameters(BaseConfig):
...    import_folder: Path
...    name: str
...    some_option: bool = True
>>> parameters = ExampleParameters(
...    import_folder="Test Folder",
...    name="Test",
...    some_option=False,
... )

Example of instance of class after initialisation, the path differs depending on operating system.

>>> parameters 
ExampleParameters(
    import_folder=WindowsPath('Test Folder'),
    name='Test',
    some_option=False,
)

Config class can be converted to YAML or saved with save_yaml.

>>> print(parameters.to_yaml())
import_folder: Test Folder
name: Test
some_option: no

Config class data can be loaded from a YAML config file using load_yaml.

>>> yaml_text = '''
... import_folder: Test Folder
... name: Test
... some_option: no
... '''
>>> loaded_parameters = ExampleParameters.from_yaml(yaml_text)
>>> loaded_parameters == parameters
True