[BangPypers] calling instance of the other class.

Jeff Rush jeff at taupro.com
Wed Jun 24 04:12:33 CEST 2009

learningpython wrote:
> Hi Jeff,
> Can i ask you a related question. Long shot ..
> but since you have good idea of area i am working ( which is communications,
> sending, decoding, encoding) data over a serial interface. 
> I have a problem, i am facing.. Like in ASN format..
> I have a Data Unit ( am defining a class for each )
> 1. msg type  ( 8 bits )
> 2. Identity(8 bits)
> 3. ADESC ( 8 bits)
> 4. Address ( Now this depends on value of ADESC ) 
> so the other elements, i am allocating the memory, but how can i allocate
> Address, the number of fields ( i mean size) depends on the ASESC value so
> it could be any value .. how can i say reserve memory .
> The only idea i get is to have a function within the class of this
> definition where, i read the ADESC and then allocate value in address. Any
> other good ideas, please can you let me know. I really appreciate your
> links.

The important module for bit manipulation in Python in the struct
module.  If you haven't come across it before, you should read the docs.
 It is part of the Python standard library.


Once you've done that, check out this cool module that does much of what
you are trying to do.  Fair warning though, it uses very advanced Python
coding including metaclasses.  You'll learn a lot by carefully figuring
out how it does what it does.  For example it avoids the need for a
self.order list to sequence the fields of a class definition in a rather
clever fashion.


I'm probably going to do a source code walkthrough of this module with
the Dallas usergroup this Saturday because of its interesting parts.


More information about the BangPypers mailing list