[Python-ideas] Suggest adding struct iter_unpack allowing mixed formats & add iter_pack

Steve Barnes gadgetsteve at live.co.uk
Tue Jan 30 01:28:45 EST 2018

In my work I make a lot of use of struct pack & unpack but sometimes 
find the fact that I either have to supply the whole format string or 
add position tracking mechanisms to my code explicitly so as to use 
pack_to or unpack_from if the structures that I am dealing with are not 
simple arrays of a single type. In my particular use case of de/encoding 
messages from a remote device that is sometimes forwarding on collected 
information to & from the devices that it controls the endianness is not 
always consistent between message elements in a single message.

It would be very nice if:
   a) iter_unpack.next produced an iterator that optionally took an 
additional parameter of a format string, (with endianness permitted), to 
replace the current format string in use.
   b) there was a matching iter_pack function that the next method took 
either more data to pack into the buffer extending it or a new format 
string, either as a named parameter or with an exposed set_fmt method on 
the packer.

This would allow my specific use case and the ones where a buffer or 
stream is constructed of blocks consisting of a byte or word that 
specifies the data type(s) to follow (and sometimes a count) followed by 
the actual data.

Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect 
those of my employer.

More information about the Python-ideas mailing list