Skip to content

utilities

Module with various utility functions

Authors:

  • Kenneth Hoste (Ghent University)

flatten(lst)

Flatten a list of lists.

get_class_for(modulepath, class_name)

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

PARAMETER DESCRIPTION
modulepath

Python module path (e.g., 'easybuild.base.generaloption')

class_name

Python class name (e.g., 'GeneralOption')

get_subclasses(klass, include_base_class=False)

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

get_subclasses_dict(klass, include_base_class=False)

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

import_available_modules(namespace)

Import all available module in the specified namespace.

PARAMETER DESCRIPTION
namespace

The namespace to import modules from.

mk_md_table(titles, columns)

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

mk_rst_table(titles, columns)

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

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)

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 DESCRIPTION

a new list with each element from list appearing only once (cfr. Michelle Dubois).

only_if_module_is_available(modnames, pkgname=None, url=None)

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

quote_py_str(val)

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

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.

PARAMETER DESCRIPTION
escape_newline

wrap strings that include a newline in triple quotes

DEFAULT: False

prefer_single_quotes

if possible use single quotes

DEFAULT: False

escape_backslash

escape backslash characters in the string

DEFAULT: False

tcl

Boolean for whether we are quoting for Tcl syntax

DEFAULT: False

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 ('_').

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.

time2str(delta)

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

trace_msg(message, silent=False)

Print trace message.