Today I’m dealing with python code for the first time. Basically, have no time for python tutorial. I’m Java background. If you could help me with python syntax, that would be great. Piece of code:
def fromData(headerdata, offset = 0):
"""Return a new Header object from the supplied binary data."""
(id, flags, qdc, anc, nsc, arc,) = \
struct.unpack_from(">6H", headerdata, offset)
qr_ = (flags >> Header.OFFSET_QR) & 0x1
questions:
-
what is this
(id, flags, qdc, anc, nsc, arc,) = \ struct.unpack_from(">6H", headerdata, offset)mean? What is it doing?
- why the “arc” ends with “,”?
- why qr is followed by “_”
- what is
>6H. could you redirect me for the list of formats?
A C
structcan be packed into flat binary data, which is what Python 2 calls a string. Thestructmodule lets you take a string that represents one of these C structs, and “unpack” it into a Python data structure.To do so you call
struct.unpack. You need to specify a format string (as defined in the linked docs), and it returns a tuple of values unpacked from the data.