[PEAK] Circular class adapters..

Phillip J. Eby pje at telecommunity.com
Fri Feb 20 00:11:49 EST 2004


At 11:34 PM 2/19/04 -0500, Bob Ippolito wrote:
>The str/int subclasses are to maintain metadata.. you don't know if it's 
>supposed to be a pascal string or a c string or a short integer or a byte 
>if you don't carry around metadata.

Huh?  You lost me there.  If it's part of a structure type, wouldn't the 
structure type know what the type of a part of it is supposed to 
be?  Meanwhile, strings and ints aren't mutable.  So I guess maybe I missed 
something.


>Some code using the original (no adaptation whatsoever) ptypes looked like 
>this:
>
>class mach_header(Structure):
>     _fields_ = (
>         ('magic', p_ulong),
>         ('cputype', cpu_type_t),
>         ('cpusubtype', cpu_subtype_t),
>         ('filetype', p_ulong),
>         ('ncmds', p_ulong),
>         ('sizeofcmds', p_ulong),
>         ('flags', p_ulong),
>     )
>     def _describe(self):
>         bit = 1L
>         flags = self.flags
>         dflags = []
>         while flags and bit < (1<<32L):
>             if flags & bit:
>                 dflags.append(MH_FLAGS_NAMES.get(bit, str(bit)))
>                 flags = flags ^ bit
>             bit <<= 1L
>         return (
>             ('magic', '0x%08X' % self.magic),
>             ('cputype', CPU_TYPE_NAMES.get(self.cputype, self.cputype)),
>             ('cpusubtype', self.cpusubtype),
>             ('filetype', MH_FILETYPE_NAMES.get(self.filetype, 
> self.filetype)),
>             ('ncmds', self.ncmds),
>             ('sizeofcmds', self.sizeofcmds),
>             ('flags', dflags),
>         )

You know, this looks a lot more like a job for peak.model.  E.g.

class mach_header(model.Element):

    class magic(model.Attribute):
        sortPosn = 1
        referencedType = model.ULong
        packFormat = ...

    class cputype(model.Attribute):
        ...

etc.  Technically, you'd create a base class that walked 
self.__class__.mdl_features, and you'd create subclasses of model.ULong 
etc. that had the right formatting for their input and output.  Some of the 
protocol stuff would also come in handy here and there.  You could also use 
binding.classAttr(binding.Make(...)) calls to cache class-specific data the 
first time it's used by a particular subclass.  For example, suppose you 
wanted to have a binding for a 'pack' string to be used for a given 
class.  In your base class, you'd define a classAttr(Make()) that computes 
it from the mdl_features list.  Each subclass will have its own value for 
that attribute, computed the first time it's used by an instance of that 
subclass.

Anyway, at least within PEAK, peak.model would be the place I'd start if I 
was developing something that needed to manipulate externally-defined 
formats like these.  I'd start with just what's there, and then gradually 
add Element and Attribute subclasses, sprinkled with a bit of PyProtocols 
stuff as the framework evolved.




More information about the PEAK mailing list