<s> from __future__ import unicode_literals <EOL> from collections import deque <EOL> __all__ = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class SimpleCache ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , maxsize = <NUM_LIT:8> ) : <EOL> assert isinstance ( maxsize , int ) and maxsize > <NUM_LIT:0> <EOL> self . _data = { } <EOL> self . _keys = deque ( ) <EOL> self . maxsize = maxsize <EOL> def get ( self , key , getter_func ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _data [ key ] <EOL> except KeyError : <EOL> value = getter_func ( ) <EOL> self . _data [ key ] = value <EOL> self . _keys . append ( key ) <EOL> if len ( self . _data ) > self . maxsize : <EOL> key_to_remove = self . _keys . popleft ( ) <EOL> if key_to_remove in self . _data : <EOL> del self . _data [ key_to_remove ] <EOL> return value <EOL> class FastDictCache ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , get_value = None , size = <NUM_LIT> ) : <EOL> assert callable ( get_value ) <EOL> assert isinstance ( size , int ) and size > <NUM_LIT:0> <EOL> self . _keys = deque ( ) <EOL> self . get_value = get_value <EOL> self . size = size <EOL> def __missing__ ( self , key ) : <EOL> if len ( self ) > self . size : <EOL> key_to_remove = self . _keys . popleft ( ) <EOL> if key_to_remove in self : <EOL> del self [ key_to_remove ] <EOL> result = self . get_value ( * key ) <EOL> self [ key ] = result <EOL> self . _keys . append ( key ) <EOL> return result </s>
