I have little C/Qt experience and have a small parser that I need to port to Python. Is anyone able to explain how I can implement the below in Python? I understand what the result is, just can't understand how to achieve the uint32 instantiation and shift that results in different part lengths based on the 4 byte "id". I'm hoping to parse it using just native Python 3.5+ packages, numpy or similar is fine if it makes the typing convenient.
QDataStream stream(item);
stream.setByteOrder(QDataStream::LittleEndian);
Items parts;
while (!stream.atEnd()) {
quint32 partId;
stream >> id;
char *bytes;
uint length;
stream.readBytes(bytes, length);
parts.append(QPair<quint32, QByteArray>(id, QByteArray(bytes, length)));
delete bytes;
}
return parts;
Since in python the numeric types do not match those of C++ then QDataStream no longer uses the ">>" operator to obtain the value but has specific methods such as readUInt32.
Considering the following code that generates the data:
The following code gets the data:
Output:
If PySide2 is used then the implementation changes a bit.
Output:
Update:
It is not possible to obtain the data if you do not use QDataStream since Qt uses its own format for each type of data, and this format is not a standard that can change with each version without notifying it. For this reason, the byteorder and the version of QDataStream used must be indicated.
Update 2
Assuming that the format that QDataStream uses to pack quint32 and bytes is QDataStream.Qt_5_15 then a possible implementation is:
Output: