Configuration

flat_bug.config.check_cfg_types(cfg: dict, strict: bool = False) bool

Check if the config is a dictionary and that the types of the values in the config dictionary are correct.

Parameters:
  • cfg (dict) – The config dictionary to check.

  • strict (bool, optional) – If True, raise an error if a key is not recognized. Defaults to False.

Returns:

True if all checks pass, raises an error otherwise.

Return type:

bool

flat_bug.config.check_types(value: Any, expected_type: List[Any] | Iterable[type] | type, key: str = '<Not specified>', strict: bool = True) bool

Recursively check if the type of a value matches the expected type.

If the expected type is a list, the first element is the type of the value, and the second element is a list of types that the elements of the value match, a single type that all elements should match or a tuple/type of types that all elements should match any of.

Parameters:
  • value (Any) – The value to check.

  • expected_type (Union[List[Any], Iterable[type], type]) – The expected type of the value.

  • key (str, optional) – Name of the value to use in error messages. Defaults to “<Not specified>”.

  • strict (bool, optional) – If True, raise an error if the check fails. Defaults to True.

Returns:

True if the check passes, and False if strict is False and the check fails. Raises an error otherwise.

Return type:

out (bool)

Raises:
  • ValueError – If the expected type list does not have exactly 2 elements.

  • TypeError – If the expected type is not a list, an iterable or a ‘type’ object.

  • TypeError – If the number of types in the list does not match the number of items in the value.

  • TypeError – If the value does not match the expected type.

flat_bug.config.get_type_def(obj: Any, tuple_list_interchangeable: bool = False) Any | List[Any]

Generates a dynamic type definition for an object.

The type definition schema is defined like this: - If the object is a tuple or a list, the first element is the type of the object, and the second element is a list of type definitions for the elements of the object. - If the object is not a tuple or a list, the type definition is the type of the object.

For example;
  • the type definition for the object (1, “A”, True) would be [tuple, [int, str, bool]].

  • the type definition for the object [[2, “B”], [3, “C”]] would be [list, [[list, [int, str]], [list, [int, str]]]].

Parameters:
  • obj (Any) – The object to generate a type definition for.

  • tuple_list_interchangeable (bool, optional) – If True, tuples and lists are considered interchangeable. Defaults to False.

Returns:

The type definition for the object.

Return type:

out (Union[Any, List[Any]])

flat_bug.config.read_cfg(path: str | PathLike, strict: bool = False) dict

Load and validate the config file.

Missing keys are replaced with default values.

Parameters:
  • config (Union[str, os.PathLike]) – The path to the config file.

  • strict (bool, optional) – If True, raise an error if a key is not recognized. Defaults to False.

Returns:

The config dictionary.

Return type:

out (dict)

flat_bug.config.write_cfg(cfg: dict, path: str | PathLike, overwrite: bool = False) str | PathLike

Save the config dictionary to a YAML file.

Parameters:
  • cfg (dict) – The config dictionary to save.

  • path (Union[str, os.PathLike]) – The path to save the config file.

  • overwrite (bool, optional) – If True, overwrite the file if it already exists. Defaults to False.

Returns:

The path to the saved config file.

Return type:

out (Union[str, os.PathLike])