(maxsize=100)
return decorating_function
A simple cache that, when the cache is full, deletes the least frequently
    used 10% of the cached values.

    This function duplicates (more-or-less) the protocol of the
    ``functools.lru_cache`` decorator in the Python 3.2 standard library.

    Arguments to the cached function must be hashable.

    View the cache statistics tuple ``(hits, misses, maxsize, currsize)``
    with f.cache_info().  Clear the cache and statistics with f.cache_clear().
    Access the underlying function with f.__wrapped__.
A simple cache that, when the cache is full, deletes the least frequently
    used 10% of the cached values.
A simple cache that when the cache is full deletes the least frequently used 10% of the cached values .
def lfu_cache(maxsize=100):
    """A simple cache that, when the cache is full, deletes the least frequently
    used 10% of the cached values.

    This function duplicates (more-or-less) the protocol of the
    ``functools.lru_cache`` decorator in the Python 3.2 standard library.

    Arguments to the cached function must be hashable.

    View the cache statistics tuple ``(hits, misses, maxsize, currsize)``
    with f.cache_info().  Clear the cache and statistics with f.cache_clear().
    Access the underlying function with f.__wrapped__.
    """

    def decorating_function(user_function):
        stats = [0, 0]  # Hits, misses
        data = {}
        usecount = Counter()

        @functools.wraps(user_function)
        def wrapper(*args):
            try:
                result = data[args]
                stats[0] += 1  # Hit
            except KeyError:
                stats[1] += 1  # Miss
                if len(data) == maxsize:
                    for k, _ in nsmallest(maxsize // 10 or 1,
                                          iteritems(usecount),
                                          key=itemgetter(1)):
                        del data[k]
                        del usecount[k]
                data[args] = user_function(*args)
                result = data[args]
            finally:
                usecount[args] += 1
            return result

        def cache_info():
            return stats[0], stats[1], maxsize, len(data)

        def cache_clear():
            data.clear()
            usecount.clear()

        wrapper.cache_info = cache_info
        wrapper.cache_clear = cache_clear
        return wrapper
    return decorating_function
def lfu_cache ( maxsize = 100 ) : def decorating_function ( user_function ) : stats = [ 0 , 0 ] # Hits, misses data = { } usecount = Counter ( ) @ functools . wraps ( user_function ) def wrapper ( * args ) : try : result = data [ args ] stats [ 0 ] += 1 # Hit except KeyError : stats [ 1 ] += 1 # Miss if len ( data ) == maxsize : for k , _ in nsmallest ( maxsize // 10 or 1 , iteritems ( usecount ) , key = itemgetter ( 1 ) ) : del data [ k ] del usecount [ k ] data [ args ] = user_function ( * args ) result = data [ args ] finally : usecount [ args ] += 1 return result def cache_info ( ) : return stats [ 0 ] , stats [ 1 ] , maxsize , len ( data ) def cache_clear ( ) : data . clear ( ) usecount . clear ( ) wrapper . cache_info = cache_info wrapper . cache_clear = cache_clear return wrapper return decorating_function