 from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> from hashlib import sha1 <EOL> class PayloadFieldAlreadyDefinedError ( Exception ) : pass <EOL> class PayloadFrozenError ( Exception ) : pass <EOL> class Payload ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _fields = { } <EOL> self . _frozen = False <EOL> self . _fingerprint_memo_map = { } <EOL> @ property <EOL> def fields ( self ) : <EOL> return self . _fields . items ( ) <EOL> def freeze ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _frozen = True <EOL> def get_field ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _fields . get ( key , default ) <EOL> def get_field_value ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> if key in self . _fields : <EOL> payload_field = self . _fields [ key ] <EOL> if payload_field : <EOL> return payload_field . value <EOL> return default <EOL> def add_fields ( self , field_dict ) : <EOL> """<STR_LIT>""" <EOL> for key , field in field_dict . items ( ) : <EOL> self . add_field ( key , field ) <EOL> def add_field ( self , key , field ) : <EOL> """<STR_LIT>""" <EOL> if key in self . _fields : <EOL> raise PayloadFieldAlreadyDefinedError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( key = key , existing_field = self . _fields [ key ] , field = field ) ) <EOL> elif self . _frozen : <EOL> raise PayloadFrozenError ( <EOL> '<STR_LIT>' <EOL> . format ( key = key ) ) <EOL> else : <EOL> self . _fields [ key ] = field <EOL> self . _fingerprint_memo = None <EOL> def fingerprint ( self , field_keys = None ) : <EOL> """<STR_LIT>""" <EOL> field_keys = frozenset ( field_keys or self . _fields . keys ( ) ) <EOL> if field_keys not in self . _fingerprint_memo_map : 
 self . _fingerprint_memo_map [ field_keys ] = self . _compute_fingerprint ( field_keys ) 
 from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> from hashlib import sha1 <EOL> class PayloadFieldAlreadyDefinedError ( Exception ) : pass <EOL> class PayloadFrozenError ( Exception ) : pass <EOL> class Payload ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _fields = { } <EOL> self . _frozen = False <EOL> self . _fingerprint_memo_map = { } <EOL> @ property <EOL> def fields ( self ) : <EOL> return self . _fields . items ( ) <EOL> def freeze ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _frozen = True <EOL> def get_field ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _fields . get ( key , default ) <EOL> def get_field_value ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> if key in self . _fields : <EOL> payload_field = self . _fields [ key ] <EOL> if payload_field : <EOL> return payload_field . value <EOL> return default <EOL> def add_fields ( self , field_dict ) : <EOL> """<STR_LIT>""" <EOL> for key , field in field_dict . items ( ) : <EOL> self . add_field ( key , field ) <EOL> def add_field ( self , key , field ) : <EOL> """<STR_LIT>""" <EOL> if key in self . _fields : <EOL> raise PayloadFieldAlreadyDefinedError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( key = key , existing_field = self . _fields [ key ] , field = field ) ) <EOL> elif self . _frozen : <EOL> raise PayloadFrozenError ( <EOL> '<STR_LIT>' <EOL> . format ( key = key ) ) <EOL> else : <EOL> self . _fields [ key ] = field <EOL> self . _fingerprint_memo = None <EOL> def fingerprint ( self , field_keys = None ) : <EOL> """<STR_LIT>""" <EOL> field_keys = frozenset ( field_keys or self . _fields . keys ( ) ) <EOL> if field_keys not in self . _fingerprint_memo_map : <EOL> self . _fingerprint_memo_map [ field_keys ] = self . _compute_fingerprint ( field_keys ) <EOL> return self . _fingerprint_memo_map [ field_keys ] <EOL> def _compute_fingerprint ( self , field_keys ) : <EOL> hasher = sha1 ( ) <EOL> empty_hash = True <EOL> for key in sorted ( field_keys ) : <EOL> field = self . _fields [ key ] <EOL> if field is not None : <EOL> fp = field . fingerprint ( ) <EOL> if fp is not None : <EOL> empty_hash = False <EOL> hasher . update ( sha1 ( key ) . hexdigest ( ) ) <EOL> hasher . update ( fp ) <EOL> if empty_hash : <EOL> return None <EOL> else : <EOL> return hasher . hexdigest ( ) <EOL> def mark_dirty ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _fingerprint_memo_map = { } <EOL> for field in self . _fields . values ( ) : <EOL> field . mark_dirty ( ) <EOL> def __getattr__ ( self , attr ) : <EOL> field = self . _fields [ attr ] <EOL> if field is not None : <EOL> return field . value <EOL> else : <EOL> return None <EOL> def __hasattr__ ( self , attr ) : <EOL> return attr in self . _fields </s>
