Skip to content

config

thutil.config

Functions:

  • validate_config

    Validate the config file with the schema file.

  • load_config

    Load data from a JSON or YAML file. The YAML file can contain variable-interpolation, will be processed by OmegaConf.

  • load_jsonc

    Load data from a JSON file that allow comments.

  • unpack_dict

    Unpack one level of nested dictionary.

  • write_yaml

    Write data to a YAML file.

  • read_yaml

    Read data from a YAML file.

validate_config(config_dict=None, config_file=None, schema_dict=None, schema_file=None, allow_unknown=False, require_all=False)

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

load_config(filename: Union[str, Path]) -> dict

Load data from a JSON or YAML file. The YAML file can contain variable-interpolation, will be processed by OmegaConf.

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

Returns:

  • jdata ( dict ) –

    (dict) The data loaded from the file

load_jsonc(filename: str) -> dict

Load data from a JSON file that allow comments.

unpack_dict(nested_dict: dict) -> dict

Unpack one level of nested dictionary.

write_yaml(jdata: dict, filename: Union[str, Path])

Write data to a YAML file.

read_yaml(filename: Union[str, Path]) -> dict

Read data from a YAML file.