def test_memoized(): @memoized(cache=sql_archive()) def add(x,y): return x+y add(1,2) add(1,2) add(1,3) #print ("sql_cache = %s" % add.__cache__()) _key4 = '((), '+str({'y':3, 'x':1})+')' _key3 = '((), '+str({'y':2, 'x':1})+')' key4_ = '((), '+str({'x':1, 'y':3})+')' key3_ = '((), '+str({'x':1, 'y':2})+')' assert add.__cache__() == {_key4: 4, _key3: 3} or {key4_: 4, key3_: 3} @memoized(cache=dict_archive(cached=False)) # use archive backend 'direcly' def add(x,y): return x+y add(1,2) add(1,2) add(1,3) #print ("dict_cache = %s" % add.__cache__()) assert add.__cache__() == {_key4: 4, _key3: 3} or {key4_: 4, key3_: 3} @memoized(cache=dict()) def add(x,y): return x+y add(1,2) add(1,2) add(1,3) #print ("dict_cache = %s" % add.__cache__()) assert add.__cache__() == {_key4: 4, _key3: 3} or {key4_: 4, key3_: 3} @memoized(cache=add.__cache__()) def add(x,y): return x+y add(1,2) add(2,2) #print ("re_dict_cache = %s" % add.__cache__()) _key2 = '((), '+str({'y':2, 'x':2})+')' key2_ = '((), '+str({'x':2, 'y':2})+')' assert add.__cache__() == {_key4: 4, _key3: 3, _key2: 4} or {key4_: 4, key3_: 3, key2_: 4} @memoized(keymap=dumps) def add(x,y): return x+y add(1,2) add(1,2) add(1,3) #print ("pickle_dict_cache = %s" % add.__cache__()) _pkey4 = dill.dumps(eval(_key4)) _pkey3 = dill.dumps(eval(_key3)) pkey4_ = dill.dumps(eval(key4_)) pkey3_ = dill.dumps(eval(key3_)) assert add.__cache__() == {_pkey4: 4, _pkey3: 3} or {pkey4_: 4, pkey3_: 3}
#!/usr/bin/env python # # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) # Copyright (c) 2013-2016 California Institute of Technology. # Copyright (c) 2016-2022 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.archives import sqltable_archive as sql_archive #from klepto.archives import sql_archive #d = sql_archive('postgresql://*****:*****@localhost/defaultdb', cached=False) #d = sql_archive('mysql://*****:*****@localhost/defaultdb', cached=False) d = sql_archive(cached=False) try: import sqlalchemy __alchemy = True except ImportError: __alchemy = False def test_basic(): d['a'] = 1 d['b'] = '1' assert d['a'] == 1 assert d['b'] == '1' def test_alchemy(): if __alchemy: d['c'] = min
costD([1,2,3.1234], 3.9876)# == -32.94723372 costD([1,2,3.1234], 3.9876)# == -32.94723372 costD([1,2,3.1234], 3.6789)# == -32.94723372 costD([1,2,3.4321], 3.6789)# == -32.94723372 assert costD.info().hit == 3 assert costD.info().miss == 1 assert costD.info().load == 0 #print ("") except ImportError: pass import sys import dill from klepto.archives import cache, sql_archive, dict_archive @memoized(cache=sql_archive()) def add(x,y): return x+y add(1,2) add(1,2) add(1,3) #print ("sql_cache = %s" % add.__cache__()) _key4 = '((), '+str({'y':3, 'x':1})+')' _key3 = '((), '+str({'y':2, 'x':1})+')' assert add.__cache__() == {_key4: 4, _key3: 3} @memoized(cache=dict_archive(cached=False)) # use archive backend 'direcly' def add(x,y): return x+y add(1,2) add(1,2)
#!/usr/bin/env python # # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) # Copyright (c) 2013-2014 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.archives import sqltable_archive as sql_archive #from klepto.archives import sql_archive #d = sql_archive('postgresql://*****:*****@localhost/defaultdb', cached=False) #d = sql_archive('mysql://*****:*****@localhost/defaultdb', cached=False) d = sql_archive(cached=False) try: import sqlalchemy __alchemy = True except ImportError: __alchemy = False d['a'] = 1 d['b'] = '1' assert d['a'] == 1 assert d['b'] == '1' if __alchemy: d['c'] = min squared = lambda x:x**2 d['d'] = squared assert d['c'] == min assert d['d'](2) == squared(2) else:
costD([1, 2, 3.1234], 3.9876) # == -32.94723372 costD([1, 2, 3.1234], 3.6789) # == -32.94723372 costD([1, 2, 3.4321], 3.6789) # == -32.94723372 assert costD.info().hit == 3 assert costD.info().miss == 1 assert costD.info().load == 0 #print ("") except ImportError: pass import sys import dill from klepto.archives import cache, sql_archive, dict_archive @memoized(cache=sql_archive()) def add(x, y): return x + y add(1, 2) add(1, 2) add(1, 3) #print ("sql_cache = %s" % add.__cache__()) _key4 = '((), ' + str({'y': 3, 'x': 1}) + ')' _key3 = '((), ' + str({'y': 2, 'x': 1}) + ')' assert add.__cache__() == {_key4: 4, _key3: 3} @memoized(cache=dict_archive(cached=False)) # use archive backend 'direcly' def add(x, y):