Beispiel #1
0
def test_argument_variations():
    """
    Test to ensure that permutations of argument order and/or argument naming
    all reduce to the identical unique hash for the same invocation.
    """
    def foo(bar, baz, *args, **kwargs):
        pass

    def check(*keys):
        assert len(set(keys)) == 1

    check(
        argument_hash(foo, 1, 2, 3, 4, a=1, b=2, c=3),
        argument_hash(foo, 1, 2, 3, 4, b=2, a=1, c=3),
        argument_hash(foo, 1, 2, 3, 4, b=2, c=3, a=1),
        argument_hash(foo, a=1, b=2, c=3, *(1, 2, 3, 4)),
        argument_hash(foo, 1, 2, *(3, 4), **{'a': 1, 'b': 2, 'c': 3}),
    )

    check(
        argument_hash(foo, 1, 2),
        argument_hash(foo, bar=1, baz=2),
        argument_hash(foo, baz=2, bar=1),
        argument_hash(foo, 1, baz=2)
    )
Beispiel #2
0
def test_argument_variations():
    """
    Test to ensure that permutations of argument order and/or argument naming
    all reduce to the identical unique hash for the same invocation.
    """
    def foo(bar, baz, *args, **kwargs):
        pass

    def check(*keys):
        assert len(set(keys)) == 1

    check(
        argument_hash(foo, 1, 2, 3, 4, a=1, b=2, c=3),
        argument_hash(foo, 1, 2, 3, 4, b=2, a=1, c=3),
        argument_hash(foo, 1, 2, 3, 4, b=2, c=3, a=1),
        argument_hash(foo, a=1, b=2, c=3, *(1, 2, 3, 4)),
        argument_hash(foo, 1, 2, *(3, 4), **{
            'a': 1,
            'b': 2,
            'c': 3
        }),
    )

    check(argument_hash(foo, 1, 2), argument_hash(foo, bar=1, baz=2),
          argument_hash(foo, baz=2, bar=1), argument_hash(foo, 1, baz=2))
Beispiel #3
0
        def inner(*args, **kwargs):
            # TODO: Make the argument hash function pluggable.
            key = '%s:%s' % (prefix, argument_hash(function, *args, **kwargs))

            # Try to get the value from the cache. If it doesn't exist, invoke
            # the function and set the cache value to the returned value.
            value = backend.get(key)
            if value is CACHE_MISS:
                value = function(*args, **kwargs)
                backend.set(key, value)

            return value
Beispiel #4
0
        def inner(*args, **kwargs):
            # TODO: Make the argument hash function pluggable.
            key = "%s:%s" % (prefix, argument_hash(function, *args, **kwargs))

            # Try to get the value from the cache. If it doesn't exist, invoke
            # the function and set the cache value to the returned value.
            value = backend.get(key)
            if value is CACHE_MISS:
                value = function(*args, **kwargs)
                backend.set(key, value)

            return value