Need a structure with multiple members

William Dandreta wjdandreta at worldnet.att.net
Tue Aug 8 14:25:06 EDT 2000


Hi Thomas,

I am a Python novice but here are a couple of lines I use to read part of a
dbf header that might give you an idea how to manipulate structs.

hdra = DBF.read(32)
numRecs,hdrSize,recSize = unpack('lhh',hdra[4:12])

here's how I write it:

dbfout.write(pack('4bl2h20s',
                   hdr.tableType,
                   hdr.year,
                   hdr.month,
                   hdr.day,
                   hdr.numRecs,
                   hdr.hdrSize,
                   hdr.recSize,
                   20*chr(0)))

I think the idea of pack and unpack is to unpack the info to manipulate it
in Python and pack it to store it back to the file.

Bill
Thomas Gagne wrote in message <398F723D.D7D7B6B2 at ix.netcom.com>...
>The struct module is interesting for creating a string encoded with binary
>data, but the pack() and unpack() functions don't seem to describe how once
>created you would safely access members of that structure.
>
>For instance, I want to create a class that mirrors the C header:
>
>typedef struct {
>    int32_t len;
>    int32_t sequence;
>    int16_t reply;
>    int16_t error;
>    /* blah blah blah for 64 bytes total */
>} isdHeader;
>
>It as though I coudl start my isdHeader class as:
>
>class IsdHeader:
>    def __init__(self):
>        self.data = struct.pack('llhh', 0, 0, 0, 0)
>
>But I would like to create member functions (methods) for accessing the
>various members, such that:
>
>h = IsdHeader();
>h.len(20) // sets h.len to 20
>h.len()   // returns the value of h.len (the member)
>
>I'm kinda of reading, "Quick Python" and trying to code the interface
library
>at the same time, and I haven't read everything yet...
>





More information about the Python-list mailing list