-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow memoization to disk for shared processing
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
*.egg-info | ||
*.pyc | ||
cached_func_calls/* |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
import shelve | ||
import inspect | ||
import functools | ||
|
||
cache_dirname = 'cached_func_calls' | ||
|
||
|
||
def memoize(f): | ||
if not os.path.isdir(cache_dirname): | ||
os.mkdir(cache_dirname) | ||
print 'Created cache directory %s' \ | ||
% os.path.join(os.path.abspath(__file__), 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:] | ||
# argdict['classname'] = f.__self__.__class__ | ||
|
||
tempargdict = inspect.getcallargs(f, *args, **kwargs) | ||
|
||
# handle numpy arrays | ||
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: | ||
print 'Warning: could not disk cache call to %s; it probably has unhashable args' \ | ||
% (f.__module__ + '.' + f.__name__) | ||
return f(*args, **kwargs) | ||
|
||
return wrapped |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from memoize import memoize | ||
|
||
|
||
@memoize | ||
def reverse(text): | ||
return text[::-1] |