Package Biskit :: Module decorators
[hide private]
[frames] | no frames]

Module decorators

source code

Defines method decorators. Decorators are wrapping functions that can preceed the definition of methods (since Python 2.4, using the '@' symbol) and enforce, for example, type-checking, or static calls. Before each call, the decorator function replaces the original function by some modified version. The details are described in the Python 2.4 documentation.

Classes [hide private]
  Test
Test case

Functions [hide private]
  accept(*required, **optional)
Decorator function that enforces type checking on arguments of a method.
  synchronized(f)
Decorator function ensuring that parallel threads call the wrapped class method one after the other.

Function Details [hide private]

accept(*required, **optional)

source code 

Decorator function that enforces type checking on arguments of a method.

Example:
@accept( int, float, opt1=int, opt2=PDBModel )
def method( n, fraction, opt2=PDBModel(), **args ):
    ...
The leading 'self' argument of class methods is automatically accepted and the parent class of the method should thus not be given as first type. For keyword arguments, None is always an accepted value.

synchronized(f)

source code 

Decorator function ensuring that parallel threads call the wrapped class method one after the other. That means, it is guaranteed that this method of a given object is never executed in parallel. However, different instances of the same class are not blocked and can still call the routine in parallel.

Example:
 @synchronized
 def open_log_file( self, fname ):
     ...

Note:

The decorator adds (if not already present) a RLock object 'lock' and a Condition object 'lockMsg' to the object holding this method. The wrapped method can hence call self.lockMsg.wait() or self.lockMsg.notify/notifyAll() directly without any need for acquiring a lock or creating a self.lockMsg.

For the same reason synchronized can only be applied to methods of objects.