On Thu, 2005-01-06 at 13:17 +0000, Michael Hudson wrote: > Ilya Sandler <[EMAIL PROTECTED]> writes: > > > A problem: > > > > The current struct.unpack api works well for unpacking C-structures where > > everything is usually unpacked at once, but it > > becomes inconvenient when unpacking binary files where things > > often have to be unpacked field by field. Then one has to keep track > > of offsets, slice the strings,call struct.calcsize(), etc... > > IMO (and E), struct.unpack is the primitive atop which something more > sensible is built. I've certainly tried to build that more sensible > thing at least once, but haven't ever got the point of believing what > I had would be applicable to the general case... maybe it's time to > write such a thing for the standard library.
I've been using this simple wrapper:
def stream_unpack(stream, format):
return struct.unpack(format, stream.read(struct.calcsize(format)))
It works with file-like objects, such as file, StringIO,
socket.makefile(), etc. Working with streams is useful because
sometimes you don't know how much you need to read to decode a message
in advance.
Regards.
>
> Cheers,
> mwh
>
--
Gustavo J. A. M. Carneiro
<[EMAIL PROTECTED]> <[EMAIL PROTECTED]>
The universe is always one step beyond logic.
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
