Helper functions and classes — bornprofiler.utilities

The module defines some convenience functions and classes that are used in other modules.

Classes

class bornprofiler.utilities.AttributeDict[source]

A dictionary with pythonic access to keys as attributes — useful for interactive work.

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 None if any of the args is None.

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 = False is set)
  • At the end or after an exception code always returns to the directory that was the current directory before entering the block.
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 True if 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:

Unlink path but do not complain if file does not exist.