On Wed, Jun 24, 2009 at 7:42 AM, Jeff Rush <j...@taupro.com> wrote: > 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. > > http://docs.python.org/library/struct.html
I was just scanning the replies wondering why someone has not mentioned "struct" so far! "struct" is very useful to pack data structures in/out of the wire. In fact if you want to convert your data structure to a bytestream, you should first investigate the possiblity of using "struct" before writing custom code or searching other solutions. > > 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. > > http://code.activestate.com/recipes/498149/ This seems like a very useful recipe. Often one requres something which can handle nested objects which struct cannot do, since it wants things to be presented to it as basic data types. > > > I'm probably going to do a source code walkthrough of this module with > the Dallas usergroup this Saturday because of its interesting parts. > > -Jeff > > _______________________________________________ > BangPypers mailing list > BangPypers@python.org > http://mail.python.org/mailman/listinfo/bangpypers > -- -Anand
_______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers