Class probkem - getting msg that self not defined

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Robert

    #1

    Class probkem - getting msg that self not defined

    Hi Everyone,

    I am having a problem with a class and hope you can help.

    When I try to use the class listed below, I get the statement that self
    is not defined.

    test=TriggerMes sage(data)
    var = test.decode(sel f.qname)

    I would have thought that self would have carried forward when I grabbed
    an instance of TriggerMessage.

    Any ideas on this?



    The class in question is:


    class TriggerMessage( object):

    def __init__(self,d ata):
    """
    Unpacks the passed binary data based on the MQTCM2 format dictated in
    the MQ Application Programming Reference
    """

    self.data=data
    self.structid=N one
    self.version=No ne
    self.qname=None
    self.procname=N one
    self.trigdata=N one
    self.appltype=N one
    self.applid=Non e
    self.envdata=No ne
    self.userdata=N one
    self.qmgr=None


    def decode(self):
    import struct
    format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
    size=struct.cal csize(format)
    self.data=data
    self.structid, self.version, self.qname, self.processnam e, \
    self.triggerdat a, self.appltype, self.applid, \
    self.envdata, self.userdata, self.qmgr \
    = struct.unpack(f ormat,self.data )
  • wes weston

    #2
    Re: Class probkem - getting msg that self not defined

    Andrew Robert wrote:[color=blue]
    > Hi Everyone,
    >
    > I am having a problem with a class and hope you can help.
    >
    > When I try to use the class listed below, I get the statement that self
    > is not defined.
    >
    > test=TriggerMes sage(data)[/color]

    self is not known here; only inside the class.
    [color=blue]
    > var = test.decode(sel f.qname)
    >
    > I would have thought that self would have carried forward when I grabbed
    > an instance of TriggerMessage.
    >
    > Any ideas on this?
    >
    >
    >
    > The class in question is:
    >
    >
    > class TriggerMessage( object):
    >
    > def __init__(self,d ata):
    > """
    > Unpacks the passed binary data based on the MQTCM2 format dictated in
    > the MQ Application Programming Reference
    > """
    >
    > self.data=data
    > self.structid=N one
    > self.version=No ne
    > self.qname=None
    > self.procname=N one
    > self.trigdata=N one
    > self.appltype=N one
    > self.applid=Non e
    > self.envdata=No ne
    > self.userdata=N one
    > self.qmgr=None
    >
    >
    > def decode(self):
    > import struct
    > format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
    > size=struct.cal csize(format)
    > self.data=data
    > self.structid, self.version, self.qname, self.processnam e, \
    > self.triggerdat a, self.appltype, self.applid, \
    > self.envdata, self.userdata, self.qmgr \
    > = struct.unpack(f ormat,self.data )[/color]

    Comment

    • Andrew Robert

      #3
      Re: Class probkem - getting msg that self not defined

      wes weston wrote:[color=blue]
      > Andrew Robert wrote:[color=green]
      >> Hi Everyone,
      >>
      >> I am having a problem with a class and hope you can help.
      >>
      >> When I try to use the class listed below, I get the statement that self
      >> is not defined.
      >>
      >> test=TriggerMes sage(data)[/color]
      >
      > self is not known here; only inside the class.
      >[color=green]
      >> var = test.decode(sel f.qname)
      >>[/color][/color]
      <snip>

      I guess I was barking up the wrong tree on that one.

      How would I go about getting the required values out of the class?

      Return self?

      Comment

      • wes weston

        #4
        Re: Class probkem - getting msg that self not defined

        Andrew Robert wrote:[color=blue]
        > wes weston wrote:[color=green]
        >> Andrew Robert wrote:[color=darkred]
        >>> Hi Everyone,
        >>>
        >>> I am having a problem with a class and hope you can help.
        >>>
        >>> When I try to use the class listed below, I get the statement that self
        >>> is not defined.
        >>>
        >>> test=TriggerMes sage(data)[/color]
        >> self is not known here; only inside the class.
        >>[color=darkred]
        >>> var = test.decode(sel f.qname)
        >>>[/color][/color]
        > <snip>
        >
        > I guess I was barking up the wrong tree on that one.
        >
        > How would I go about getting the required values out of the class?
        >
        > Return self?[/color]

        You can refer to the variables inside the class as test.version;
        for example - as they are not protected. Or, you could add access
        methods in the class like def GetVersion(self ): return self.version
        If you want to protect version, call it self.__version which mangles
        the name from outside.
        wes

        Comment

        • Dennis Benzinger

          #5
          Re: Class probkem - getting msg that self not defined

          wes weston schrieb:[color=blue]
          > Andrew Robert wrote:
          >[color=green]
          >> wes weston wrote:
          >>[color=darkred]
          >>> Andrew Robert wrote:
          >>>
          >>>> Hi Everyone,
          >>>>
          >>>> I am having a problem with a class and hope you can help.
          >>>>
          >>>> When I try to use the class listed below, I get the statement that self
          >>>> is not defined.
          >>>> test=TriggerMes sage(data)
          >>>
          >>> self is not known here; only inside the class.
          >>>
          >>>> var = test.decode(sel f.qname)
          >>>>[/color]
          >> <snip>
          >>
          >> I guess I was barking up the wrong tree on that one.
          >>
          >> How would I go about getting the required values out of the class?
          >>
          >> Return self?[/color]
          >
          >
          > You can refer to the variables inside the class as test.version;
          > for example - as they are not protected. Or, you could add access
          > methods in the class like def GetVersion(self ): return self.version
          > If you want to protect version, call it self.__version which mangles
          > the name from outside.
          > wes[/color]

          Or use the property function to define properties
          <http://docs.python.org/lib/built-in-funcs.html>.

          Dennis

          Comment

          • Bruno Desthuilliers

            #6
            Re: Class probkem - getting msg that self not defined

            Andrew Robert a écrit :[color=blue]
            > Hi Everyone,
            >
            > I am having a problem with a class and hope you can help.
            >
            > When I try to use the class listed below, I get the statement that self
            > is not defined.
            >
            > test=TriggerMes sage(data)
            > var = test.decode(sel f.qname)
            >
            > I would have thought that self would have carried forward when I grabbed
            > an instance of TriggerMessage.[/color]

            Hint : what is the name of your TriggerMessage instance in the above code ?

            I think that what you wannt is more like this :

            import struct

            class TriggerMessage( object):
            def __init__(self,d ata):
            """
            Unpacks the passed binary data based on the
            MQTCM2 format dictated in
            the MQ Application Programming Reference
            """
            self._format = '4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
            self._data = data

            (self.version,
            self.qname,
            self.processnam e,
            self.triggerdat a,
            self.appltype,
            self.applid,
            self.envdata,
            self.userdata,
            self.qmgr) = self._decode()

            def _decode(self):
            assert len(self._data) == struct.calcsize (self._format)
            return struct.unpack(s elf._format, self._data)

            # test code
            # data = ???
            test=TriggerMes sage(data)
            var = test.qname

            Comment

            • Andrew Robert

              #7
              Re: Class probkem - getting msg that self not defined


              Hey Bruno,


              Although I have not tested it, this appears to be it exactly.


              Some confusion though.

              <snip

              </snip>[color=blue]
              > import struct
              >
              > class TriggerMessage( object):
              > def __init__(self,d ata):
              > """
              > Unpacks the passed binary data based on the
              > MQTCM2 format dictated in
              > the MQ Application Programming Reference
              > """[/color]
              I am okay up to here :).

              After that, well..

              What does the _ before the variables mean?

              Why are you defining _format and _data here? I would have thought it
              belongs in the decode section.

              I think it is very slick but I would like to try and understand your
              approach.

              Also, why assert in calculating the struct size?

              Very cool how you did this.


              [color=blue]
              > self._format = '4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
              > self._data = data
              >
              > (self.version,
              > self.qname,
              > self.processnam e,
              > self.triggerdat a,
              > self.appltype,
              > self.applid,
              > self.envdata,
              > self.userdata,
              > self.qmgr) = self._decode()
              >
              > def _decode(self):
              > assert len(self._data) == struct.calcsize (self._format)
              > return struct.unpack(s elf._format, self._data)
              >
              > # test code
              > # data = ???
              > test=TriggerMes sage(data)
              > var = test.qname[/color]

              Comment

              • bruno at modulix

                #8
                Re: Class probkem - getting msg that self not defined

                Andrew Robert wrote:[color=blue]
                > Hey Bruno,
                >
                >
                > Although I have not tested it, this appears to be it exactly.
                >
                >
                > Some confusion though.
                >
                > <snip
                >
                > </snip>
                >[color=green]
                >>import struct
                >>
                >>class TriggerMessage( object):
                >> def __init__(self,d ata):
                >> """
                >> Unpacks the passed binary data based on the
                >> MQTCM2 format dictated in
                >> the MQ Application Programming Reference
                >> """[/color]
                >
                > I am okay up to here :).
                >
                > After that, well..
                >
                > What does the _ before the variables mean?[/color]

                It's a convention for implementation attributes (not part of the class
                public interface).
                [color=blue]
                > Why are you defining _format and _data here?[/color]

                Since the data object is passed at instanciation time (at least this was
                the case in your code), this is the only place where I can bind it to an
                instance attribute. As for the format string, it's really an attribute
                of the class, and something that won't change, so better to have it in
                the __init__ too. In fact, from what I saw of your code, it may as well
                be a class attribute (ie : defined outside a method, and shared by all
                instances), since the same format apply for all instances.
                [color=blue]
                > I would have thought it
                > belongs in the decode section.[/color]

                It is used in the _decode method, but that does not mean it must be
                defined in the _decode method.
                [color=blue]
                > I think it is very slick but I would like to try and understand your
                > approach.
                >
                > Also, why assert in calculating the struct size?[/color]

                You don't *need* this calculation. struct.calcsize () is meant to let do
                you check that the format string and the data string match (at least in
                size). The assert here is mostly for developpement.. .


                --
                bruno desthuilliers
                python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                p in 'onurb@xiludom. gro'.split('@')])"

                Comment

                Working...