Skip to content

yaml_model

Classes#

YamlModel #

Bases: BaseModel

Functions#

from_yaml classmethod #
from_yaml(
    filepath: Path,
    overrides: Optional[Dict[str, Any]] = None,
)

Creates a python configuration object from a yaml file.

Parameters:

Name Type Description Default
filepath Path

The path to the yaml file

required
overrides Optional[Dict[str, Any]]

Overrides to apply to the yaml before instantiating the YamlModel object. Defaults to None.

None

Returns:

Name Type Description
YamlModel

A YamlModel subclass


Source code in tsdat/config/utils/yaml_model.py
@classmethod
def from_yaml(cls, filepath: Path, overrides: Optional[Dict[str, Any]] = None):
    """------------------------------------------------------------------------------------
    Creates a python configuration object from a yaml file.

    Args:
        filepath (Path): The path to the yaml file
        overrides (Optional[Dict[str, Any]], optional): Overrides to apply to the
            yaml before instantiating the YamlModel object. Defaults to None.

    Returns:
        YamlModel: A YamlModel subclass

    ------------------------------------------------------------------------------------
    """
    config = read_yaml(filepath)
    if overrides:
        for pointer, new_value in overrides.items():
            set_pointer(config, pointer, new_value)
    try:
        return cls(**config)
    except (ValidationError, Exception) as e:
        raise ConfigError(
            f"Error encountered while instantiating {filepath}"
        ) from e
generate_schema classmethod #
generate_schema(output_file: Path)

Generates JSON schema from the model fields and type annotations.

Parameters:

Name Type Description Default
output_file Path

The path to store the JSON schema.

required

Source code in tsdat/config/utils/yaml_model.py
@classmethod
def generate_schema(cls, output_file: Path):
    """------------------------------------------------------------------------------------
    Generates JSON schema from the model fields and type annotations.

    Args:
        output_file (Path): The path to store the JSON schema.

    ------------------------------------------------------------------------------------
    """
    output_file.write_text(cls.schema_json(indent=4))

Functions#