tempor.core.utils module

Utility functions for TemporAI core.

tempor.core.utils.get_class_full_name(o: object) str[source]

Get the full name of a class.

See: https://stackoverflow.com/a/2020083.

Parameters:
o : object

The object to get the class full name of.

Returns:

The full name of the class.

Return type:

str

class tempor.core.utils.RichReprStrPassthrough(string: str)[source]

Bases: object

A pass-through class for rich __repr__ strings. Yields the string in its __repr__.

Parameters:
string : str

The string to pass through.

tempor.core.utils.is_iterable(o: object) bool[source]

Check if an object is an iterable.

Parameters:
o : object

The object to check.

Returns:

Whether the object is an iterable.

Return type:

bool

tempor.core.utils.ensure_literal_matches_dict_keys(literal: Any, d: dict[str, Any], literal_name: str = 'literal', dict_name: str = 'dictionary') None[source]

Check that the args of a literal match the keys of a dictionary.

Parameters:
literal : Any

A literal.

d : Dict[str, Any]

A dictionary.

literal_name : str, optional

The name of the literal, for exception description. Defaults to "literal".

dict_name : str, optional

The name of the dictionary, for exception description. Defaults to "dictionary".

Raises:

TypeError – Raised if the args of the literal do not match the keys of the dictionary.

tempor.core.utils.PreferArgOrKwarg

Literal type for prefer argument in get_from_args_or_kwargs. One of "arg", "kwarg", or "exception".

alias of Literal[arg, kwarg, exception]

tempor.core.utils.get_from_args_or_kwargs(args: tuple, kwargs: dict, argument_name: str, argument_type: type, position_if_args: int, prefer: Literal[arg] | Literal[kwarg] | Literal[exception] = 'exception') tuple[Any, tuple, dict][source]

Will attempt to get the function argument as defined by argument_name, argument_type, and position_if_args from args and kwargs. Will return None if no such argument found.

Algorithm:
  1. Check if an arg of type argument_type is found at index position_if_args in args.

  2. Check if a kwarg by key argument_name is found in kwargs.

  3. If both 1 and 2 are found raise RuntimeError if prefer is set to "exception". Otherwise take the arg or kwarg item as specified by prefer accordingly. The other item will be left as it was originally provided in args/kwargs.

  4. If kwarg from 2 is not of type argument_type raise TypeError.

  5. Return 1 or 2 if argument is found, else return None. Also return args and kwargs with the argument “popped”.

Parameters:
args : Tuple

args to check.

kwargs : Dict

kwargs to check.

argument_name : str

The name of the argument to look for.

argument_type : Type

The type of the argument to confirm.

position_if_args : int

The index in args at which the argument should be found, if it is provided by args.

prefer : PreferArgOrKwarg, optional

Whether to prefer the arg or the kwarg if both are found, or to raise an exception if this is set to "exception". Defaults to "exception".

Raises:
  • RuntimeError – Error in case the argument appears to have been provided by both args and kwargs.

  • TypeError – Error in case the kwarg provided by key argument_name is not of the expected type.

Returns:

(found_argument_or_None, args, kwargs). If argument found, it will be removed from the args/kwargs returned.

Return type:

Tuple[Any, Tuple, Dict]

tempor.core.utils.unique_in_order_of_appearance(iterable: Iterable) list[source]

Return unique elements from iterable in order of their appearance.

Note

All items in iterable must be hashable.

Parameters:
iterable : Iterable

The iterable to get unique elements from.

Returns:

List of unique elements in order of their appearance.

Return type:

List

tempor.core.utils.is_method_defined_in_class(cls_or_obj: Any, method_name: str) bool[source]

Check if method named method_name method is defined in the given class (or object’s class) or inherited.

Parameters:
cls_or_obj : Any

The class or object to check.

method_name : str

The name of the method to check.

Returns:

True if method is defined in cls, False if inherited.

Return type:

bool

tempor.core.utils.clean_multiline_docstr(docstr: str) str[source]

Clean a multi-line docstring by getting rid of newlines and cleaning up whitespace.

Parameters:
docstr : str

The docstring to clean.

Returns:

The cleaned docstring.

Return type:

str

tempor.core.utils.make_description_from_doc(obj: Any, max_len_keep: int = 100) str[source]

Make a description from the docstring of an object. Take the class docstring and the __init__ docstring (if there was one defined on the class). If the combined length of these is greater than max_len_keep, then truncate with ....

Parameters:
obj : Any

The object to get the description of.

max_len_keep : int, optional

Maximum description length before truncating. Defaults to 100.

Returns:

Description of the object.

Return type:

str