-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeco.py
More file actions
163 lines (125 loc) · 3.3 KB
/
deco.py
File metadata and controls
163 lines (125 loc) · 3.3 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
#!/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
'''
@decorator(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
def decorator(dec):
'''
Decorate a decorator so that it inherits the docstrings
and stuff from the function it's decorating.
'''
def decorated(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
update_wrapper(wrapper, decorated)
return wrapper
update_wrapper(decorated, dec)
return decorated
def countcalls(func):
'''Decorator that counts calls made to the function decorated.'''
@decorator(func)
def wrapper(*args):
wrapper.calls += 1
return func(*args)
wrapper.calls = 0
return wrapper
def memo(func):
'''
Memoize a function so that it caches all return values for
faster future lookups.
'''
cache = dict()
@decorator(func)
def wrapper(*args):
try:
key = tuple(args)
if key not in cache:
cache[key] = func(*args)
return cache[key]
except TypeError:
return func(*args)
return wrapper
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.
'''
@decorator(func)
def wrapper(*args):
if len(args) == 1:
return args[0]
if len(args) == 2:
return func(*args)
return func(args[0], wrapper(*args[1:]))
return wrapper
def trace(trace_spacer):
'''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 tracer(func):
func.trace_spacer = ""
@decorator(func)
def wrapper(*args):
arg_str = ', '.join(list(map(str, args)))
spacer = wrapper.trace_spacer
print(f"{spacer} --> {func.__name__}({arg_str})")
spacer = spacer + trace_spacer
wrapper.trace_spacer = spacer
res = func(*args)
spacer = spacer[:-len(trace_spacer)]
wrapper.trace_spacer = spacer
print(f"{spacer} <-- {func.__name__}({arg_str}) == {res}")
return res
return wrapper
return tracer
@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__)
print(fib(3))
print(fib.calls, 'calls made')
if __name__ == '__main__':
main()