Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def runme(arxiv, expected=None):
pm = picklemap(serializer='dill')
@memoized(cache=arxiv, keymap=pm)
def doit(x):
return x
doit(1)
doit('2')
doit(data)
doit(lambda x:x**2)
doit.load()
doit.dump()
c = doit.__cache__()
r = getattr(c, '__archive__', '')
info = doit.info()
ck = c.keys()
#FIXME: hdfdir_archive fails with serialized=False in python 3.x
]
maps = [
None,
keymap(typed=False, flat=True, sentinel=NOSENTINEL),
keymap(typed=False, flat=False, sentinel=NOSENTINEL),
keymap(typed=True, flat=False, sentinel=NOSENTINEL),
hashmap(typed=False, flat=True, sentinel=NOSENTINEL),
hashmap(typed=False, flat=False, sentinel=NOSENTINEL),
hashmap(typed=True, flat=True, sentinel=NOSENTINEL),
hashmap(typed=True, flat=False, sentinel=NOSENTINEL),
stringmap(typed=False, flat=True, sentinel=NOSENTINEL),
stringmap(typed=False, flat=False, sentinel=NOSENTINEL),
stringmap(typed=True, flat=True, sentinel=NOSENTINEL),
stringmap(typed=True, flat=False, sentinel=NOSENTINEL),
picklemap(typed=False, flat=True, sentinel=NOSENTINEL),
picklemap(typed=False, flat=False, sentinel=NOSENTINEL),
picklemap(typed=True, flat=True, sentinel=NOSENTINEL),
picklemap(typed=True, flat=False, sentinel=NOSENTINEL),
]
for mapper in maps:
#print (mapper)
func = [_test_cache(cache, mapper) for cache in archives]
_cleanup()
for f in func:
#print (f.info())
assert f.info().hit + f.info().miss + f.info().load == N
#hashmap(typed=False, flat=True, sentinel=SENTINEL),
#hashmap(typed=False, flat=False, sentinel=SENTINEL),
#hashmap(typed=True, flat=True, sentinel=SENTINEL),
#hashmap(typed=True, flat=False, sentinel=SENTINEL),
stringmap(typed=False, flat=True, sentinel=NOSENTINEL),
stringmap(typed=False, flat=False, sentinel=NOSENTINEL),
stringmap(typed=True, flat=True, sentinel=NOSENTINEL),
stringmap(typed=True, flat=False, sentinel=NOSENTINEL),
#stringmap(typed=False, flat=True, sentinel=SENTINEL),
#stringmap(typed=False, flat=False, sentinel=SENTINEL),
#stringmap(typed=True, flat=True, sentinel=SENTINEL),
#stringmap(typed=True, flat=False, sentinel=SENTINEL),
picklemap(typed=False, flat=True, sentinel=NOSENTINEL),
picklemap(typed=False, flat=False, sentinel=NOSENTINEL),
picklemap(typed=True, flat=True, sentinel=NOSENTINEL),
picklemap(typed=True, flat=False, sentinel=NOSENTINEL),
#picklemap(typed=False, flat=True, sentinel=SENTINEL),
#picklemap(typed=False, flat=False, sentinel=SENTINEL),
#picklemap(typed=True, flat=True, sentinel=SENTINEL),
#picklemap(typed=True, flat=False, sentinel=SENTINEL),
]
#XXX: should have option to serialize value (as well as key) ?
for mapper in maps:
#print (mapper)
func = [_test_cache(cache, mapper) for cache in archives]
_cleanup()
for f in func:
#print (f.info())
assert f.info().hit + f.info().miss + f.info().load == N
new: (1,), {}
64
>>> s.eggs(1)
64
>>> s.eggs(1, bar='spam')
new: (1,), {'bar': 'spam'}
78
>>> s2 = Spam()
>>> s2.eggs(1, bar='spam')
78
"""
from klepto.safe import inf_cache as memoized
#from klepto import inf_cache as memoized
from klepto.keymaps import picklemap
dumps = picklemap(flat=False, serializer='dill')
class Spam(object):
"""A simple class with a memoized method"""
@memoized(keymap=dumps, ignore='self')
def eggs(self, *args, **kwds):
#print ('new:', args, kwds)
from random import random
return int(100 * random())
def test_classmethod():
s = Spam()
assert s.eggs() == s.eggs()
assert s.eggs(1) == s.eggs(1)
s2 = Spam()
assert s.eggs(1, bar='spam') == s2.eggs(1, bar='spam')
maps = [
None,
keymap(typed=False, flat=True, sentinel=NOSENTINEL),
keymap(typed=False, flat=False, sentinel=NOSENTINEL),
keymap(typed=True, flat=False, sentinel=NOSENTINEL),
hashmap(typed=False, flat=True, sentinel=NOSENTINEL),
hashmap(typed=False, flat=False, sentinel=NOSENTINEL),
hashmap(typed=True, flat=True, sentinel=NOSENTINEL),
hashmap(typed=True, flat=False, sentinel=NOSENTINEL),
stringmap(typed=False, flat=True, sentinel=NOSENTINEL),
stringmap(typed=False, flat=False, sentinel=NOSENTINEL),
stringmap(typed=True, flat=True, sentinel=NOSENTINEL),
stringmap(typed=True, flat=False, sentinel=NOSENTINEL),
picklemap(typed=False, flat=True, sentinel=NOSENTINEL),
picklemap(typed=False, flat=False, sentinel=NOSENTINEL),
picklemap(typed=True, flat=True, sentinel=NOSENTINEL),
picklemap(typed=True, flat=False, sentinel=NOSENTINEL),
]
for mapper in maps:
#print (mapper)
func = [_test_cache(cache, mapper) for cache in archives]
_cleanup()
for f in func:
#print (f.info())
assert f.info().hit + f.info().miss + f.info().load == N
hashmap(typed=False, flat=False, sentinel=NOSENTINEL),
hashmap(typed=True, flat=True, sentinel=NOSENTINEL),
hashmap(typed=True, flat=False, sentinel=NOSENTINEL),
#hashmap(typed=False, flat=True, sentinel=SENTINEL),
#hashmap(typed=False, flat=False, sentinel=SENTINEL),
#hashmap(typed=True, flat=True, sentinel=SENTINEL),
#hashmap(typed=True, flat=False, sentinel=SENTINEL),
stringmap(typed=False, flat=True, sentinel=NOSENTINEL),
stringmap(typed=False, flat=False, sentinel=NOSENTINEL),
stringmap(typed=True, flat=True, sentinel=NOSENTINEL),
stringmap(typed=True, flat=False, sentinel=NOSENTINEL),
#stringmap(typed=False, flat=True, sentinel=SENTINEL),
#stringmap(typed=False, flat=False, sentinel=SENTINEL),
#stringmap(typed=True, flat=True, sentinel=SENTINEL),
#stringmap(typed=True, flat=False, sentinel=SENTINEL),
picklemap(typed=False, flat=True, sentinel=NOSENTINEL),
picklemap(typed=False, flat=False, sentinel=NOSENTINEL),
picklemap(typed=True, flat=True, sentinel=NOSENTINEL),
picklemap(typed=True, flat=False, sentinel=NOSENTINEL),
#picklemap(typed=False, flat=True, sentinel=SENTINEL),
#picklemap(typed=False, flat=False, sentinel=SENTINEL),
#picklemap(typed=True, flat=True, sentinel=SENTINEL),
#picklemap(typed=True, flat=False, sentinel=SENTINEL),
]
#XXX: should have option to serialize value (as well as key) ?
for mapper in maps:
#print (mapper)
func = [_test_cache(cache, mapper) for cache in archives]
_cleanup()
for f in func: