easybuild.tools.utilities module

Module with various utility functions

author:Kenneth Hoste (Ghent University)
easybuild.tools.utilities.flatten(lst)

Flatten a list of lists.

easybuild.tools.utilities.get_class_for(modulepath, class_name)

Get class for a given Python class name and Python module path.

Parameters:
  • modulepath – Python module path (e.g., ‘easybuild.base.generaloption’)
  • class_name – Python class name (e.g., ‘GeneralOption’)
easybuild.tools.utilities.get_subclasses(klass, include_base_class=False)

Get list of all subclasses, recursively from the specified base class.

easybuild.tools.utilities.get_subclasses_dict(klass, include_base_class=False)

Get dict with subclasses per classes, recursively from the specified base class.

easybuild.tools.utilities.import_available_modules(namespace)

Import all available module in the specified namespace.

Parameters:namespace – The namespace to import modules from.
easybuild.tools.utilities.mk_rst_table(titles, columns)

Returns an rst table with given titles and columns (a nested list of string columns for each column)

easybuild.tools.utilities.natural_keys(key)

Can be used as the sort key in list.sort(key=natural_keys) to sort in natural order (i.e. respecting numbers)

easybuild.tools.utilities.nub(list_)

Returns the unique items of a list of hashables, while preserving order of the original list, i.e. the first unique element encoutered is retained.

Code is taken from http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order

Supposedly, this is one of the fastest ways to determine the unique elements of a list.

@type list_: a list :-)

Returns:a new list with each element from list appearing only once (cfr. Michelle Dubois).
easybuild.tools.utilities.only_if_module_is_available(modnames, pkgname=None, url=None)

Decorator to guard functions/methods against missing required module with specified name.

easybuild.tools.utilities.quote_py_str(val)

Version of quote_str specific for generating use in Python context (e.g., easyconfig parameters).

easybuild.tools.utilities.quote_str(val, escape_newline=False, prefer_single_quotes=False, escape_backslash=False, tcl=False)

Obtain a new value to be used in string replacement context.

For non-string values, it just returns the exact same value.

For string values, it tries to escape the string in quotes, e.g., foo becomes ‘foo’, foo’bar becomes “foo’bar”, foo’bar”baz becomes “””foo’bar”baz”””, etc.

Parameters:
  • escape_newline – wrap strings that include a newline in triple quotes
  • prefer_single_quotes – if possible use single quotes
  • escape_backslash – escape backslash characters in the string
  • tcl – Boolean for whether we are quoting for Tcl syntax
easybuild.tools.utilities.remove_unwanted_chars(inputstring)

Remove unwanted characters from the given string and return a copy

All non-letter and non-numeral characters are considered unwanted except for underscore (‘_’).

easybuild.tools.utilities.shell_quote(token)

Wrap provided token in single quotes (to escape space and characters with special meaning in a shell), so it can be used in a shell command. This results in token that is not expanded/interpolated by the shell.

easybuild.tools.utilities.time2str(delta)

Return string representing provided datetime.timedelta value in human-readable form.

easybuild.tools.utilities.trace_msg(message, silent=False)

Print trace message.