(vary_on=())
return decorator
Decorator which caches calculations performed during a UCR EvaluationContext
    The decorated function or method must have a parameter called 'context'
    which will be used by this decorator to store the cache.
Decorator which caches calculations performed during a UCR EvaluationContext
    The decorated function or method must have a parameter called 'context'
    which will be used by this decorator to store the cache.
Decorator which caches calculations performed during a UCR EvaluationContext The decorated function or method must have a parameter called context which will be used by this decorator to store the cache .
def ucr_context_cache(vary_on=()):
    """
    Decorator which caches calculations performed during a UCR EvaluationContext
    The decorated function or method must have a parameter called 'context'
    which will be used by this decorator to store the cache.
    """
    def decorator(fn):
        assert 'context' in fn.__code__.co_varnames
        assert isinstance(vary_on, tuple)

        @wraps(fn)
        def _inner(*args, **kwargs):
            # shamelessly stolen from quickcache
            callargs = inspect.getcallargs(fn, *args, **kwargs)
            context = callargs['context']
            prefix = '{}.{}'.format(
                fn.__name__[:40] + (fn.__name__[40:] and '..'),
                hashlib.md5(inspect.getsource(fn).encode('utf-8')).hexdigest()[-8:]
            )
            cache_key = (prefix,) + tuple(callargs[arg_name] for arg_name in vary_on)
            if context.exists_in_cache(cache_key):
                return context.get_cache_value(cache_key)
            res = fn(*args, **kwargs)
            context.set_cache_value(cache_key, res)
            return res
        return _inner
    return decorator
def ucr_context_cache ( vary_on = ( ) ) : def decorator ( fn ) : assert 'context' in fn . __code__ . co_varnames assert isinstance ( vary_on , tuple ) @ wraps ( fn ) def _inner ( * args , * * kwargs ) : # shamelessly stolen from quickcache callargs = inspect . getcallargs ( fn , * args , * * kwargs ) context = callargs [ 'context' ] prefix = '{}.{}' . format ( fn . __name__ [ : 40 ] + ( fn . __name__ [ 40 : ] and '..' ) , hashlib . md5 ( inspect . getsource ( fn ) . encode ( 'utf-8' ) ) . hexdigest ( ) [ - 8 : ] ) cache_key = ( prefix , ) + tuple ( callargs [ arg_name ] for arg_name in vary_on ) if context . exists_in_cache ( cache_key ) : return context . get_cache_value ( cache_key ) res = fn ( * args , * * kwargs ) context . set_cache_value ( cache_key , res ) return res return _inner return decorator