1 __all__
= ('decorator', 'deprecated', 'memoize')
4 def decorator(caller
, func
=None):
6 decorator(caller) converts a caller function into a decorator;
7 decorator(caller, func) decorates a function using a caller.
9 if func
is None: # returns a decorator
10 def _decorator(f
, *args
, **opts
):
11 def _caller(*args
, **opts
):
12 return caller(f
, *args
, **opts
)
16 else: # returns a decorated function
17 def _decorator(*args
, **opts
):
18 return caller(func
, *args
, **opts
)
23 def deprecated(func
, *args
, **kw
):
24 "A decorator for deprecated functions"
26 warnings
.warn('Calling deprecated function %r' % func
.__name
__,
27 DeprecationWarning, stacklevel
=3)
28 return func(*args
, **kw
)
33 A decorator for memoizing function calls
35 http://en.wikipedia.org/wiki/Memoization
39 return decorator(_memoize
, func
)
42 def _memoize(func
, *args
, **opts
):
43 """Implements memoized cache lookups"""
44 if opts
: # frozenset is used to ensure hashability
45 key
= args
, frozenset(opts
.items())
48 cache
= func
.cache
# attribute added by memoize
52 cache
[key
] = result
= func(*args
, **opts
)