mltk.utils.python

Common Python utilities

See the source code on Github: mltk/utils/python.py

Variables

SHORT_VERSION

The Python version as <major>.<minor> e.g.: 3.9

Functions

DefaultDict([d])

Creates a directory that returns None if a key does not exist NOTE: Nested dictionaries are also updated to a defaultdict

append_exception_msg(e, msg)

Append a message to the given exception

as_list(obj[, split])

Convert the given object to a list

contains_class_type(l, cls)

Return if the given list contains a class with the given type

debugger_is_active()

Return if the debugger is currently active

disable_warnings(func[, level])

Function decorator hide warnings from the Python warnings library and absl.logging library while the decorated function executes

find_object_key_with_value(obj, needle[, ...])

Given an class or class instance, search the attribute values of the object for the given "needle" and return its corresponding key.

find_object_value_with_key(obj, needle[, ...])

Given a class or class instance, search the attribute keys of the object for the given "needle" and return its corresponding value.

find_object_value_with_key_or_value(obj, needle)

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_list(l)

Flatten the given iterable object to a list

forward_method_kwargs(**kwargs)

Return all the keyword-arguments of a method, excluding the 'self' argument

get_case_insensitive(value, l)

Get the given string with case-insensitive comparsion

import_module_at_path(path[, reload])

Import the Python module at the given path and return the imported module

install_pip_package(package[, module_name, ...])

Install the given pip package is necessary

is_false(arg)

Return if the given argument is a False value

is_true(arg)

Return if the given argument is a True value

list_rindex(lst, value)

Reverse find element index that is given value

load_json_safe(path, *args, **kwargs)

Load a JSON file and ignoring any single-line, multi-line comments and trailing commas

merge_dict(destination, source[, ...])

Recursively merge the source dictionary into the destination and return the destination

notebook_is_active()

Return if script is executing in a IPython notebook (e.g.

prepend_exception_msg(e, msg)

Prepend a message to the given exception

set_absl_log_level(level)

Set the absl.logging library log level and return previous level

timeit(method)

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

__getattr__(name)[source]
__setattr__(name, value)[source]

Implement setattr(self, name, value).

__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) –

is_true(arg)[source]

Return if the given argument is a True value

Return type:

bool

is_false(arg)[source]

Return if the given argument is a False value

Return type:

bool

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) –

debugger_is_active()[source]

Return if the debugger is currently active

Return type:

bool

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 file

  • args – 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 instance

  • needle (object) – Class attribute value to find in class instance

  • throw_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 instance

  • needle (str) – Class attribute key to find in class instance

  • ignore_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 instance

  • needle (Union[str, object]) – Class attribute key or value to find in class instance

  • ignore_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

timeit(method)[source]

Decorator to measure time it takes for method or function to execute

set_absl_log_level(level)[source]

Set the absl.logging library log level and return previous level

The absl.logging library, a google Python package, can be very verbose. This sets the log level and returns the previous log level

Parameters:

level – The new log level

Returns:

Previous log level

disable_warnings(func, level='ERROR')[source]

Function decorator hide warnings from the Python warnings library and absl.logging library while the decorated function executes