44@author: piranna
55'''
66
7+ try :
8+ from collections import OrderedDict
9+ except ImportError :
10+ OrderedDict = None
11+
12+
13+ if OrderedDict :
14+ class Cache (OrderedDict ):
15+ """Cache with LRU algorithm using an OrderedDict as basis
16+ """
17+ def __init__ (self , maxsize = 100 ):
18+ OrderedDict .__init__ (self )
19+
20+ self ._maxsize = maxsize
21+
22+ def __getitem__ (self , key , * args , ** kwargs ):
23+ # Get the key and remove it from the cache, or raise KeyError
24+ value = OrderedDict .__getitem__ (self , key )
25+ del self [key ]
26+
27+ # Insert the (key, value) pair on the front of the cache
28+ OrderedDict .__setitem__ (self , key , value )
29+
30+ # Return the value from the cache
31+ return value
32+
33+ def __setitem__ (self , key , value , * args , ** kwargs ):
34+ # Key was inserted before, remove it so we put it at front later
35+ if key in self :
36+ del self [key ]
37+
38+ # Too much items on the cache, remove the least recent used
39+ elif len (self ) >= self ._maxsize :
40+ self .popitem (False )
41+
42+ # Insert the (key, value) pair on the front of the cache
43+ OrderedDict .__setitem__ (self , key , value , * args , ** kwargs )
44+
45+ else :
46+ class Cache (dict ):
47+ """Cache that reset when gets full
48+ """
49+ def __init__ (self , maxsize = 100 ):
50+ dict .__init__ (self )
51+
52+ self ._maxsize = maxsize
53+
54+ def __setitem__ (self , key , value , * args , ** kwargs ):
55+ # Reset the cache if we have too much cached entries and start over
56+ if len (self ) >= self ._maxsize :
57+ self .clear ()
58+
59+ # Insert the (key, value) pair on the front of the cache
60+ dict .__setitem__ (self , key , value , * args , ** kwargs )
61+
762
863def memoize_generator (func ):
964 """Memoize decorator for generators
@@ -13,25 +68,18 @@ def memoize_generator(func):
1368 Obviusly, this is only useful if the generator will always return the same
1469 values for each specific parameters...
1570 """
16- cache = {}
71+ cache = Cache ()
1772
1873 def wrapped_func (* args , ** kwargs ):
1974# params = (args, kwargs)
2075 params = (args , tuple (sorted (kwargs .items ())))
2176
2277 # Look if cached
2378 try :
24- print params
2579 cached = cache [params ]
2680
2781 # Not cached, exec and store it
2882 except KeyError :
29- # Reset the cache if we have too much cached entries and start over
30- # In the future would be better to use an OrderedDict and drop the
31- # Least Recent Used entries
32- if len (cache ) >= 10 :
33- cache .clear ()
34-
3583 cached = []
3684
3785 for item in func (* args , ** kwargs ):
0 commit comments