1 from __future__
import division
, absolute_import
, unicode_literals
6 __all__
= ('decorator', 'memoize', 'interruptable')
9 def decorator(caller
, func
=None):
11 Create a new decorator
13 decorator(caller) converts a caller function into a decorator;
14 decorator(caller, func) decorates a function using a caller.
19 @functools.wraps(caller
)
20 def _decorator(f
, *dummy_args
, **dummy_opts
):
22 def _caller(*args
, **opts
):
23 return caller(f
, *args
, **opts
)
25 _decorator
.func
= caller
28 # return a decorated function
29 @functools.wraps(func
)
30 def _decorated(*args
, **opts
):
31 return caller(func
, *args
, **opts
)
32 _decorated
.func
= func
38 A decorator for memoizing function calls
40 http://en.wikipedia.org/wiki/Memoization
44 return decorator(_memoize
, func
)
47 def _memoize(func
, *args
, **opts
):
48 """Implements memoized cache lookups"""
49 if opts
: # frozenset is used to ensure hashability
50 key
= (args
, frozenset(list(opts
.items())))
53 cache
= func
.cache
# attribute added by memoize
57 result
= cache
[key
] = func(*args
, **opts
)
62 def interruptable(func
, *args
, **opts
):
63 """Handle interruptable system calls
65 OSX and others are known to interrupt system calls
67 http://en.wikipedia.org/wiki/PCLSRing
68 http://en.wikipedia.org/wiki/Unix_philosophy#Worse_is_better
70 The @interruptable decorator handles this situation
75 result
= func(*args
, **opts
)
77 if e
.errno
== errno
.EINTR
:
81 if e
.errno
in (errno
.EINTR
, errno
.EINVAL
):