<s> """<STR_LIT>""" <EOL> try : <EOL> from cPickle import Pickler , Unpickler <EOL> except ImportError : <EOL> from pickle import Pickler , Unpickler <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> import UserDict <EOL> import warnings <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> class Shelf ( UserDict . DictMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dict , protocol = None , writeback = False ) : <EOL> self . dict = dict <EOL> if protocol is None : <EOL> protocol = <NUM_LIT:0> <EOL> self . _protocol = protocol <EOL> self . writeback = writeback <EOL> self . cache = { } <EOL> def keys ( self ) : <EOL> return self . dict . keys ( ) <EOL> def __len__ ( self ) : <EOL> return len ( self . dict ) <EOL> def has_key ( self , key ) : <EOL> return self . dict . has_key ( key ) <EOL> def __contains__ ( self , key ) : <EOL> return self . dict . has_key ( key ) <EOL> def get ( self , key , default = None ) : <EOL> if self . dict . has_key ( key ) : <EOL> return self [ key ] <EOL> return default <EOL> def __getitem__ ( self , key ) : <EOL> try : <EOL> value = self . cache [ key ] <EOL> except KeyError : <EOL> f = StringIO ( self . dict [ key ] ) <EOL> value = Unpickler ( f ) . load ( ) <EOL> if self . writeback : <EOL> self . cache [ key ] = value <EOL> return value <EOL> def __setitem__ ( self , key , value ) : <EOL> if self . writeback : <EOL> self . cache [ key ] = value <EOL> f = StringIO ( ) <EOL> p = Pickler ( f , self . _protocol ) <EOL> p . dump ( value ) <EOL> self . dict [ key ] = f . getvalue ( ) <EOL> def __delitem__ ( self , key ) : <EOL> del self . dict [ key ] <EOL> try : <EOL> del self . cache [ key ] <EOL> except KeyError : <EOL> pass <EOL> def close ( self ) : <EOL> self . sync ( ) <EOL> try : <EOL> self . dict . close ( ) <EOL> except AttributeError : <EOL> pass <EOL> self . dict = <NUM_LIT:0> <EOL> def __del__ ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> return <EOL> self . close ( ) <EOL> def sync ( self ) : <EOL> if self . writeback and self . cache : <EOL> self . writeback = False <EOL> for key , entry in self . cache . iteritems ( ) : <EOL> self [ key ] = entry <EOL> self . writeback = True <EOL> self . cache = { } <EOL> if hasattr ( self . dict , '<STR_LIT>' ) : <EOL> self . dict . sync ( ) <EOL> class BsdDbShelf ( Shelf ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dict , protocol = None , writeback = False ) : <EOL> Shelf . __init__ ( self , dict , protocol , writeback ) <EOL> def set_location ( self , key ) : <EOL> ( key , value ) = self . dict . set_location ( key ) <EOL> f = StringIO ( value ) <EOL> return ( key , Unpickler ( f ) . load ( ) ) <EOL> def next ( self ) : <EOL> ( key , value ) = self . dict . next ( ) <EOL> f = StringIO ( value ) <EOL> return ( key , Unpickler ( f ) . load ( ) ) <EOL> def previous ( self ) : <EOL> ( key , value ) = self . dict . previous ( ) <EOL> f = StringIO ( value ) <EOL> return ( key , Unpickler ( f ) . load ( ) ) <EOL> def first ( self ) : <EOL> ( key , value ) = self . dict . first ( ) <EOL> f = StringIO ( value ) <EOL> return ( key , Unpickler ( f ) . load ( ) ) <EOL> def last ( self ) : <EOL> ( key , value ) = self . dict . last ( ) <EOL> f = StringIO ( value ) <EOL> return ( key , Unpickler ( f ) . load ( ) ) <EOL> class DbfilenameShelf ( Shelf ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , flag = '<STR_LIT:c>' , protocol = None , writeback = False ) : <EOL> import anydbm <EOL> Shelf . __init__ ( self , anydbm . open ( filename , flag ) , protocol , writeback ) <EOL> def open ( filename , flag = '<STR_LIT:c>' , protocol = None , writeback = False ) : <EOL> """<STR_LIT>""" <EOL> return DbfilenameShelf ( filename , flag , protocol , writeback ) </s>
