utils#

Module attributes and functions#

utils.binary_to_boolean_type(value)[source]#

Converts a binary string to a boolean type.

Parameters:

value (str) – the binary string

Returns:

the boolean type

Return type:

bool

utils.check_fn_dynamic_type(fn, tp, strict=True)[source]#

Controls if the function respects the type tp. The function must have the same number of arguments as the type tp and the same type for each argument.

Parameters:
  • fn (T) – the function to be checked

  • tp (Type[T]) – the type to be respected

  • strict – if True, raises an error if the function does not respect the type tp

Return type:

bool

utils.create_if_not_exists(path)[source]#

Creates the specified folder if it does not exist.

Parameters:

path (str) – the complete path of the folder to be created

utils.custom_str_underscore(value)[source]#
class utils.disable_logging(min_level=50)[source]#

Wrapper for disabling logging for a specific block of code.

utils.field_with_aliases(choices)[source]#

Build a data type where for each key in choices there are a set of aliases.

Example

Given the following dictionary:

choices = {
    'a': ['a', 'alpha'],
    'b': ['b', 'beta']
}

The values ‘a’ and ‘alpha’ will be converted to ‘a’, and ‘b’ and ‘beta’ will be converted to ‘b’.

Parameters:

choices (dict) – the dictionary containing the aliases

Returns:

the data type for argparse

Return type:

str

utils.in_notebook()[source]#
utils.infer_args_from_signature(signature, excluded_signature=None, ignore_args=None)[source]#

Load the arguments of a function from its signature.

Parameters:
  • signature (Signature) – the signature of the function

  • excluded_signature (Signature | None) – the signature of the function to be excluded from the arguments

  • ignore_args (list | None) – a list of arguments to be ignored when inferring the arguments from the signature

Return type:

dict

This function will return a dictionary with the arguments of the function, their type, and whether they are required or not. If an argument has a default value, it will be included in the dictionary as well.

Returns:

the inferred arguments

Return type:

dict

utils.none_or_float(value)[source]#
utils.random_id(length=8, alphabet='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')[source]#

Returns a random string of the specified length.

Parameters:
  • length – the length of the string

  • alphabet – the alphabet to be used

Returns:

the random string

utils.register_dynamic_module_fn(name, register, tp, ignore_args=None)[source]#

Register a dynamic module in the specified dictionary.

Parameters:
  • name (str) – the name of the module

  • register (dict) – the dictionary where the module will be registered

  • cls – the class to be registered

  • tp (Type[T]) – the type of the class, used to dynamically infer the arguments

  • ignore_args (list | None) – a list of arguments to be ignored when inferring the arguments from the signature

Return type:

Callable[[T | Callable], T]

utils.setup_logging()[source]#

Configures the logging module.

utils.smart_joint(*paths)[source]#
utils.to_parsable_obj(r)[source]#

Convert a non-builtin object to a parsable (and loadable with weights_only=True) object. Looking at you, Namespace.

Return type:

Dict | list | str | int | float | bool