sportsref/decorators.py
def memoize(fun):
    """A decorator for memoizing functions.

    Only works on functions that take simple arguments - arguments that take
    list-like or dict-like arguments will not be memoized, and this function
    will raise a TypeError.
    """
    @funcutils.wraps(fun)
    def wrapper(*args, **kwargs):

        do_memoization = sportsref.get_option('memoize')
        if not do_memoization:
            return fun(*args, **kwargs)

        hash_args = tuple(args)
        hash_kwargs = frozenset(sorted(kwargs.items()))
        key = (hash_args, hash_kwargs)

        def _copy(v):
            if isinstance(v, pq):
                return v.clone()
            else:
                return copy.deepcopy(v)

        try:
            ret = _copy(cache[key])
            return ret
        except KeyError:
            cache[key] = fun(*args, **kwargs)
            ret = _copy(cache[key])
            return ret
        except TypeError:
            print('memoization type error in function {} for arguments {}'
                  .format(fun.__name__, key))
            raise

    cache = {}
    return wrapper
def memoize(fun):
    @funcutils.wraps(fun)
    def wrapper(*args, **kwargs):

        do_memoization = sportsref.get_option('memoize')
        if not do_memoization:
            return fun(*args, **kwargs)

        hash_args = tuple(args)
        hash_kwargs = frozenset(sorted(kwargs.items()))
        key = (hash_args, hash_kwargs)

        def _copy(v):
            if isinstance(v, pq):
                return v.clone()
            else:
                return copy.deepcopy(v)

        try:
            ret = _copy(cache[key])
            return ret
        except KeyError:
            cache[key] = fun(*args, **kwargs)
            ret = _copy(cache[key])
            return ret
        except TypeError:
            print('memoization type error in function {} for arguments {}'
                  .format(fun.__name__, key))
            raise

    cache = {}
    return wrapper
def memoize ( fun ) : @ funcutils . wraps ( fun ) def wrapper ( * args , * * kwargs ) : do_memoization = sportsref . get_option ( 'memoize' ) if not do_memoization : return fun ( * args , * * kwargs ) hash_args = tuple ( args ) hash_kwargs = frozenset ( sorted ( kwargs . items ( ) ) ) key = ( hash_args , hash_kwargs ) def _copy ( v ) : if isinstance ( v , pq ) : return v . clone ( ) else : return copy . deepcopy ( v ) try : ret = _copy ( cache [ key ] ) return ret except KeyError : cache [ key ] = fun ( * args , * * kwargs ) ret = _copy ( cache [ key ] ) return ret except TypeError : print ( 'memoization type error in function {} for arguments {}' . format ( fun . __name__ , key ) ) raise cache = { } return wrapper
A decorator for memoizing functions.

    Only works on functions that take simple arguments - arguments that take
    list-like or dict-like arguments will not be memoized, and this function
    will raise a TypeError.
A decorator for memoizing functions .