config
thkit.config
¶
Functions:
-
validate_config–Validate the config file with the schema file.
-
loadconfig–Load data from a JSON or YAML file.
-
load_jsonc–Load data from a JSON file that allow comments.
-
get_default_args–Get dict of default values of arguments of a function
-
argdict_to_schemadict–Convert a function's type-annotated arguments into a cerberus schema dict.
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
loadconfig(filename: str | Path) -> dict
¶
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:
load_jsonc(filename: str) -> dict
¶
Load data from a JSON file that allow comments.
get_default_args(func: callable) -> dict
¶
Get dict of default values of arguments of a function Args: 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 (
Nonein 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 aUnionis given.
Parameters:
-
func(callable) –function to inspect
Returns:
-
schemadict(dict) –cerberus schema dictionary