def cache1lvl(maxsize=100):
 def decorating_function(user_function):
  cache = {}
  use_count = Counter()
  @functools.wraps(user_function)
  def wrapper(key, *args, **kwargs):
   try:
    result = cache[key]
   except KeyError:
    if (len(cache) == maxsize):
     for (k, _) in nsmallest(((maxsize // 10) or 1), use_count.iteritems(), key=itemgetter(1)):
      del cache[k], use_count[k]
    cache[key] = user_function(key, *args, **kwargs)
    result = cache[key]
   finally:
    use_count[key] += 1
   return result
  def clear():
   cache.clear()
   use_count.clear()
  def delete(key):
   try:
    del cache[key]
    del use_count[key]
   except KeyError:
    return False
   else:
    return True
  wrapper.clear = clear
  wrapper.cache = cache
  wrapper.delete = delete
  return wrapper
 return decorating_function

def cache1lvl ( maxsize = 100 ) : def decorating_function ( user_function ) : cache = { } use_count = Counter ( ) @ functools . wraps ( user_function ) def wrapper ( key , * args , ** kwargs ) : try : result = cache [ key ] except KeyError : if ( len ( cache ) == maxsize ) : for ( k , _ ) in nsmallest ( ( ( maxsize // 10 ) or 1 ) , use_count . iteritems ( ) , key = itemgetter ( 1 ) ) : del cache [ k ] , use_count [ k ] cache [ key ] = user_function ( key , * args , ** kwargs ) result = cache [ key ] finally : use_count [ key ] += 1 return result def clear ( ) : cache . clear ( ) use_count . clear ( ) def delete ( key ) : try : del cache [ key ] del use_count [ key ] except KeyError : return False else : return True wrapper . clear = clear wrapper . cache = cache wrapper . delete = delete return wrapper return decorating_function
modified version of URL .