Created
January 14, 2012 18:30
-
-
Save rdeaton/1612405 to your computer and use it in GitHub Desktop.
Spyral SmartMemoize fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __call__(self, *args): | |
from spyral import director | |
frame = director.get_tick() | |
if director.get_scene() is not self.scene: | |
self.scene = director.get_scene() | |
self.cache = {} | |
if frame - self.last_clear > 100: | |
self.cache = dict((key,value) for key,value in self.cache.items() if frame - value[1] > 250) | |
self.last_clear = frame | |
try: | |
data, oldframe = self.cache[args] | |
self.cache[args] = (data, frame) | |
return data | |
except KeyError: | |
res = self.func(*args) | |
self.cache[args] = (res, frame) | |
return res | |
except TypeError: | |
print "WARNING: Unhashable type passed to memoize2. Reconsider using this decorator" | |
return self.func(*args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment