Created
March 5, 2015 09:20
memoization & module import issue
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
import os | |
import shelve | |
import functools | |
import inspect | |
def memoize(f): | |
"""Cache results of computations on disk.""" | |
path_of_this_file = os.path.dirname(os.path.realpath(__file__)) | |
cache_dirname = os.path.join(path_of_this_file, "cache") | |
if not os.path.isdir(cache_dirname): | |
os.mkdir(cache_dirname) | |
cache_filename = f.__module__ + "." + f.__name__ | |
cachepath = os.path.join(cache_dirname, cache_filename) | |
try: | |
cache = shelve.open(cachepath, protocol=2) | |
except: | |
print 'Could not open cache file %s, maybe name collision' % cachepath | |
cache = None | |
@functools.wraps(f) | |
def wrapped(*args, **kwargs): | |
argdict = {} | |
# handle instance methods | |
if hasattr(f, '__self__'): | |
args = args[1:] | |
tempargdict = inspect.getcallargs(f, *args, **kwargs) | |
for k, v in tempargdict.iteritems(): | |
argdict[k] = v | |
key = str(hash(frozenset(argdict.items()))) | |
try: | |
return cache[key] | |
except KeyError: | |
value = f(*args, **kwargs) | |
cache[key] = value | |
cache.sync() | |
return value | |
except TypeError: | |
call_to = f.__module__ + '.' + f.__name__ | |
print ['Warning: could not disk cache call to ', | |
'%s; it probably has unhashable args'] % (call_to) | |
return f(*args, **kwargs) | |
return wrapped | |
@memoize | |
def make_blob(text): | |
import textblob | |
return textblob.TextBlob(text) | |
if __name__ == '__main__': | |
make_blob("hello") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment