itwinai.utils

Utilities for itwinai package.

itwinai.utils.generate_random_name()[source]
itwinai.utils.load_yaml(path: str) Dict[source]

Load YAML file as dict.

Parameters:

path (str) – path to YAML file.

Raises:

yaml.YAMLError – for loading/parsing errors.

Returns:

nested dict representation of parsed YAML file.

Return type:

Dict

itwinai.utils.dynamically_import_class(name: str) Type[source]

Dynamically import class by module path. Adapted from https://stackoverflow.com/a/547867

Parameters:

name (str) – path to the class (e.g., mypackage.mymodule.MyClass)

Returns:

class type.

Return type:

__class__

itwinai.utils.flatten_dict(d: MutableMapping, parent_key: str = '', sep: str = '.') MutableMapping[source]

Flatten dictionary

Parameters:
  • d (MutableMapping) – nested dictionary to flatten

  • parent_key (str, optional) – prefix for all keys. Defaults to β€˜β€™.

  • sep (str, optional) – separator for nested key concatenation. Defaults to β€˜.’.

Returns:

flattened dictionary with new keys.

Return type:

MutableMapping

class itwinai.utils.SignatureInspector(func: Callable)[source]

Bases: object

Provides the functionalities to inspect the signature of a function or a method.

Parameters:

func (Callable) – function to be inspected.

INFTY: int = 9223372036854775807
property has_varargs: bool

Checks if the function has *args parameter.

property has_kwargs: bool

Checks if the function has **kwargs parameter.

property required_params: Tuple[str]

Names of required parameters. Class method’s β€˜self’ is skipped.

property min_params_num: int

Minimum number of arguments required.

property max_params_num: int

Max number of supported input arguments. If no limit, SignatureInspector.INFTY is returned.

itwinai.utils.clear_key(my_dict: Dict, dict_name: str, key: Hashable, complain: bool = True) Dict[source]

Remove key from dictionary if present and complain.

Parameters:
  • my_dict (Dict) – Dictionary.

  • dict_name (str) – name of the dictionary.

  • key (Hashable) – Key to remove.

itwinai.utils.make_config_paths_absolute(args: List[str])[source]

Process CLI arguments to make paths specified for –config-path or -cp absolute. Returns the modified arguments list.

Parameters:

args (List[str]) – a list of system arguments

Returns:

the updated list of system arguments, where all the config path argument is

absolute.

Return type:

List(str)

itwinai.utils.get_root_cause(exception: Exception) Exception[source]

Recursively extract the first exception in the exception chain.

itwinai.utils.to_uri(path_str: str | Path) str[source]

Parse a path and convert it to a URI.

Parameters:

path_str (str) – path to convert.

Returns:

URI.

Return type:

str