Helper functions and classes — bornprofiler.utilities¶
The module defines some convenience functions and classes that are used in other modules.
Classes¶
Functions¶
Some additional convenience functions that deal with files and directories:
-
bornprofiler.utilities.openany(directory[, mode='r'])[source]¶ Context manager to open a compressed (bzip2, gzip) or plain file (uses
anyopen()).
-
bornprofiler.utilities.anyopen(datasource, mode='r')[source]¶ Open datasource (gzipped, bzipped, uncompressed) and return a stream.
Arguments: - datasource: a file or a stream
- mode: ‘r’ or ‘w’
-
bornprofiler.utilities.realpath(*args)[source]¶ Join all args and return the real path, rooted at /.
Expands ‘~’, ‘~user’, and environment variables such as $HOME.
Returns
Noneif any of the args isNone.
-
bornprofiler.utilities.in_dir(directory[, create=True])[source]¶ Context manager to execute a code block in a directory.
- The directory is created if it does not exist (unless
create =
Falseis set) - At the end or after an exception code always returns to the directory that was the current directory before entering the block.
- The directory is created if it does not exist (unless
create =
-
bornprofiler.utilities.find_first(filename, suffices=None)[source]¶ Find first filename with a suffix from suffices.
Arguments: - filename
base filename; this file name is checked first
- suffices
list of suffices that are tried in turn on the root of filename; can contain the ext separator (
os.path.extsep) or not
Returns: The first match or
None.
-
bornprofiler.utilities.withextsep(extensions)[source]¶ Return list in which each element is guaranteed to start with
os.path.extsep.
Functions that improve list processing and which do not treat strings as lists:
-
bornprofiler.utilities.iterable(obj)[source]¶ Returns
Trueif obj can be iterated over and is not a string.
-
bornprofiler.utilities.asiterable(obj)[source]¶ Returns obj so that it can be iterated over; a string is not treated as iterable
Functions that help handling files: