Skip to content

Commit

Permalink
Allow memoization to disk for shared processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Suchow authored and mpacer committed Sep 27, 2015
1 parent c070354 commit a49aa8a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.egg-info
*.pyc
cached_func_calls/*
3 changes: 3 additions & 0 deletions proselint/checks/example_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
The first line always is always wrong.
"""
from proselint.reverse import reverse


def check(text):

reversed_text = reverse(text)

error_code = "PL000"
msg = "First line always has an error."

Expand Down
53 changes: 53 additions & 0 deletions proselint/memoize.py
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
6 changes: 6 additions & 0 deletions proselint/reverse.py
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]

0 comments on commit a49aa8a

Please sign in to comment.