Packing a simple dictionary into a string - extending struct?
Jonathan Fine
jfine at pytex.org
Fri Jun 22 19:26:30 EDT 2007
John Machin wrote:
> def unpack(bytes, unpack_entry=unpack_entry):
> '''Return dictionary gotten by unpacking supplied bytes.
> Both keys and values in the returned dictionary are byte-strings.
> '''
> bytedict = {}
> ptr = 0
> while 1:
> key, val, ptr = unpack_entry(bytes, ptr)
> bytedict[key] = val
> if ptr == len(bytes):
> break
> # That's beautiful code -- as pretty as a cane-toad.
Well, it's nearly right. It has a transposition error.
> # Well-behaved too, a very elegant response to unpack(pack({}))
Yes, you're right. An attempt to read bytes that aren't there.
> # Try this:
> blen = len(bytes)
> while ptr < blen:
> key, val, ptr = unpack_entry(bytes, ptr)
> bytedict[key] = val
>
> return bytedict
I've committed such a change. Thank you.
--
Jonathan
More information about the Python-list
mailing list