def test_combinations(): seed(1234) # random seed #XXX: archive/cache should allow scalar and list, also dict (as new table) ? dicts = [ {}, { 'a': 1 }, { 'a': [1, 2] }, { 'a': { 'x': 3 } }, ] init = dicts[0] archives = [ hdf_archive('memo.hdf5', init, serialized=True, meta=False), hdf_archive('memo.h5', init, serialized=False, meta=False), hdf_archive('xxxx.hdf5', init, serialized=True, meta=True), hdf_archive('xxxx.h5', init, serialized=False, meta=True), hdfdir_archive('memoq', init, serialized=False, meta=False), hdfdir_archive('memor', init, serialized=True, meta=False), hdfdir_archive('memos', init, serialized=False, meta=True), hdfdir_archive('memot', init, serialized=True, meta=True), ] if tuple(int(i) for i in h5py.__version__.split('.', 2)) < (3, 0, 0): #FIXME: hdfdir_archive fails with serialized=False in python 3.x archives = archives[:4] + archives[5::2] 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
def test_combinations(): seed(1234) # random seed #XXX: archive/cache should allow scalar and list, also dict (as new table) ? dicts = [ {}, {'a':1}, {'a':[1,2]}, {'a':{'x':3}}, ] init = dicts[0] archives = [ hdf_archive('memo.hdf5',init,serialized=True,meta=False), hdf_archive('memo.h5',init,serialized=False,meta=False), hdf_archive('xxxx.hdf5',init,serialized=True,meta=True), hdf_archive('xxxx.h5',init,serialized=False,meta=True), # hdfdir_archive('memoq',init,serialized=False,meta=False), hdfdir_archive('memor',init,serialized=True,meta=False), # hdfdir_archive('memos',init,serialized=False,meta=True), hdfdir_archive('memot',init,serialized=True,meta=True), #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
def test_keygen_foo(): assert foo(0, 1, 2) == ('x', NULL, 'y', 1, 'z', 2) assert foo.valid() == True assert foo(10, 1, 2) == ('x', NULL, 'y', 1, 'z', 2) assert foo(0, 1) == ('x', NULL, 'y', 1, 'z', 2) assert foo(0, 1, 3) == ('x', NULL, 'y', 1, 'z', 3) assert foo(0, 1, r=3) == ('x', NULL, 'y', 1, 'z', 2) assert foo.valid() == False assert foo(0, 1, x=1) == ('x', NULL, 'y', 1, 'z', 2) assert foo.valid() == False res2 = ('x', NULL, 'y', 2, 'z', 10) assert foo(10, y=2, z=10) == res2 assert foo.valid() == True res1 = ('x', NULL, 'y', 1, 'z', 10) assert foo(0, 1, z=10) == res1 assert foo.valid() == True assert foo.call() == 11 h = hashmap(algorithm='md5') foo.register(h) if hex(sys.hexversion) < '0x30300f0': _hash1 = '2c8d801f4078eba873a5fb6909ab0f8d' _hash2 = '949883b97d9fda9c8fe6bd468fe90af9' else: # python 3.3 has hash randomization, apparently from klepto.crypto import hash _hash1 = hash(res1, 'md5') _hash2 = hash(res2, 'md5') assert foo(0, 1, z=10) == _hash1 assert str(foo.keymap()) == str(h) assert foo.key() == _hash1 assert foo(10, y=1, z=10) == _hash1 assert foo(10, y=2, z=10) == _hash2
def test_keygen_foo(): assert foo(0, 1, 2) == ("x", NULL, "y", 1, "z", 2) assert foo.valid() == True assert foo(10, 1, 2) == ("x", NULL, "y", 1, "z", 2) assert foo(0, 1) == ("x", NULL, "y", 1, "z", 2) assert foo(0, 1, 3) == ("x", NULL, "y", 1, "z", 3) assert foo(0, 1, r=3) == ("x", NULL, "y", 1, "z", 2) assert foo.valid() == False assert foo(0, 1, x=1) == ("x", NULL, "y", 1, "z", 2) assert foo.valid() == False res2 = ("x", NULL, "y", 2, "z", 10) assert foo(10, y=2, z=10) == res2 assert foo.valid() == True res1 = ("x", NULL, "y", 1, "z", 10) assert foo(0, 1, z=10) == res1 assert foo.valid() == True assert foo.call() == 11 h = hashmap(algorithm="md5") foo.register(h) if hex(sys.hexversion) < "0x30300f0": _hash1 = "2c8d801f4078eba873a5fb6909ab0f8d" _hash2 = "949883b97d9fda9c8fe6bd468fe90af9" else: # python 3.3 has hash randomization, apparently from klepto.crypto import hash _hash1 = hash(res1, "md5") _hash2 = hash(res2, "md5") assert foo(0, 1, z=10) == _hash1 assert str(foo.keymap()) == str(h) assert foo.key() == _hash1 assert foo(10, y=1, z=10) == _hash1 assert foo(10, y=2, z=10) == _hash2
def test_keygen_foo(): assert foo(0,1,2) == ('x', NULL, 'y', 1, 'z', 2) assert foo.valid() == True assert foo(10,1,2) == ('x', NULL, 'y', 1, 'z', 2) assert foo(0,1) == ('x', NULL, 'y', 1, 'z', 2) assert foo(0,1,3) == ('x', NULL, 'y', 1, 'z', 3) assert foo(0,1,r=3) == ('x', NULL, 'y', 1, 'z', 2) assert foo.valid() == False assert foo(0,1,x=1) == ('x', NULL, 'y', 1, 'z', 2) assert foo.valid() == False res2 = ('x', NULL, 'y', 2, 'z', 10) assert foo(10,y=2,z=10) == res2 assert foo.valid() == True res1 = ('x', NULL, 'y', 1, 'z', 10) assert foo(0,1,z=10) == res1 assert foo.valid() == True assert foo.call() == 11 h = hashmap(algorithm='md5') foo.register(h) if hex(sys.hexversion) < '0x30300f0': _hash1 = '2c8d801f4078eba873a5fb6909ab0f8d' _hash2 = '949883b97d9fda9c8fe6bd468fe90af9' else: # python 3.3 has hash randomization, apparently from klepto.crypto import hash _hash1 = hash(res1, 'md5') _hash2 = hash(res2, 'md5') assert foo(0,1,z=10) == _hash1 assert str(foo.keymap()) == str(h) assert foo.key() == _hash1 assert foo(10,y=1,z=10) == _hash1 assert foo(10,y=2,z=10) == _hash2
#!/usr/bin/env python # # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) # Copyright (c) 2013-2016 California Institute of Technology. # Copyright (c) 2016-2020 The Uncertainty Quantification Foundation. # License: 3-clause BSD. The full license text is available at: # - https://github.com/uqfoundation/klepto/blob/master/LICENSE from klepto.keymaps import hashmap from klepto import lru_cache as memoize from klepto import inf_cache from klepto import keygen hasher = hashmap(algorithm='md5') class Adder(object): """A simple class with a memoized method""" @memoize(keymap=hasher, ignore=('self', '**')) def __call__(self, x, *args, **kwds): debug = kwds.get('debug', False) if debug: print('debug:', x, args, kwds) return sum((x, ) + args) add = __call__ def test_adder(): add = Adder() assert add(2, 0) == 2
#!/usr/bin/env python # # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) # Copyright (c) 2013-2015 California Institute of Technology. # License: 3-clause BSD. The full license text is available at: # - http://trac.mystic.cacr.caltech.edu/project/pathos/browser/klepto/LICENSE from klepto import lru_cache as memoize from klepto.keymaps import hashmap hasher = hashmap(algorithm='md5') class Adder(object): """A simple class with a memoized method""" @memoize(keymap=hasher, ignore=('self','**')) def __call__(self, x, *args, **kwds): debug = kwds.get('debug', False) if debug: print ('debug:', x, args, kwds) return sum((x,)+args) add = __call__ add = Adder() assert add(2,0) == 2 assert add(2,0,z=4) == 2 # cached (ignore z) assert add(2,0,debug=False) == 2 # cached (ignore debug) assert add(1,2,debug=False) == 3 assert add(1,2,debug=True) == 3 # cached (ignore debug) assert add(4) == 4 assert add(x=4) == 4 # cached
#XXX: when running a single map, there should be 3 possible results: # 1) flat=False may produce unhashable keys: all misses # 2) typed=False doesn't distinguish float & int: more hits & loads # 3) typed=True distingushes float & int: less hits & loads #XXX: due to the seed, each of the 3 cases should yield the same results maps = [ None, keymap(typed=False, flat=True, sentinel=NOSENTINEL), keymap(typed=False, flat=False, sentinel=NOSENTINEL), keymap(typed=True, flat=True, sentinel=NOSENTINEL), keymap(typed=True, flat=False, sentinel=NOSENTINEL), #keymap(typed=False, flat=True, sentinel=SENTINEL), #keymap(typed=False, flat=False, sentinel=SENTINEL), #keymap(typed=True, flat=True, sentinel=SENTINEL), #keymap(typed=True, flat=False, sentinel=SENTINEL), 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), #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),
def test_combinations(): seed(1234) # random seed #XXX: archive/cache should allow scalar and list, also dict (as new table) ? dicts = [ {}, {'a':1}, {'a':[1,2]}, {'a':{'x':3}}, ] init = dicts[0] archives = [ null_archive(None,init), dict_archive(None,init), file_archive(None,init,serialized=True), file_archive(None,init,serialized=False), file_archive('xxxx.pkl',init,serialized=True), file_archive('xxxx.py',init,serialized=False), dir_archive('memoi',init,serialized=False), dir_archive('memop',init,serialized=True), dir_archive('memoj',init,serialized=True,fast=True), dir_archive('memoz',init,serialized=True,compression=1), dir_archive('memom',init,serialized=True,memmode='r+'), #sqltable_archive(None,init), #sqltable_archive('sqlite:///memo.db',init), #sqltable_archive('memo',init), #sql_archive(None,init), #sql_archive('sqlite:///memo.db',init), #sql_archive('memo',init), ] #FIXME: even 'safe' archives throw Error when cache.load, cache.dump fails # (often demonstrated in sqltable_archive, as barfs on tuple & dict) #XXX: when running a single map, there should be 3 possible results: # 1) flat=False may produce unhashable keys: all misses # 2) typed=False doesn't distinguish float & int: more hits & loads # 3) typed=True distingushes float & int: less hits & loads #XXX: due to the seed, each of the 3 cases should yield the same results maps = [ None, keymap(typed=False, flat=True, sentinel=NOSENTINEL), keymap(typed=False, flat=False, sentinel=NOSENTINEL), #FIXME: keymap of (typed=True,flat=True) fails w/ dir_archive on Windows b/c # keymap(typed=True, flat=True, sentinel=NOSENTINEL), # bad directory name? keymap(typed=True, flat=False, sentinel=NOSENTINEL), #keymap(typed=False, flat=True, sentinel=SENTINEL), #keymap(typed=False, flat=False, sentinel=SENTINEL), #keymap(typed=True, flat=True, sentinel=SENTINEL), #keymap(typed=True, flat=False, sentinel=SENTINEL), 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), #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
# 1) flat=False may produce unhashable keys: all misses # 2) typed=False doesn't distinguish float & int: more hits & loads # 3) typed=True distingushes float & int: less hits & loads #XXX: due to the seed, each of the 3 cases should yield the same results maps = [ None, keymap(typed=False, flat=True, sentinel=NOSENTINEL), keymap(typed=False, flat=False, sentinel=NOSENTINEL), #FIXME: keymap of (typed=True,flat=True) fails w/ dir_archive on Windows b/c # keymap(typed=True, flat=True, sentinel=NOSENTINEL), # bad directory name? keymap(typed=True, flat=False, sentinel=NOSENTINEL), #keymap(typed=False, flat=True, sentinel=SENTINEL), #keymap(typed=False, flat=False, sentinel=SENTINEL), #keymap(typed=True, flat=True, sentinel=SENTINEL), #keymap(typed=True, flat=False, sentinel=SENTINEL), 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), #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),
def test_combinations(): seed(1234) # random seed #XXX: archive/cache should allow scalar and list, also dict (as new table) ? dicts = [ {}, { 'a': 1 }, { 'a': [1, 2] }, { 'a': { 'x': 3 } }, ] init = dicts[0] archives = [ null_archive(None, init), dict_archive(None, init), file_archive(None, init, serialized=True), file_archive(None, init, serialized=False), file_archive('xxxx.pkl', init, serialized=True), file_archive('xxxx.py', init, serialized=False), dir_archive('memoi', init, serialized=False), dir_archive('memop', init, serialized=True), dir_archive('memoj', init, serialized=True, fast=True), dir_archive('memoz', init, serialized=True, compression=1), dir_archive('memom', init, serialized=True, memmode='r+'), #sqltable_archive(None,init), #sqltable_archive('sqlite:///memo.db',init), #sqltable_archive('memo',init), #sql_archive(None,init), #sql_archive('sqlite:///memo.db',init), #sql_archive('memo',init), ] #FIXME: even 'safe' archives throw Error when cache.load, cache.dump fails # (often demonstrated in sqltable_archive, as barfs on tuple & dict) #XXX: when running a single map, there should be 3 possible results: # 1) flat=False may produce unhashable keys: all misses # 2) typed=False doesn't distinguish float & int: more hits & loads # 3) typed=True distingushes float & int: less hits & loads #XXX: due to the seed, each of the 3 cases should yield the same results maps = [ None, keymap(typed=False, flat=True, sentinel=NOSENTINEL), keymap(typed=False, flat=False, sentinel=NOSENTINEL), #FIXME: keymap of (typed=True,flat=True) fails w/ dir_archive on Windows b/c # keymap(typed=True, flat=True, sentinel=NOSENTINEL), # bad directory name? keymap(typed=True, flat=False, sentinel=NOSENTINEL), #keymap(typed=False, flat=True, sentinel=SENTINEL), #keymap(typed=False, flat=False, sentinel=SENTINEL), #keymap(typed=True, flat=True, sentinel=SENTINEL), #keymap(typed=True, flat=False, sentinel=SENTINEL), 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), #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