Miscellaneous

Provides some utilities functions used by the shennong library

Those fonctions are not designed to be used by the end-user.

shennong.utils.get_njobs(njobs=None, log=<Logger null (WARNING)>)[source]

Returns the number of parallel jobs to run

The returned number of jobs is adapted from the input njobs value, considering the number of CPU cores available on the machine.

Parameters
  • njobs (int, optional) – The desired number of jobs to use, default to the number of cores available on the machine.

  • log (logging.Logger, optional) – A logger where to send messages, no logging by default.

Returns

njobs (int) – The returned value is min(njobs, ncpus).

Raises

ValueError – If njobs is not a strictly positive integer.

shennong.utils.list2array(seq)[source]

Converts lists in seq into numpy arrays

shennong.utils.array2list(seq)[source]

Converts numpy arrays in seq into lists

shennong.utils.dict_equal(dict1, dict2)[source]

Returns True if dict1 and dict2 are equals

The dictionnaries dict1 and dict2 can contain numpy arrays.

Parameters
  • dict1 (dict) – The first dictionnary to compare

  • dict2 (dict) – The second dictionnary to compare

Returns

equal (bool) – True if dict1 == dict2, False otherwise

shennong.utils.list_files_with_extension(directory, extension, abspath=False, realpath=True, recursive=True)[source]

Return all files of given extension in directory hierarchy

Parameters
  • directory (str) – The directory where to search for files

  • extension (str) – The extension of the targeted files (e.g. ‘.wav’)

  • abspath (bool, optional) – If True, return the absolute path to the file/link, default to False.

  • realpath (bool, optional) – If True, return resolved links, default to True.

  • recursive (bool, optional) – If True, list files in the whole subdirectories tree, if False just list the top-level directory, default to True.

Returns

files (list) – The files are returned in a sorted list with a path relative to ‘directory’, except if abspath or realpath is True

class shennong.utils.CatchExceptions(function)[source]

Bases: object

Decorator wrapping a function in a try/except block

When an exception occurs, display a user friendly message on standard output before exiting with error code 1.

The detected exceptions are ValueError, OSError, RuntimeError, AssertionError, KeyboardInterrupt and pkg_resources.DistributionNotFound.

Parameters

function – The function to wrap in a try/except block

static exit(msg)[source]

Write msg on stderr and exit with error code 1