Here is the example in Alex Martelli Python Cookbook that show how to create a memoize decorator using cPickle for function that take mutable argument (original version) :
import cPickle
class MemoizeMutable:
def __init__(self, fn):
self.fn = fn
self.memo = {}
def __call__(self, *args, **kwds):
import cPickle
str = cPickle.dumps(args, 1)+cPickle.dumps(kwds, 1)
if not self.memo.has_key(str):
print "miss" # DEBUG INFO
self.memo[str] = self.fn(*args, **kwds)
else:
print "hit" # DEBUG INFO
return self.memo[str]
Here is a link.
EDIT: Using the code that you have given and this memoize decorator :
_level = MemoizeMutable(_level)
equirements = {'a': [],
'b': [],
'c': ['a'],
'd': ['a','b'],
'e': ['c','d'],
'f': ['e']
}
print uses_hierarchy(equirements)
i was able to reproduce this:
miss
miss
hit
miss
miss
hit
miss
hit
hit
hit
miss
hit
{0: ['a', 'b'], 1: ['c', 'd'], 2: ['e'], 3: ['f']}