<s> class StrictDict ( dict ) : <EOL> def __init__ ( self , initDict = None , name = '<STR_LIT>' ) : <EOL> self . add ( '<STR_LIT>' , name ) <EOL> if initDict : <EOL> self . consumeDict ( initDict ) <EOL> def __getattr__ ( self , key ) : <EOL> return self . __getitem__ ( key ) <EOL> def __setattr__ ( self , key , value ) : <EOL> return self . __setitem__ ( key , value ) <EOL> def __getitem__ ( self , key ) : <EOL> if not self . has_key ( key ) : <EOL> raise AttributeError , '<STR_LIT>' % ( self . __name__ , key ) <EOL> return dict . __getitem__ ( self , key ) <EOL> def __setitem__ ( self , key , value , _override = False ) : <EOL> if not _override and not self . has_key ( key ) : <EOL> if not self . has_key ( '<STR_LIT>' ) : <EOL> self . add ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> raise AttributeError , '<STR_LIT>' % ( self . __name__ , key , self . __name__ , key ) <EOL> dictItems = self . _getItemsOrFalse ( value ) <EOL> if dictItems is False : <EOL> dict . __setitem__ ( self , key , value ) <EOL> else : <EOL> dict . __setitem__ ( self , key , StrictDict ( value , name = '<STR_LIT>' % ( self . __name__ , key ) ) ) <EOL> def keys ( self ) : <EOL> return [ key for key in dict . keys ( self ) if key != '<STR_LIT>' ] <EOL> def items ( self ) : <EOL> return [ ( key , value ) for ( key , value ) in dict . items ( self ) if key != '<STR_LIT>' ] <EOL> def values ( self ) : <EOL> return [ value for ( key , value ) in dict . items ( self ) if key != '<STR_LIT>' ] <EOL> def iterkeys ( self ) : <EOL> return dict . iterkeys ( dict ( self . items ( ) ) ) <EOL> def itervalues ( self ) : <EOL> return dict . itervalues ( dict ( self . items ( ) ) ) <EOL> def __iter__ ( self ) : <EOL> return dict . __iter__ ( dict ( self . items ( ) ) ) <EOL> def add ( self , key , value ) : <EOL> return self . __setitem__ ( key , value , _override = True ) <EOL> def consumeDict ( self , obj ) : <EOL> items = self . _getItemsOrFalse ( obj ) <EOL> if not items : <EOL> raise TypeError , '<STR_LIT>' <EOL> else : <EOL> for ( k , v ) in items : <EOL> self . __setitem__ ( k , v , _override = True ) <EOL> return True <EOL> def _getItemsOrFalse ( self , obj ) : <EOL> try : <EOL> return obj . items ( ) <EOL> except AttributeError : <EOL> return False <EOL> def toDict ( self ) : <EOL> return self . _toDict ( self ) <EOL> def _toDict ( self , oldDict ) : <EOL> items = self . _getItemsOrFalse ( oldDict ) <EOL> if not items : <EOL> return oldDict <EOL> else : <EOL> newDict = { } <EOL> for ( k , v ) in items : <EOL> newDict [ k ] = self . _toDict ( v ) <EOL> return newDict </s>
