# typhon.utils¶

This module contains convenience functions for any purposes.

typhon.utils.extract_block_diag(M, n)[source]

Extract diagonal blocks from square Matrix.

Parameters: M (np.array) – Square matrix. n (int) – Number of blocks to extract.

Example

>>> foo = np.array([[ 1.,  1.,  0.,  0.],
... [ 1.,  1.,  0.,  0.],
... [ 0.,  0.,  2.,  2.],
... [ 0.,  0.,  2.,  2.]])
>>> extract_block_diag(foo, 2)
[array([[ 1.,  1.],
[ 1.,  1.]]), array([[ 2.,  2.],
[ 2.,  2.]])]

class typhon.utils.Timer(verbose=True)[source]

Bases: object

Provide a simple time profiling utility.

Timer class adapted from blog entry [0].

Parameters: verbose – Print results after stopping the timer.

Examples

Timer in with block:

>>> import time
>>> with Timer():
...     time.sleep(1)
elapsed time: 1.001s


Timer object:

>>> import time
>>> t = Timer.start()
>>> time.sleep(1)
>>> t.stop()
>>> print(t.secs)
elapsed time: 1.001s

start()[source]

Start timer.

stop()[source]

Stop timer.

typhon.utils.path_append(dirname, path='PATH')[source]

Append a directory to environment path variable.

Append entries to colon-separated variables (e.g. the system path). If the entry is already in the list, it is moved to the end. A path variable is set, if not existing at function call.

Parameters: dirname (str) – Directory to add to the path. path (str) – Name of the path variable to append to. Defaults to the system path ‘PATH’.
typhon.utils.path_prepend(dirname, path='PATH')[source]

Prepend a directory to environment path variable.

Append entries to colon-separated variables (e.g. the system path). If the entry is already in the list, it is moved to the end. A path variable is set, if not existing at function call.

Parameters: dirname (str) – Directory to add to the path. path (str) – Name of the path variable to append to. Defaults to the system path ‘PATH’.
typhon.utils.path_remove(dirname, path='PATH')[source]

Remove a directory from environment path variable.

Remove entries from colon-separated variables (e.g. the system path). If the path variable is not set, nothing is done.

Parameters: dirname (str) – Directory to add to the path. path (str) – Name of the path variable to append to. Defaults to the system path ‘PATH’.

## typhon.utils.metaclass¶

Contains metaclasses for typhon development and others

This module contains metaclasses that are used within typhon, but that may also be useful for others.

class typhon.utils.metaclass.AbstractDocStringInheritor[source]

Automatically inherit docstrings for abstract classes.

For details, see DocStringInheritor.

class typhon.utils.metaclass.DocStringInheritor[source]

Bases: type

Automatically inherit docstrings.

If overriding a method for a class with a DocStringInheritor metaclass, the subclass can omit the docstring. The docstring will be inherited from the superclass.

This is inspired by the default behaviour in MatlabⓇ. The implementation is inspired by Paul Mcguire, see http://groups.google.com/group/comp.lang.python/msg/26f7b4fcb4d66c95

## typhon.utils.cache¶

Utilities related to caching and memoisation

typhon.utils.cache.mutable_cache(maxsize=10)[source]

In-memory cache like functools.lru_cache but for any object

This is a re-implementation of functools.lru_cache. Unlike functools.lru_cache, it works for any objects, mutable or not. Therefore, it returns returns a copy and it is wrong if the mutable object has changed! Use with caution!

If you call the resulting function with a keyword argument ‘CLEAR_CACHE’, the cache will be cleared. Otherwise, cache is rotated when more than maxsize elements exist in the cache. Additionally, if you call the resulting function with NO_CACHE=True, it doesn’t cache at all. Be careful with functions returning large objects, such as reading routines for datasets like IASI. Everything is kept in RAM!

Parameters: maxsize (int) – Maximum number of return values to be remembered. New function that has caching implemented.