5 __all__
= ('decorator', 'memoize', 'interruptable')
8 def decorator(caller
, func
=None):
10 Create a new decorator
12 decorator(caller) converts a caller function into a decorator;
13 decorator(caller, func) decorates a function using a caller.
18 @functools.wraps(caller
)
19 def _decorator(func
, *dummy_args
, **dummy_opts
):
20 @functools.wraps(func
)
21 def _caller(*args
, **opts
):
22 return caller(func
, *args
, **opts
)
26 _decorator
.func
= caller
29 # return a decorated function
30 @functools.wraps(func
)
31 def _decorated(*args
, **opts
):
32 return caller(func
, *args
, **opts
)
34 _decorated
.func
= func
40 A decorator for memoizing function calls
42 https://en.wikipedia.org/wiki/Memoization
46 return decorator(_memoize
, func
)
49 def _memoize(func
, *args
, **opts
):
50 """Implements memoized cache lookups"""
51 if opts
: # frozenset is used to ensure hashability
52 key
= (args
, frozenset(list(opts
.items())))
55 cache
= func
.cache
# attribute added by memoize
59 result
= cache
[key
] = func(*args
, **opts
)
64 def interruptable(func
, *args
, **opts
):
65 """Handle interruptible system calls
67 macOS and others are known to interrupt system calls
69 https://en.wikipedia.org/wiki/PCLSRing
70 http://en.wikipedia.org/wiki/Unix_philosophy#Worse_is_better
72 The @interruptable decorator handles this situation
77 result
= func(*args
, **opts
)
79 if e
.errno
in (errno
.EINTR
, errno
.EINVAL
):