def test_basic(): try: from numpy import sum, asarray @memoized(keymap=dumps, tol=3) def add(*args): #print ('new:', args) return sum(args) assert add(1, 2, 3.0001) == 6.0000999999999998 assert add(1, 2, 3.00012) == 6.0000999999999998 assert add(1, 2, 3.0234) == 6.0234000000000005 assert add(1, 2, 3.023) == 6.0234000000000005 assert add.info().hit == 2 assert add.info().miss == 2 assert add.info().load == 0 def cost(x, y): #print ('new: %s or %s' % (str(x), str(y))) x = asarray(x) y = asarray(y) return sum(x**2 - y**2) cost1 = memoized(keymap=dumps, tol=1)(cost) cost0 = memoized(keymap=dumps, tol=0)(cost) costD = memoized(keymap=dumps, tol=0, deep=True)(cost) #print ("rounding to one decimals...") cost1([1, 2, 3.1234], 3.9876) # == -32.94723372 cost1([1, 2, 3.1234], 3.9876) # == -32.94723372 cost1([1, 2, 3.1234], 3.6789) # == -25.84728807 cost1([1, 2, 3.4321], 3.6789) # == -23.82360522 assert cost1.info().hit == 1 assert cost1.info().miss == 3 assert cost1.info().load == 0 #print ("\nrerun the above with rounding to zero decimals...") cost0([1, 2, 3.1234], 3.9876) # == -32.94723372 cost0([1, 2, 3.1234], 3.9876) # == -32.94723372 cost0([1, 2, 3.1234], 3.6789) # == -32.94723372 cost0([1, 2, 3.4321], 3.6789) # == -23.82360522 assert cost0.info().hit == 2 assert cost0.info().miss == 2 assert cost0.info().load == 0 #print ("\nrerun again with deep rounding to zero decimals...") 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
def test_basic(): try: from numpy import sum, asarray @memoized(keymap=dumps, tol=3) def add(*args): #print ('new:', args) return sum(args) assert add(1,2,3.0001) == 6.0000999999999998 assert add(1,2,3.00012) == 6.0000999999999998 assert add(1,2,3.0234) == 6.0234000000000005 assert add(1,2,3.023) == 6.0234000000000005 assert add.info().hit == 2 assert add.info().miss == 2 assert add.info().load == 0 def cost(x,y): #print ('new: %s or %s' % (str(x), str(y))) x = asarray(x) y = asarray(y) return sum(x**2 - y**2) cost1 = memoized(keymap=dumps, tol=1)(cost) cost0 = memoized(keymap=dumps, tol=0)(cost) costD = memoized(keymap=dumps, tol=0, deep=True)(cost) #print ("rounding to one decimals...") cost1([1,2,3.1234], 3.9876)# == -32.94723372 cost1([1,2,3.1234], 3.9876)# == -32.94723372 cost1([1,2,3.1234], 3.6789)# == -25.84728807 cost1([1,2,3.4321], 3.6789)# == -23.82360522 assert cost1.info().hit == 1 assert cost1.info().miss == 3 assert cost1.info().load == 0 #print ("\nrerun the above with rounding to zero decimals...") cost0([1,2,3.1234], 3.9876)# == -32.94723372 cost0([1,2,3.1234], 3.9876)# == -32.94723372 cost0([1,2,3.1234], 3.6789)# == -32.94723372 cost0([1,2,3.4321], 3.6789)# == -23.82360522 assert cost0.info().hit == 2 assert cost0.info().miss == 2 assert cost0.info().load == 0 #print ("\nrerun again with deep rounding to zero decimals...") 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
assert add(1,2,3.0001) == 6.0000999999999998 assert add(1,2,3.00012) == 6.0000999999999998 assert add(1,2,3.0234) == 6.0234000000000005 assert add(1,2,3.023) == 6.0234000000000005 assert add.info().hit == 2 assert add.info().miss == 2 assert add.info().load == 0 def cost(x,y): #print ('new: %s or %s' % (str(x), str(y))) x = asarray(x) y = asarray(y) return sum(x**2 - y**2) cost1 = memoized(keymap=dumps, tol=1)(cost) cost0 = memoized(keymap=dumps, tol=0)(cost) costD = memoized(keymap=dumps, tol=0, deep=True)(cost) #print ("rounding to one decimals...") cost1([1,2,3.1234], 3.9876)# == -32.94723372 cost1([1,2,3.1234], 3.9876)# == -32.94723372 cost1([1,2,3.1234], 3.6789)# == -25.84728807 cost1([1,2,3.4321], 3.6789)# == -23.82360522 assert cost1.info().hit == 1 assert cost1.info().miss == 3 assert cost1.info().load == 0 #print ("\nrerun the above with rounding to zero decimals...") cost0([1,2,3.1234], 3.9876)# == -32.94723372 cost0([1,2,3.1234], 3.9876)# == -32.94723372
assert add(1, 2, 3.0001) == 6.0000999999999998 assert add(1, 2, 3.00012) == 6.0000999999999998 assert add(1, 2, 3.0234) == 6.0234000000000005 assert add(1, 2, 3.023) == 6.0234000000000005 assert add.info().hit == 2 assert add.info().miss == 2 assert add.info().load == 0 def cost(x, y): #print ('new: %s or %s' % (str(x), str(y))) x = asarray(x) y = asarray(y) return sum(x**2 - y**2) cost1 = memoized(keymap=dumps, tol=1)(cost) cost0 = memoized(keymap=dumps, tol=0)(cost) costD = memoized(keymap=dumps, tol=0, deep=True)(cost) #print ("rounding to one decimals...") cost1([1, 2, 3.1234], 3.9876) # == -32.94723372 cost1([1, 2, 3.1234], 3.9876) # == -32.94723372 cost1([1, 2, 3.1234], 3.6789) # == -25.84728807 cost1([1, 2, 3.4321], 3.6789) # == -23.82360522 assert cost1.info().hit == 1 assert cost1.info().miss == 3 assert cost1.info().load == 0 #print ("\nrerun the above with rounding to zero decimals...") cost0([1, 2, 3.1234], 3.9876) # == -32.94723372 cost0([1, 2, 3.1234], 3.9876) # == -32.94723372