mt.base

Minh-Tri Pham’s base package for Python.

# Variable prefix naming convention

On 2022/02/24, I introduced a prefix naming convention for variables. All variable names have been gradually updated since then. The convention is described in the following paragraphs.

Variables can have a prefix in the form of a letter followed by an underscore. ‘l_’ stands for a list of things. ‘t_’ stands for a tuple of things. ‘a_’ stands for a numpy.ndarray which is an array of things. ‘b_’ stands for a tensorflow.Tensor which is a batch of things. ‘n_’ stands for a number of things.

Nested collections can be prefixed with multiple letters followed by an underscore, using the same convention as above. For example, ‘ll_’ stands for a list of lists of things. In addition, letters that are not the first letter in the prefix accept further values ‘i’, ‘m’, and ‘v’ which stand for image, matrix and vector respectively. For example, Prefix ‘biv_’ stands for a tensorflow batch of images of vectors of things.

Submodules

Functions

  • deprecated_func(): A decorator to warn the user that the function has been deprecated and will be removed in future.

  • cast(): Casts an object to a target type.

  • castable(): Checks if an object can be cast to a target type.

  • debug_exec(): Executes a function with trials.

  • debug_on(): A decorator that uses pdb to inspect the cause of an unhandled exception.

  • is_ndarray(): Checks if the object is a numpy ndarray or not.

  • is_jaxarray(): Checks if the object is a jax ndarray or not.

  • is_tftensor(): Checks if the object is a tensorflow tensor or not.

  • is_h5group(): Checks if the object is a h5py.Group instance or not.

mt.base.deprecated_func(deprecated_version, suggested_func=None, removed_version=None, docstring_prefix='')

A decorator to warn the user that the function has been deprecated and will be removed in future.

Parameters:
  • deprecated_version (str) – the version since which the function has been deprecated

  • suggested_func (str or list of strings, optional) – the function to be used in replacement of the deprecated function

  • removed_version (str, optional) – the future version from which the deprecated function will be removed

  • docstring_prefix (str) – prefix string to be inserted at the beginning of every new line in the docstring

mt.base.cast(obj, dst_type)

Casts an object to a target type.

Parameters:
  • obj (object) – object to be cast

  • dst_type (type) – type or class to cast to

Returns:

the cast version of obj

Return type:

object

Raises:

NotImplementedError – if the cast function has not been registered using function register_cast

mt.base.castable(obj, dst_type)

Checks if an object can be cast to a target type.

Parameters:
  • obj (object) – object to be cast

  • dst_type (type) – type or class to cast to

Returns:

whether or not the object is castable to dst_type

Return type:

bool

mt.base.debug_exec(func, *args, **kwargs)

Executes a function with trials.

This function executes a function. However, when an exception is raised in the function, asks the user to invoke an IPython interactive shell or execute a Python command and retry. If the user declines, it passes the exception up in the call stack.

Parameters:
  • func (function) – function to be executed

  • args (list) – positional arguments of the function

  • kwargs (dict) – keyword arguments of the function

Return type:

whatever the function returns

mt.base.debug_on(*exceptions)

A decorator that uses pdb to inspect the cause of an unhandled exception.

>>> @debug_on(TypeError)
... def buggy_function():
...    raise TypeError
mt.base.is_ndarray(a) bool

Checks if the object is a numpy ndarray or not.

mt.base.is_jaxarray(a) bool

Checks if the object is a jax ndarray or not.

mt.base.is_tftensor(a) bool

Checks if the object is a tensorflow tensor or not.

mt.base.is_h5group(a) bool

Checks if the object is a h5py.Group instance or not.

Variables

mt.base.logger

Logger with indenting capability.

<IndentedLoggerAdapter mtbase (Level 1)>
mt.base.home_dirpath

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to ‘strict’.

'/home/docs/.mtbase'
mt.base.temp_dirpath

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to ‘strict’.

'/tmp/docs/.mtbase'