dlpt.utils module
Various utility functions that simplify everyday code. Example: - converting numbers from/to string - comparing lists, dictionaries - code inspection - strings operations, - …
- dlpt.utils.float_to_str(number: float, show_num_of_digits: int = 2) str
Convert float number with any number of decimal digits and return string with
show_num_of_digits
places after ..- Parameters:
number – float number to convert to string.
show_num_of_digits – number of decimal places (characters) that are added/stripped after ..
- dlpt.utils.get_int_from_str(number: str) int
Return integer representation of a given number string. HEX number strings must start with 0x. Negative numbers are also supported.
- Parameters:
number – int/float string representation of a given number.
- dlpt.utils.get_float_from_str(number: str) float
Return float representation of a given number string. HEX number strings must start with 0x.
- Parameters:
number – int/float/string representation of a given number.
- dlpt.utils.get_list_intersection(l1: List[Any], l2: List[Any]) List[Any]
Return intersection of a given lists.
Note
Operation does not necessary maintain items order!
Note
Only applicable for a lists with primitive types - nested lists will fail - can’t set().
- Parameters:
l1 – first list to compare.
l2 – second list to compare.
- dlpt.utils.get_list_str(data: List[Any], separator: str = ', ') str
Return a human readable list string (for printing purposes).
- Parameters:
data – list to transform to string.
separator – separator to join list items with.
- dlpt.utils.get_list_difference(l1: List[Any], l2: List[Any]) List[Any]
Return difference (items that are unique just to a one of given lists) of a given lists.
Note
Operation does not necessary maintain items order!
- Parameters:
l1 – first list to compare.
l2 – second list to compare.
- dlpt.utils.remove_list_duplicates(data: List[Any]) List[Any]
Return a list of items without any duplicates.
Note
Operation does not necessary maintain items order!
- Parameters:
data – list with possibly duplicated items.
- dlpt.utils.search_str_in_lines(str_to_search: str, lines: List[str], exact_match: bool = False) int | None
- Return index of a first line where
str_to_search
string can be found. Otherwise, return None.
- Parameters:
str_to_search – string to search in lines.
lines – list of strings, where
str_to_search
is searched.exact_match – if True, only exact
str_to_search
string is compared inlines
. Otherwise, only string presence is checked.
- Return index of a first line where
- dlpt.utils.are_list_values_equal(l1: List[Any], l2: List[Any]) bool
Return True if lists have the same values, False otherwise.
Note
Items order is not respected. If order must also be respected, just use list comparison:
l1 == l2
Note
List items must be hashable.
- Parameters:
l1 – first list to compare.
l2 – second list to compare.
- dlpt.utils.are_dict_keys_equal(d1: Dict[Any, Any], d2: Dict[Any, Any]) bool
Return True if dictionaries have the same keys, False otherwise.
- Parameters:
d1 – first dict to compare.
d2 – second dict to compare.
- dlpt.utils.are_dict_values_equal(d1: Dict[Any, Any], d2: Dict[Any, Any]) bool
Return True if dicts have the same values, False otherwise.
Note
When comparing dict items that are not simple types (int, str, …), function might return False if comparing different instances, regardles if object type is the same.
- Parameters:
d1 – first dict to compare.
d2 – second dict to compare.
- dlpt.utils.map_dict_to_class(obj: object, data: Dict[str, Any]) object
Return an object
obj
updated by the values of data dictionary.Note
Only data keys, that match
obj
variable names are updated. Others are silently ignored.- Parameters:
obj – object instance (class) whose values must be updated.
- dlpt.utils.get_obj_public_vars(obj: object) Dict[str, Any]
- Return a dictionary of class variables that does not start with ‘_’ or ‘__’.
Each item represents: ‘<variable name>’: <variable value>
Note
If given
obj
is a class reference, only ‘public’ static variables are returned. If givenobj
is a class instance, ‘public’ static and instance variables are returned.- Args
obj: object (class) to inspect.
- dlpt.utils.get_obj_public_methods(obj: object) Dict[str, Callable[[...], Any]]
Return a dictionary of object public methods that does not start with ‘_’ or ‘__’. Each item represents: ‘<method name>’: < method reference>
Note
Only class ‘public’ methods are returned, without @staticmethod. They are of type ‘<bound method…>’
- Parameters:
obj – object to inspect.
- dlpt.utils.get_module_callables(module_instance: module) Dict[str, Callable[[...], Any]]
Return a dictionary of public methods that does not start with ‘_’ or ‘__’. Each item represents: ‘<callable name>’: <callable reference>
- Parameters:
module_instance – module object to inspect.
- dlpt.utils.get_module_public_classes(module_instance: module) Dict[str, Callable[[...], Any]]
Return a dictionary of public classes that does not start with ‘_’ or ‘__’. Each item represents: ‘<class name>’: <class reference>
- Parameters:
module_instance – module object to inspect.
- dlpt.utils.get_module_public_functions(module_instance: module) Dict[str, Callable[[...], Any]]
Get a list of references to all callable objects from a given module instance.
- Parameters:
module_instance – module object to inspect.
- dlpt.utils.get_caller_location(depth: int = 2) str
Return a function/location/line number of a caller function. Return string format: “<function()> @ <absolute file path>:<line number>”
Note
Using inspect module can be slow ->
inspect.getouterframes()
callinspect.stack()
which actually read files.Warning
While steping through code in a debug session, stack can be full of a debugger (example: ‘ptvsd’) entries.
- Parameters:
depth – stack frame depth inspection. The first (index = 0) entry in the returned list represents current frame; the last entry represents the outermost call on frame’s stack.