-
Notifications
You must be signed in to change notification settings - Fork 1
/
memoize.py
104 lines (93 loc) · 2.42 KB
/
memoize.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
import functools
__all__ = ['memoize', 'const_property']
class memoize(object):
"""
A decorator that adds memoization to any method, thereby avoiding
recomputation of the value.
"""
__memos = {}
class __nothing: pass
def __init__(self, fn):
self.fn = fn
def __method(self, target, *args):
"""
>>> class X(object):
... @memoize
... def f(self, x, y):
... print x, y
>>> a = X()
>>> for x in range(3):
... for y in range(3):
... a.f(x,y)
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
>>> for x in range(3):
... for y in range(3):
... a.f(x,y)
>>>
"""
attrname = '_memoize__' + self.fn.__name__
memos = target.__dict__.setdefault(attrname, {})
if args in memos:
return memos[args]
memos[args] = result = self.fn(target, *args)
return result
def __call__(self, *args):
"""
>>> @memoize
... def f(x, y):
... print x, y
...
>>> for x in range(3):
... for y in range(3):
... f(x,y)
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
>>> for x in range(3):
... for y in range(3):
... f(x,y)
>>>
"""
key = (self.fn,)+args
v = self.__memos.get(key, self.__nothing)
if v is self.__nothing:
v = self.__memos[key] = self.fn(*args)
return v
def __get__(self, obj, objtype):
# Support instance methods.
return functools.partial(self.__method, obj)
def const_property(fn):
"""
A decorator for lazily-computed, memoized, read-only properties.
class X(object):
@const_property
def some_attribute_name():
return compute_the_value();
a = X()
b = x.some_attribute_name # compute_the_value called here
c = x.some_attribute_name # compute_the_value not called again
"""
@property
def wrapped(self):
attrname = '_const_property__' + fn.__name__
if not hasattr(self, attrname):
setattr(self, attrname, fn(self))
return getattr(self, attrname)
return wrapped
if __name__ == '__main__':
import doctest
doctest.testmod()