struct: type registration?
Giovanni Bajo
noway at sorry.com
Wed May 31 20:50:37 EDT 2006
Hello,
given the ongoing work on struct (which I thought was a dead module), I was
wondering if it would be possible to add an API to register custom parsing
codes for struct. Whenever I use it for non-trivial tasks, I always happen to
write small wrapper functions to adjust the values returned by struct.
An example API would be the following:
============================================
def mystring_len():
return 20
def mystring_pack(s):
if len(s) > 20:
raise ValueError, "a mystring can be at max 20 chars"
s = (s + "\0"*20)[:20]
s = struct.pack("20s", s)
return s
def mystring_unpack(s):
assert len(s) == 20
s = struct.unpack("20s", s)[0]
idx = s.find("\0")
if idx >= 0:
s = s[:idx]
return s
struct.register("S", mystring_pack, mystring_unpack, mystring_len)
# then later
foo = struct.unpack("iilS", data)
============================================
This is only an example, any similar API which might fit better struct
internals would do as well.
As shown, the custom packer/unpacker can call the original pack/unpack as a
basis for their work. I guess an issue with this could be the endianess
problem: it would make sense if, when called recursively, struct.pack/unpack
used by the default the endianess specified by the external format string.
--
Giovanni Bajo
More information about the Python-list
mailing list