#NEWLINE# @ classmethod #NEWLINE# def encode( cls , items , size = True , recalc_message_size = True ) : #NEWLINE# #INDENT# if isinstance ( items , io . BytesIO ) : #NEWLINE# #INDENT# size = Int32 . decode ( items ) #NEWLINE# items . seek ( ( - 4 ) , 1 ) #NEWLINE# return items . read ( ( size + 4 ) ) #NEWLINE# #UNINDENT# encoded_values = [ ] #NEWLINE# for ( offset , message_size , message ) in items : #NEWLINE# #INDENT# if isinstance ( message , Message ) : #NEWLINE# #INDENT# encoded_message = message . encode ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# encoded_message = cls . ITEM . fields [ 2 ] . encode ( message ) #NEWLINE# #UNINDENT# if recalc_message_size : #NEWLINE# #INDENT# message_size = len ( encoded_message ) #NEWLINE# #UNINDENT# encoded_values . append ( cls . ITEM . fields [ 0 ] . encode ( offset ) ) #NEWLINE# encoded_values . append ( cls . ITEM . fields [ 1 ] . encode ( message_size ) ) #NEWLINE# encoded_values . append ( encoded_message ) #NEWLINE# #UNINDENT# encoded = b'' . join ( encoded_values ) #NEWLINE# if ( not size ) : #NEWLINE# #INDENT# return encoded #NEWLINE# #UNINDENT# return ( Int32 . encode ( len ( encoded ) ) + encoded )