-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeco.py
151 lines (120 loc) · 3.19 KB
/
deco.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import update_wrapper
def disable(func):
'''
Disable a decorator by re-assigning the decorator's name
to this function. For example, to turn off memoization:
>>> memo = disable
'''
return func
def decorator(deco):
'''
Decorate a decorator so that it inherits the docstrings
and stuff from the function it's decorating.
'''
def wrapper(func):
update_wrapper(deco, func)
return deco
return wrapper
def countcalls(func):
'''Decorator that counts calls made to the function decorated.'''
def countwrapper(*args):
countwrapper.calls += 1
return func(*args)
countwrapper.calls = 0
update_wrapper(countwrapper, func)
return countwrapper
def memo(func):
'''
Memoize a function so that it caches all return values for
faster future lookups.
'''
results = {}
def memowrapper(*args):
if not args in results:
results[args] = func(*args)
if hasattr(func, 'calls'):
setattr(memowrapper, 'calls', func.calls)
return results[args]
update_wrapper(memowrapper, func)
return memowrapper
def n_ary(func):
'''
Given binary function f(x, y), return an n_ary function such
that f(x, y, z) = f(x, f(y,z)), etc. Also allow f(x) = x.
'''
def wrapper(*args):
length = len(args)
if length > 2:
return func(args[0], wrapper(*args[1:]))
elif length == 2:
return func(args[0], args[1])
elif length == 1:
return args[0]
update_wrapper(wrapper, func)
return wrapper
def trace(s):
'''Trace calls made to function decorated.
@trace("____")
def fib(n):
....
>>> fib(3)
--> fib(3)
____ --> fib(2)
________ --> fib(1)
________ <-- fib(1) == 1
________ --> fib(0)
________ <-- fib(0) == 1
____ <-- fib(2) == 2
____ --> fib(1)
____ <-- fib(1) == 1
<-- fib(3) == 3
'''
def deco(func):
def tracewrapper(*args):
tracewrapper.i += 1
a = ', '.join(map(repr, args))
print ''.join([s * tracewrapper.i,
' --> ', func.__name__, '(', a, ')']
)
res = func(*args)
print ''.join([s * tracewrapper.i,
' <-- ', func.__name__, '(', a, ')', ' == ', str(res)]
)
tracewrapper.i -= 1
return res
update_wrapper(tracewrapper, func)
tracewrapper.i = -1
return tracewrapper
return deco
@memo
@countcalls
@n_ary
def foo(a, b):
return a + b
@countcalls
@memo
@n_ary
def bar(a, b):
return a * b
@countcalls
@trace("####")
@memo
def fib(n):
"""Some doc"""
return 1 if n <= 1 else fib(n-1) + fib(n-2)
def main():
print foo(4, 3)
print foo(4, 3, 2)
print foo(4, 3)
print "foo was called", foo.calls, "times"
print bar(4, 3)
print bar(4, 3, 2)
print bar(4, 3, 2, 1)
print "bar was called", bar.calls, "times"
print fib.__doc__
fib(3)
print fib.calls, 'calls made'
if __name__ == '__main__':
main()