Skip to content

config

thkit.config

Classes:

  • Config

    Base class for configuration files.

Functions:

Config

Base class for configuration files.

Methods:

  • validate

    Validate the config file with the schema file.

  • loadconfig

    Load data from a JSON or YAML file.

validate(config_dict: dict | None = None, config_file: str | None = None, schema_dict: dict | None = None, schema_file: str | None = None, allow_unknown: bool = False, require_all: bool = False) staticmethod

Validate the config file with the schema file.

Parameters:

  • config_dict (dict, default: None ) –

    config dictionary. Defaults to None.

  • config_file (str, default: None ) –

    path to the YAML config file, will override config_dict. Defaults to None.

  • schema_dict (dict, default: None ) –

    schema dictionary. Defaults to None.

  • schema_file (str, default: None ) –

    path to the YAML schema file, will override schema_dict. Defaults to None.

  • allow_unknown (bool, default: False ) –

    whether to allow unknown fields in the config file. Defaults to False.

  • require_all (bool, default: False ) –

    whether to require all fields in the schema file to be present in the config file. Defaults to False.

Raises:

  • ValueError

    if the config file does not match the schema

loadconfig(filename: str | Path) -> dict staticmethod

Load data from a JSON or YAML file.

Args: filename (Union[str, Path]): The filename to load data from, whose suffix should be .json, jsonc, .yml, or .yml

Returns:

  • jdata ( dict ) –

    (dict) The data loaded from the file

Notes
  • The YAML file can contain variable-interpolation, will be processed by OmegaConf. Example input YAML file:
    server:
    host: localhost
    port: 80
    
    client:
    url: http://${server.host}:${server.port}/
    server_port: ${server.port}
    # relative interpolation
    description: Client of ${.url}
    

get_default_args(func: Callable) -> dict

Get dict of default values of arguments of a function.

Parameters:

  • func (Callable) –

    function to inspect

argdict_to_schemadict(func: Callable) -> dict

Convert a function's type-annotated arguments into a cerberus schema dict.

Handles
  • Single types
  • Union types (as list of types)
  • Nullable types (None in Union)
  • Only checks top-level types (no recursion into list[int], dict[str, float], etc.)
  • Supports multiple types in cerberus (e.g. {"type": ["integer", "string"]}) when a Union is given.

Parameters:

  • func (callable) –

    function to inspect

Returns:

  • schemadict ( dict ) –

    cerberus schema dictionary