pysap.base.utils#

Modules that defines usefull tools.

with_metaclass(meta, *bases)[source]#

Function from jinja2/_compat.py.

License: BSD.

Use it like this:

class BaseForm(object):
    pass

class FormType(type):
    pass

class Form(with_metaclass(FormType, BaseForm)):
    pass

This requires a bit of explanation: the basic idea is to make a dummy metaclass for one level of class instantiation that replaces itself with the actual metaclass. Because of internal type checks we also need to make sure that we downgrade the custom metaclass for one level to something closer to type (that’s why __call__ and __init__ comes back from type etc.).

This has the advantage over six.with_metaclass of not introducing dummy classes into the final MRO.

monkeypatch(klass, methodname=None)[source]#

Decorator extending class with the decorated callable.

>>> class A:
...     pass
>>> @monkeypatch(A)
... def meth(self):
...     return 12
...
>>> a = A()
>>> a.meth()
12
>>> @monkeypatch(A, 'foo')
... def meth(self):
...     return 12
...
>>> a.foo()
12
Parameters
  • klass (class object) – the class to be decorated.

  • methodname (str, default None) – the name of the decorated method. If None, use the function name.

Returns

decorator – the decorator.

Return type

callable

flatten(x)[source]#

Flatten list an array.

Parameters

x (list or numpy.ndarray) – the input dataset.

Returns

  • y (numpy.ndarray 1D) – the flatten input list of array.

  • shape (list of uplet) – the input list of array structure.

unflatten(y, shape)[source]#

Unflatten a flattened array.

Parameters
  • y (numpy.ndarray 1D) – a flattened input array.

  • shape (list of uplet) – the output structure information.

Returns

x – the unflattened dataset.

Return type

list