-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathdecorators.py
59 lines (50 loc) · 1.74 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import functools
import threading
import warnings
class combomethod:
def __init__(self, method):
self.method = method
def __get__(self, obj=None, objtype=None):
@functools.wraps(self.method)
def _wrapper(*args, **kwargs):
if obj is not None:
return self.method(obj, *args, **kwargs)
else:
return self.method(objtype, *args, **kwargs)
return _wrapper
def reject_recursive_repeats(to_wrap):
'''
Prevent simple cycles by returning None when called recursively with same instance
'''
to_wrap.__already_called = {}
@functools.wraps(to_wrap)
def wrapped(*args):
arg_instances = tuple(map(id, args))
thread_id = threading.get_ident()
thread_local_args = (thread_id,) + arg_instances
if thread_local_args in to_wrap.__already_called:
raise ValueError('Recursively called %s with %r' % (to_wrap, args))
to_wrap.__already_called[thread_local_args] = True
try:
wrapped_val = to_wrap(*args)
finally:
del to_wrap.__already_called[thread_local_args]
return wrapped_val
return wrapped
def deprecated_for(replace_message):
'''
Decorate a deprecated function, with info about what to use instead, like:
@deprecated_for("toBytes()")
def toAscii(arg):
...
'''
def decorator(to_wrap):
@functools.wraps(to_wrap)
def wrapper(*args, **kwargs):
warnings.warn(
"%s is deprecated in favor of %s" % (to_wrap.__name__, replace_message),
category=DeprecationWarning,
stacklevel=2)
return to_wrap(*args, **kwargs)
return wrapper
return decorator