def test_memoize():
    @memoize(keymap=hasher, ignore=('self', '**'))
    def _add(x, *args, **kwds):
        debug = kwds.get('debug', False)
        if debug:
            print('debug:', x, args, kwds)
        return sum((x, ) + args)

    _add(2, 0)
    _add(2, 0, z=4)
    _add(2, 0, debug=False)
    _add(1, 2, debug=False)
    _add(1, 2, debug=True)
    _add(4)
    _add(x=4)
    _add(4, debug=True)
    _add(2, 0, 3)
    _add(2, 0, 4)

    _cache = _add.__cache__()
    _func = _add.__wrapped__

    # do a lookup
    assert _add.lookup(2, 0) == _func(2, 0)

    # generate the key, and do a look-up
    key = _add.key(2, 0)
    assert _cache[key] == _func(2, 0)

    # look-up the key again, doing a little more work...
    lookup = keygen('self', '**')(_func)
    lookup.register(hasher)
    key = lookup(2, 0)
    assert _cache[key] == _func(2, 0)

    # since we have the 'key lookup', let's play with lookup a bit
    assert lookup.valid()
    assert lookup.call() == _func(2, 0)
Exemple #2
0
def test_memoize():
    @memoize(keymap=hasher, ignore=('self','**'))
    def _add(x, *args, **kwds):
        debug = kwds.get('debug', False)
        if debug:
            print ('debug:', x, args, kwds)
        return sum((x,)+args)

    _add(2,0)
    _add(2,0,z=4)
    _add(2,0,debug=False)
    _add(1,2,debug=False)
    _add(1,2,debug=True)
    _add(4)
    _add(x=4)
    _add(4,debug=True)
    _add(2,0,3)
    _add(2,0,4)

    _cache =  _add.__cache__()
    _func = _add.__wrapped__

    # do a lookup
    assert _add.lookup(2,0) == _func(2,0)

    # generate the key, and do a look-up
    key = _add.key(2,0)
    assert _cache[key] == _func(2,0)

    # look-up the key again, doing a little more work...
    lookup = keygen('self','**')(_func)
    lookup.register(hasher)
    key = lookup(2,0)
    assert _cache[key] == _func(2,0)

    # since we have the 'key lookup', let's play with lookup a bit
    assert lookup.valid()
    assert lookup.call() == _func(2,0)
Exemple #3
0
_add(2,0,3)
_add(2,0,4)

_cache =  _add.__cache__()
_func = _add.__wrapped__

# do a lookup
assert _add.lookup(2,0) == _func(2,0)

# generate the key, and do a look-up
key = _add.key(2,0)
assert _cache[key] == _func(2,0)

# look-up the key again, doing a little more work...
from klepto import keygen
lookup = keygen('self','**')(_func)
lookup.register(hasher)
key = lookup(2,0)
assert _cache[key] == _func(2,0)

# since we have the 'key lookup', let's play with lookup a bit
assert lookup.valid()
assert lookup.call() == _func(2,0)


######################################################
# more of the same...
from klepto import inf_cache

class Foo(object):
  @keygen('self')
Exemple #4
0
_add(2, 0, 4)

_cache = _add.__cache__()
_func = _add.__wrapped__

# do a lookup
assert _add.lookup(2, 0) == _func(2, 0)

# generate the key, and do a look-up
key = _add.key(2, 0)
assert _cache[key] == _func(2, 0)

# look-up the key again, doing a little more work...
from klepto import keygen

lookup = keygen('self', '**')(_func)
lookup.register(hasher)
key = lookup(2, 0)
assert _cache[key] == _func(2, 0)

# since we have the 'key lookup', let's play with lookup a bit
assert lookup.valid()
assert lookup.call() == _func(2, 0)

######################################################
# more of the same...
from klepto import inf_cache


class Foo(object):
    @keygen('self')