mltk.utils.python¶
Common Python utilities
See the source code on Github: mltk/utils/python.py
Variables
The Python version as <major>.<minor> e.g.: 3.9 |
Functions
|
Creates a directory that returns None if a key does not exist NOTE: Nested dictionaries are also updated to a defaultdict |
|
Append a message to the given exception |
|
Convert the given object to a list |
|
Return if the given list contains a class with the given type |
Return if the debugger is currently active |
|
|
Function decorator hide warnings from the Python warnings library and absl.logging library while the decorated function executes |
|
Given an class or class instance, search the attribute values of the object for the given "needle" and return its corresponding key. |
|
Given a class or class instance, search the attribute keys of the object for the given "needle" and return its corresponding value. |
|
Given a class or class instance, search the attribute keys and values of the object for the given "needle" and return its corresponding value. |
|
Flatten the given iterable object to a list |
|
Return all the keyword-arguments of a method, excluding the 'self' argument |
|
Get the given string with case-insensitive comparsion |
|
Import the Python module at the given path and return the imported module |
|
Install the given pip package is necessary |
|
Return if the given argument is a False value |
|
Return if the given argument is a True value |
|
Reverse find element index that is given value |
|
Load a JSON file and ignoring any single-line, multi-line comments and trailing commas |
|
Recursively merge the source dictionary into the destination and return the destination |
Return if script is executing in a IPython notebook (e.g. |
|
|
Prepend a message to the given exception |
|
Set the absl.logging library log level and return previous level |
|
Decorator to measure time it takes for method or function to execute |
- SHORT_VERSION = '3.12'¶
The Python version as <major>.<minor> e.g.: 3.9
- DefaultDict(d=None, **kwargs)[source]¶
Creates a directory that returns None if a key does not exist NOTE: Nested dictionaries are also updated to a defaultdict
- Return type:
defaultdict
- Parameters:
d (dict) –
- class DictObject[source]¶
Standard Python dictionary that allows for accessing entries as object properties, e.g.:
my_dict_obj = DictObject({‘foo’: 1, ‘bar’: False})
# Both lines do the same thing foo = my_dict_obj.foo foo = my_dict_obj[‘foo’]
my_dict_obj.bar = True my_dict_obj[‘bar’] = True
- __dict__ = mappingproxy({'__module__': 'mltk.utils.python', '__doc__': "Standard Python dictionary that allows for accessing entries as object properties, e.g.:\n\n my_dict_obj = DictObject({'foo': 1, 'bar': False})\n\n # Both lines do the same thing\n foo = my_dict_obj.foo\n foo = my_dict_obj['foo']\n\n my_dict_obj.bar = True\n my_dict_obj['bar'] = True\n\n ", '__getattr__': <function DictObject.__getattr__>, '__setattr__': <function DictObject.__setattr__>, '__dict__': <attribute '__dict__' of 'DictObject' objects>, '__weakref__': <attribute '__weakref__' of 'DictObject' objects>, '__annotations__': {}})¶
- __module__ = 'mltk.utils.python'¶
- __weakref__¶
list of weak references to the object
- merge_dict(destination, source, copy_destination=False)[source]¶
Recursively merge the source dictionary into the destination and return the destination
- Return type:
dict
- Parameters:
destination (dict) –
source (dict) –
- as_list(obj, split=None)[source]¶
Convert the given object to a list
If obj is None, then return empty list
If obj is a string, If the split argument is given then return obj.split(split) else just wrap the string in a list
- Return type:
list
- Parameters:
obj (Any) –
split (str) –
- flatten_list(l)[source]¶
Flatten the given iterable object to a list
- Return type:
list
- Parameters:
l (Iterable) –
- list_rindex(lst, value)[source]¶
Reverse find element index that is given value
- Return type:
int
- Parameters:
lst (Iterable) –
value (Any) –
- contains_class_type(l, cls)[source]¶
Return if the given list contains a class with the given type
- Return type:
bool
- Parameters:
l (Iterable) –
cls (Any) –
- get_case_insensitive(value, l)[source]¶
Get the given string with case-insensitive comparsion
- Return type:
str
- Parameters:
value (str) –
l (Iterable) –
- forward_method_kwargs(**kwargs)[source]¶
Return all the keyword-arguments of a method, excluding the ‘self’ argument
- Return type:
dict
- prepend_exception_msg(e, msg)[source]¶
Prepend a message to the given exception
- Return type:
Exception
- Parameters:
e (Exception) –
msg (str) –
- append_exception_msg(e, msg)[source]¶
Append a message to the given exception
- Return type:
Exception
- Parameters:
e (Exception) –
msg (str) –
- notebook_is_active()[source]¶
Return if script is executing in a IPython notebook (e.g. Jupyter notebook)
- Return type:
bool
- install_pip_package(package, module_name=None, logger=None, install_dir=None, upgrade=False, no_deps=False)[source]¶
Install the given pip package is necessary
- Parameters:
package (str) –
module_name (str) –
logger (Logger) –
install_dir (str) –
- import_module_at_path(path, reload=False)[source]¶
Import the Python module at the given path and return the imported module
- Parameters:
path (str) –
- load_json_safe(path, *args, **kwargs)[source]¶
Load a JSON file and ignoring any single-line, multi-line comments and trailing commas
- Parameters:
path (
str
) – Path to JSON fileargs – Arguments to pass into json.loads
kwargs – Arguments to pass into json.loads
- Return type:
object
- Returns:
Loaded JSON object
- find_object_key_with_value(obj, needle, throw_exception=False)[source]¶
Given an class or class instance, search the attribute values of the object for the given “needle” and return its corresponding key.
Note: If a class if given then it must be instantiable using a default constructor.
- Parameters:
obj (
object
) – Class or class instanceneedle (
object
) – Class attribute value to find in class instancethrow_exception – If true, throw an exception if the needle is not found, return ‘none’ otherwise
- Return type:
str
- Returns:
Lowercase key of found attribute value or “none” if value is not found
- find_object_value_with_key(obj, needle, ignore_case=False, throw_exception=False)[source]¶
Given a class or class instance, search the attribute keys of the object for the given “needle” and return its corresponding value.
NOTE: If a class if given then it must be instantiable using a default constructor (except of Enum classes).
- Parameters:
obj (
object
) – Class or class instanceneedle (
str
) – Class attribute key to find in class instanceignore_case – Ignore the key’s case if True
throw_exception – If true, throw an exception if the needle is not found, return None otherwise
- Returns:
Value of found attribute key or None if key is not found
- find_object_value_with_key_or_value(obj, needle, ignore_case=False, throw_exception=False)[source]¶
Given a class or class instance, search the attribute keys and values of the object for the given “needle” and return its corresponding value.
NOTE: If a class if given then it must be instantiable using a default constructor (except of Enum classes).
- Parameters:
obj (
object
) – Class or class instanceneedle (
Union
[str
,object
]) – Class attribute key or value to find in class instanceignore_case – Ignore the key’s case if True (needle must be a string)
throw_exception – If true, throw an exception if the needle is not found, return None otherwise
- Returns:
Value of found attribute key or None if key/value is not found