tempor.core.utils module¶
Utility functions for TemporAI core.
- class tempor.core.utils.RichReprStrPassthrough(string: str)[source]¶
Bases:
objectA pass-through class for
rich__repr__strings. Yields thestringin its__repr__.
-
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.
- tempor.core.utils.PreferArgOrKwarg¶
Literal type for
preferargument inget_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, andposition_if_argsfromargsandkwargs. Will returnNoneif no such argument found.- Algorithm:
Check if an
argof typeargument_typeis found at indexposition_if_argsinargs.Check if a
kwargby keyargument_nameis found inkwargs.If both 1 and 2 are found raise
RuntimeErrorifpreferis set to"exception". Otherwise take theargorkwargitem as specified bypreferaccordingly. The other item will be left as it was originally provided inargs/kwargs.If
kwargfrom 2 is not of typeargument_typeraiseTypeError.Return 1 or 2 if argument is found, else return
None. Also returnargsandkwargswith the argument “popped”.
- Parameters:¶
- args : Tuple¶
argsto check.- kwargs : Dict¶
kwargsto 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
argsat which the argument should be found, if it is provided byargs.- prefer : PreferArgOrKwarg, optional¶
Whether to prefer the
argor thekwargif 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
argsandkwargs.TypeError – Error in case the
kwargprovided by keyargument_nameis not of the expected type.
- Returns:¶
(found_argument_or_None, args, kwargs). If argument found, it will be removed from theargs/kwargsreturned.- Return type:¶
Tuple[Any, Tuple, Dict]
- tempor.core.utils.unique_in_order_of_appearance(iterable: Iterable) list[source]¶
Return unique elements from
iterablein order of their appearance.Note
All items in
iterablemust be hashable.
- tempor.core.utils.is_method_defined_in_class(cls_or_obj: Any, method_name: str) bool[source]¶
Check if method named
method_namemethod is defined in the given class (or object’s class) or inherited.
- tempor.core.utils.clean_multiline_docstr(docstr: str) str[source]¶
Clean a multi-line docstring by getting rid of newlines and cleaning up whitespace.
-
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 thanmax_len_keep, then truncate with....