Function decorator that caches return values
If the decorated function is called with the same arguments a cached response is returned. This is useful for functions that does system calls, like talking over the network or accessing the filesystem and whose value wont be changed during the lifetime of the process:
@sy.util.memoize
def uname(arg):
print 'Getting uname', arg
return sy.cmd.outlines('uname -{}', arg)[0]
uname('r')
Getting uname r
uname('s')
Getting uname s
uname('r') # cached value returned
The decorator creates a pickle of the arguments and uses it as cache key. If the arguments cant be pickled it will throw a pickle.PickleError.
Decorator for retrying function if exception occurs:
@sy.util.retry
def errorprone():
raise Exception('fail')
Note
Hides all raised exceptions except the last one.
Parameters: |
|
---|
Todo
Not tested
alias of Exception