Class probkem - getting msg that self not defined

A

Andrew Robert

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=TriggerMessage(data)
var = test.decode(self.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,data):
"""
Unpacks the passed binary data based on the MQTCM2 format dictated in
the MQ Application Programming Reference
"""

self.data=data
self.structid=None
self.version=None
self.qname=None
self.procname=None
self.trigdata=None
self.appltype=None
self.applid=None
self.envdata=None
self.userdata=None
self.qmgr=None


def decode(self):
import struct
format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
size=struct.calcsize(format)
self.data=data
self.structid, self.version, self.qname, self.processname, \
self.triggerdata, self.appltype, self.applid, \
self.envdata, self.userdata, self.qmgr \
= struct.unpack(format,self.data)
 
W

wes weston

Andrew said:
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=TriggerMessage(data)

self is not known here; only inside the class.
 
A

Andrew Robert

wes said:
self is not known here; only inside the class.
<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?
 
W

wes weston

Andrew said:
<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?

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
 
D

Dennis Benzinger

wes said:
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

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

Dennis
 
A

Andrew Robert

Hey Bruno,


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


Some confusion though.

<snip

import struct

class TriggerMessage(object):
def __init__(self,data):
"""
Unpacks the passed binary data based on the
MQTCM2 format dictated in
the MQ Application Programming Reference
"""
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.
 
B

Bruno Desthuilliers

Andrew Robert a écrit :
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=TriggerMessage(data)
var = test.decode(self.qname)

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

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,data):
"""
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.processname,
self.triggerdata,
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(self._format, self._data)

# test code
# data = ???
test=TriggerMessage(data)
var = test.qname
 
B

bruno at modulix

Andrew said:
Hey Bruno,


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


Some confusion though.

<snip



I am okay up to here :).

After that, well..

What does the _ before the variables mean?

It's a convention for implementation attributes (not part of the class
public interface).
Why are you defining _format and _data here?

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.
I would have thought it
belongs in the decode section.

It is used in the _decode method, but that does not mean it must be
defined in the _decode method.
I think it is very slick but I would like to try and understand your
approach.

Also, why assert in calculating the struct size?

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top