473,549 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 )
May 22 '06 #1
7 1893
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)

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 )

May 22 '06 #2
wes weston wrote:
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)

<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?
May 22 '06 #3
Andrew Robert wrote:
wes weston wrote:
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)

<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
May 22 '06 #4
wes weston schrieb:
Andrew Robert wrote:
wes weston wrote:
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)

<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


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

Dennis
May 22 '06 #5
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=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.


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
May 22 '06 #6

Hey Bruno,
Although I have not tested it, this appears to be it exactly.
Some confusion though.

<snip

</snip>
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
""" 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.
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

May 22 '06 #7
Andrew Robert wrote:
Hey Bruno,
Although I have not tested it, this appears to be it exactly.
Some confusion though.

<snip

</snip>
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
"""
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.. .
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
May 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1796
by: Rim | last post by:
Hi, It appears to me the simplest way to add a function to a class outside of the class declaration is as follows: >>> class a(object): .... pass .... >>> def f(self): .... print 'hi'
2
9583
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A, while an instance of class C should be able to check all methods #defined in C, B and A. #------------------------------------------------
2
2193
by: Jerry | last post by:
My "main" class is getting a bit long...Is it possble to split a class definition into several files and then import the pieces to get the whole definition? Jerry
8
2522
by: Mark English | last post by:
I'd like to write a Tkinter app which, given a class, pops up a window(s) with fields for each "attribute" of that class. The user could enter values for the attributes and on closing the window would be returned an instance of the class. The actual application I'm interested in writing would either have simple type attributes (int, string,...
166
8527
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
10
1624
by: David Hirschfield | last post by:
Here's a strange concept that I don't really know how to implement, but I suspect can be implemented via descriptors or metaclasses somehow: I want a class that, when instantiated, only defines certain methods if a global indicates it is okay to have those methods. So I want something like: global allow allow =
5
1462
by: Russell Warren | last post by:
I just ran across a case which seems like an odd exception to either what I understand as the "normal" variable lookup scheme in an instance/object heirarchy, or to the rules regarding variable usage before creation. Check this out: >>> class foo(object): .... I = 1 .... def __init__(self): .... print self.__dict__ .... ...
3
1634
by: Luis P. Mendes | last post by:
Hi, I have the following problem: I instantiate class Sistema from another class. The result is the same if I import it to interactive shell. s = Sistema("par") class Sistema:
4
2953
by: harijay | last post by:
Hi I am new to writing module and object oriented python code. I am trying to understand namespaces and classes in python. I have the following test case given in three files runner , master and child. I am getting an error within child where in one line it understands variable master.name and in the next line it gives a NameError as given...
0
7527
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7459
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7726
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7967
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7485
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5377
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1953
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.