[Pythonmac-SIG] convert binary plist to xml string
Bill Janssen
janssen at parc.com
Mon May 16 19:31:39 CEST 2005
> first, second, third, fourth, fifth, ..... = struct.unpack
> ("iiIIiIiiILllfd", data)
>
> It is SO EASY to screw this up. Pick the wrong type code, or
> misalign the type and field. It needs to look more like something sane:
>
> class Point(struct.BigEndianStruct):
> x = struct.SInt32()
> y = struct.SInt32()
Why not (for yourself) just add a module which addresses some of this?
As in:
from unpacking import *
def getPoint (bvar):
unpack = unpacking(bvar)
x = unpack(S_INT_32)
y = unpack(S_INT_32)
============================== unpacking.py ============================
import struct
S_INT_32 = "i"
U_INT_32 = "I"
## ... etc. ...
class unpacker:
def __init__(self, bdata):
if type(bdata) != type(''):
raise ValueError("type of bdata (%s) should be simple str" % bdata)
self.bdata = bdata
def unpack(self, typespec):
if not self.bdata:
raise ValueError("data source exhausted")
if len(self.bdata) > 0:
valsize = struct.calcsize(typespec)
if len(self.bdata) < valsize:
raise ValueError("data source exhausted")
val = struct.unpack(typespec, self.bdata[:valsize])
self.bdata = self.bdata[valsize:]
return val
def unpacking(bdata):
return (lambda x, y=unpacker(bdata): y.unpack(x))
More information about the Pythonmac-SIG
mailing list