How to Nest Structs?

web at cjhanks.name web at cjhanks.name
Mon Mar 4 23:29:01 CET 2013


Try unpacking the nested struct as a fixed width string and then unpacking the string.  

Then unpack the string 

On Monday, March 4, 2013 10:22:07 AM UTC-8, Ari King wrote:
> Hi,
> 
> 
> 
> I'm trying to nest the "info_header", "info_body", and "info_trailer" structs (see below) into a "data_packet" struct. Does anyone know how I can/should accomplish this? Thanks.
> 
> 
> 
> batch_header_format = struct.Struct('!c2h')
> 
> info_header_format = struct.Struct('!2hl')
> 
> mkt_status_format = struct.Struct('!c')
> 
> info_trailer_format = struct.Struct('!hc')
> 
> 
> 
> mkt_session_codes = [b'PO',b'PC',b'CO',b'CC',b'CK',b'CL']
> 
> mkt_type = [b'N',b'S',b'O',b'A',b'C',b'G']
> 
> 
> 
> batch_header = batch_header_format.pack(b'1',1,1024)
> 
> 
> 
> total_size = info_header_format.size + mkt_status_format.size + info_trailer_format.size
> 
> 
> 
> # Combine following into data struct.
> 
> info_header = info_header_format.pack(int(binascii.hexlify(mkt_session_codes[random.randint(0,5)])), total_size, 124)
> 
> info_body = mkt_status_format.pack(mkt_type[random.randint(0,5)])
> 
> info_trailer = info_trailer_format.pack(0, b'\r')
> 
> 
> 
> -Ari



More information about the Python-list mailing list