[BangPypers] calling instance of the other class.

Jeff Rush jeff at taupro.com
Tue Jun 23 10:50:42 CEST 2009


learningpython wrote:
> Sorry Senthil and all,
> I am sure, it's pretty easy but don't i am not able to catch it. In C, i
> just pass the & of the another structure in the structure element, some
> thing close.

The equivalent to the & operator in C is just an object reference in Python.

> class Send_Msg_req(msg):
>      def __init__(self):
>        self.usr_mac =< Earlier class defined > 
>        self.msgtype = <blah>
>        self.msgsize = <blah>
>        self.order = ['usr_mac', 
>                       'msgtype ',
>                        'msgsize ']

so the line becomes just:

  self.usr.mac = usr_mac()

where you declared usr_mac as a class in your prior email.  If the
usr_mac() call needs arguments, provide them in the __init__ for
Send_MSG_req and pass them through:

  class Send_Msg_req(msg):
      def __init__(self, a, b):
          self.usr_mac = usr_mac(a, b)
          self.msgtype = BV.Bit24.new()

You haven't mentioned it but from here you'll need a flattener.  From
your code I presume you have the idea for one already, in that:

>        self.msgtype = <blah>
>        self.msgsize = <blah>
>        self.order = ['usr_mac',
>                       'msgtype ',
>                        'msgsize ']

at some point you have a method defined for the base 'msg' class that
uses the self.order list to construct the actual bitstream going out the
serial port from the various object attributes.  So your BV.Bit24 class
has some method that gives you back the 24-bits that make it up.  The
method that walks self.order and returns the raw bits is a flattener.

Now with the declaring of self.usr_mac referencing an earlier class, you
just need to extend the idea of a flattener a bit further.  So when you
call:

  msg = Send_Msg_req(MSG_A, 34)
  serial.output(msg.flatten())

that .flatten method, upon encountering the self.usr_mac attribute,
calls the .flatten method of the usr_mac class and merges those bits
into the stream it is building for the Send_Msg_req class.

This is why it is called a flattener -- it walks a tree of msg fragments
and constructs a flat sequence of bits making up a single message.

Another example of the use of a flattener is the STAN DOM used in the
Nevow templating module used with the Twisted web framework.

  http://www.kieranholland.com/code/documentation/nevow-stan/

It takes an object graph of nested lists and attributes and returns a
flat string of HTML.  It goes a bit further in that it has an extensible
registry of adapters for teaching the system to handle new kinds of
object graph nodes.

-Jeff


More information about the BangPypers mailing list