struct.Struct random access

castironpi castironpi at gmail.com
Tue Aug 26 15:37:37 EDT 2008


I'd like to seriously nominate this idea and get a considered opinion
on it.

struct.Struct lets you encode Python objects into structured memory.
It accepts a format string, and optionally a buffer and offset to/from
which to read/write the structure.  What do you think of random access
to the results?  To avoid Marc's concern about meaningless anonymous
record entries, model the constructor after 'namedtuple' type.

(unproduced)
>>> packer= struct.Struct( 'IIIf255p', 'i1 i2 i3 afloat name' )
>>> packer.pack_into( buf, off, 10, 20, 30, 0.5, 'abc' )
>>> packer.unpack_from( buf, off, 'i3' )
30
>>> packer.unpack_from( buf, off )
( 10, 20, 30, 0.5, 'abc' )
>>> packer.pack_into( buf, off, 'i1', 12 )

Even in sequential access speed benefits, by avoiding the construction
of n-1 objects.

PS: If you don't have time or don't want to consider it seriously, or
want to see more specifics, just say so.



More information about the Python-list mailing list