#NEWLINE# def serialize( self ) : #NEWLINE# #INDENT# 'Encode a packet and store the resulted bytearray in self.data.\n\n        This method is legal only when encoding a packet.\n        ' #NEWLINE# self . data = bytearray ( ) #NEWLINE# r = self . protocols [ : : ( - 1 ) ] #NEWLINE# for ( i , p ) in enumerate ( r ) : #NEWLINE# #INDENT# if isinstance ( p , packet_base . PacketBase ) : #NEWLINE# #INDENT# if ( i == ( len ( r ) - 1 ) ) : #NEWLINE# #INDENT# prev = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# prev = r [ ( i + 1 ) ] #NEWLINE# #UNINDENT# data = p . serialize ( self . data , prev ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# data = six . binary_type ( p ) #NEWLINE# #UNINDENT# self . data = ( data + self . data )