Package ivs :: Package aux :: Module decorators
[hide private]
[frames] | no frames]

Module decorators

source code

Various decorator functions

Classes [hide private]
    Common tools
  countcalls
Keeps track of the number of times a function is called.
  LogPrinter
LogPrinter class which serves to emulates a file object and logs whatever it gets sent to a Logger object at the INFO level.
Functions [hide private]
    Common tools
 
memoized(fctn)
Cache a function's return value each time it is called.
source code
 
clear_memoization(keys=None)
Clear contents of memory
source code
 
make_parallel(fctn)
Make a parallel version of a function.
source code
2-tuple
timeit(fctn)
Time a function.
source code
2-tuple
timeit_duration(fctn)
Time a function and return duration.
source code
 
retry(tries, delay=3, backoff=2)
Retry a function or method until it returns True.
source code
 
retry_http(tries, backoff=2, on_failure='error')
Retry a function or method reading from the internet until no socket or IOError is raised
source code
 
logprintinfo(func)
Wraps a method so that any calls made to print get logged instead
source code
 
filter_kwargs(fctn)
Remove keyword arguments which are not used by a function.
source code
    Disable decorator
 
disabled(func)
Disables the provided function
source code
    Extend/Reopen class decorator
 
extend(cls)
Decorator that allows you to add methods or attributes to an already existing class.
source code
 
class_extend(cls)
Similar as extend(cls), but instead of decorating a function, you use it to decorate a class.
source code
Variables [hide private]
  logger = logging.getLogger("DEC")
  memory = {}
Function Details [hide private]

memoized(fctn)

source code 

Cache a function's return value each time it is called. If called later with the same arguments, the cached value is returned, and not re-evaluated.

make_parallel(fctn)

source code 

Make a parallel version of a function.

This extends the function's arguments with one extra argument, which is a parallel array that collects the output of the function.

You have to decorate this function in turn with a function that calls the basic function, but with different arguments so to effectively make it parallel (e.g. in the frequency analysis case, this would be a function that calls the periodogram calculation with different f0 and fn.

timeit(fctn)

source code 

Time a function.

Returns: 2-tuple
output from func, duration of function

timeit_duration(fctn)

source code 

Time a function and return duration.

Returns: 2-tuple
output from func, duration of function

retry(tries, delay=3, backoff=2)

source code 

Retry a function or method until it returns True.

Delay sets the initial delay, and backoff sets how much the delay should lengthen after each failure. backoff must be greater than 1, or else it isn't really a backoff. tries must be at least 0, and delay greater than 0.

retry_http(tries, backoff=2, on_failure='error')

source code 

Retry a function or method reading from the internet until no socket or IOError is raised

delay sets the initial delay, and backoff sets how much the delay should lengthen after each failure. backoff must be greater than 1, or else it isn't really a backoff. tries must be at least 0, and delay greater than 0.

disabled(func)

source code 

Disables the provided function

use as follows:

  1. set a global enable flag for a decorator: >>> global_mydecorator_enable_flag = True
  2. toggle decorator right before the definition >>> state = mydecorator if global_mydecorator_enable_flag else disabled >>> @state

extend(cls)

source code 

Decorator that allows you to add methods or attributes to an already existing class. Inspired on the reopening of classes in Ruby

Example:

>>> @extend(SomeClassThatAlreadyExists)
>>> def some_method():
>>>     do stuff

Will add the method some_method to SomeClassThatAlreadyExists

class_extend(cls)

source code 

Similar as extend(cls), but instead of decorating a function, you use it to decorate a class. All attributes of that class will be added to cls. Use at own risk, results may vary!!!