Re: [Tutor] Need help with using methods in a base class
Hi. It worked. class LightsHandle(Parallel): ... def __init__(self): ... Parallel.__init__(self) ... def __del__(self): ... Parallel.__del__(self) ... def setLatch(self, x, y, z): ... self.setDataStrobe(x) ... print 'Data Strobe set. \n' ... self.setAutoFeed(y) ... print 'AutoFeed set. \n' ... self.setInitOut(z) ... print 'InitOut set. \n' ... def generateClockPulse(self): ... self.setSelect(0) ... print 'Select set to 0. \n' ... self.setSelect(1) ... print 'Select set to 1. \n' Just to answer some questions. Is _fd initialized in Parallel.__init__() ? Yes it is. I already tried using the base class and it works just fine. from parallel import Parallel p = Parallel() p.setData(0xFF) Note this is a different value than you used above, is that significant? No, not really. It's just what I want to output. 0xFF would mean all 8 LEDs are off while 0xF0 would mean only half the lights are on. Thank you very much. On Mon, Sep 8, 2008 at 2:35 AM, Kent Johnson [EMAIL PROTECTED] wrote: On Sun, Sep 7, 2008 at 11:07 AM, Roy Khristopher Bayot [EMAIL PROTECTED] wrote: Hi. I added self to parts of the code. But after making an instance and using the setData method it gave out an AttributeError. from parallel import Parallel class LightsHandle(Parallel): ... def __init__(self): ... pass This will *prevent* Parallel.__init__() from being called. I guess this is not what you want, it is probably the cause of your trouble. Is _fd initialized in Parallel.__init__() ? ... def setData(self, data): ... Parallel.setData(self, data) This method is not needed at all. If you omit it, the base class method will be called automatically when you call setData() on a LightsHandle instance. ... def setLatch(self, latch): ... Parallel.setDataStrobe(self, int(latch[0])) ... Parallel.setAutoFeed(self, int(latch[1])) ... Parallel.setInitOut(self, int(latch[2])) This could be written more simply and idiomatically as ... def setLatch(self, x, y, z): ... self.setDataStrobe(x) ... self.setAutoFeed(y) ... self.setInitOut(z) Since you have not overridden these methods you can call them directly. ... def generateClockPulse(self): ... Parallel.setSelect(self, 0) ... Parallel.setSelect(self, 1) Same here. a = LightsHandle() a.setData(0xF0) Traceback (most recent call last): File stdin, line 1, in module File stdin, line 5, in setData File /usr/lib/python2.5/site-packages/parallel/parallelppdev.py, line 563, in setData return self.PPWDATA(d) File /usr/lib/python2.5/site-packages/parallel/parallelppdev.py, line 465, in PPWDATA fcntl.ioctl(self._fd, PPWDATA,struct.pack('B',byte)) AttributeError: LightsHandle instance has no attribute '_fd' Does this mean I have to make '_fd' in class LightsHandle? I thought that it would be somewhat messy. And there might be other variables that werent accounted for. Probably it means you have to call the base class __init__(). a = LightsHandle() a.setData(0xF0) There were no errors thrown. But the problem is that it doesnt work. I already tried using the base class and it works just fine. from parallel import Parallel p = Parallel() p.setData(0xFF) Note this is a different value than you used above, is that significant? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with using methods in a base class
On Mon, Sep 8, 2008 at 9:21 AM, Roy Khristopher Bayot [EMAIL PROTECTED] wrote: Hi. It worked. :-) class LightsHandle(Parallel): ... def __init__(self): ... Parallel.__init__(self) ... def __del__(self): ... Parallel.__del__(self) These two methods are not needed. If you omit them, the base class methods will be called by default. You only need a subclass __init__() method if it is doing initialization specific to the subclass. It is very rare to define a __del__() method at all. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with using methods in a base class
Hi. I added self to parts of the code. But after making an instance and using the setData method it gave out an AttributeError. from parallel import Parallel class LightsHandle(Parallel): ... def __init__(self): ... pass ... def setData(self, data): ... Parallel.setData(self, data) ... def setLatch(self, latch): ... Parallel.setDataStrobe(self, int(latch[0])) ... Parallel.setAutoFeed(self, int(latch[1])) ... Parallel.setInitOut(self, int(latch[2])) ... def generateClockPulse(self): ... Parallel.setSelect(self, 0) ... Parallel.setSelect(self, 1) ... a = LightsHandle() a.setData(0xF0) Traceback (most recent call last): File stdin, line 1, in module File stdin, line 5, in setData File /usr/lib/python2.5/site-packages/parallel/parallelppdev.py, line 563, in setData return self.PPWDATA(d) File /usr/lib/python2.5/site-packages/parallel/parallelppdev.py, line 465, in PPWDATA fcntl.ioctl(self._fd, PPWDATA,struct.pack('B',byte)) AttributeError: LightsHandle instance has no attribute '_fd' Does this mean I have to make '_fd' in class LightsHandle? I thought that it would be somewhat messy. And there might be other variables that werent accounted for. So I did something else. class LightsHandle(Parallel): ... def __init__(self): ... Parallel.__init__(self, port = 0) ... def __del__(self): ... Parallel.__del__(self) ... def setData(self, data): ... Parallel.setData(self, data) ... def setLatch(self, latch): ... Parallel.setDataStrobe(self, int(latch[0])) ... Parallel.setAutoFeed(self, int(latch[1])) ... Parallel.setInitOut(self, int(latch[2])) ... def generateClockPulse(self): ... Parallel.setSelect(self,0) ... Parallel.setSelect(self,1) ... a = LightsHandle() a.setData(0xF0) a.setLatch('111') a.generateClockPulse() a.setLatch('110') a.generateClockPulse() a.setData(0x0F) a.setLatch('111') a.generateClockPulse() There were no errors thrown. But the problem is that it doesnt work. This was suppose to control 32 LEDs (8 LEDs per latch, 6 latches total). Method setData() is suppose to control the 8 bits that you want to output on a specific latch. Method setLatch() will specify the latch. Method generateClockPulse() will cause the specific latch to take in the data and retain it there. But it doesnt work. The LEDs couldnt be controlled. I already tried using the base class and it works just fine. from parallel import Parallel p = Parallel() p.setData(0xFF) p.setDataStrobe(1) p.setAutoFeed(1) p.setInitOut(1) p.setSelect(0) p.setSelect(1) So I thought maybe there was something wrong with setLatch() since elements of the string were converted to integers. I changed the code again. class LightsHandle(Parallel): ... def __init__(self): ... Parallel.__init__(self, port = 0) ... def __del__(self): ... Parallel.__del__(self) ... def setData(self, data): ... Parallel.setData(self, data) ... def setLatch(self, x, y, z): ... Parallel.setDataStrobe(self, x) ... Parallel.setAutoFeed(self, y) ... Parallel.setInitOut(self, z) ... def generateClockPulse(self): ... Parallel.setSelect(self,0) ... Parallel.setSelect(self,1) ... a = LightsHandle() a.setData(0xF0) a.setLatch(1,1,1) a.generateClockPulse() a.setData(0x0F) a.setLatch(1,1,1) a.generateClockPulse() But no LED lights were changing as it was suppose to. Any ideas on what I dont know or what I've overlooked? On Sun, Sep 7, 2008 at 6:07 AM, Alan Gauld [EMAIL PROTECTED]wrote: Roy Khristopher Bayot [EMAIL PROTECTED] wrote ... def generateClockPulse(self): ... parallel.Parallel.setSelect(0) ... parallel.Parallel.setSelect(1) ... a = LightsHandle() a.setD(0xF0) Traceback (most recent call last): File stdin, line 1, in module File stdin, line 5, in setD TypeError: unbound method setData() must be called with Parallel instance as first argument (got int instance instead) The error merssage gives the clue. Sonce you are calling a method on a classs not an object you need to pass in the instance reference explicitly. Its exactly as in calling the base constructor in __init__ class C(P): def __init__(self): P.__init__(self)# need to pass self here (Some notes: I changed setData() to setD() so that there wont be a confusion. Method setData() is from the base class Parallel. Although I think setData() could be overriden.) Thats not necessary, each class creates its owen namespace. What have I been doing wrong? Why does it say that I need a Parallel instance? Because the class needs to know where to find the instance variables. Since you are calling the class directly, not via an instance you have to pass self explicitly.
Re: [Tutor] Need help with using methods in a base class
On Sun, Sep 7, 2008 at 11:07 AM, Roy Khristopher Bayot [EMAIL PROTECTED] wrote: Hi. I added self to parts of the code. But after making an instance and using the setData method it gave out an AttributeError. from parallel import Parallel class LightsHandle(Parallel): ... def __init__(self): ... pass This will *prevent* Parallel.__init__() from being called. I guess this is not what you want, it is probably the cause of your trouble. Is _fd initialized in Parallel.__init__() ? ... def setData(self, data): ... Parallel.setData(self, data) This method is not needed at all. If you omit it, the base class method will be called automatically when you call setData() on a LightsHandle instance. ... def setLatch(self, latch): ... Parallel.setDataStrobe(self, int(latch[0])) ... Parallel.setAutoFeed(self, int(latch[1])) ... Parallel.setInitOut(self, int(latch[2])) This could be written more simply and idiomatically as ... def setLatch(self, x, y, z): ... self.setDataStrobe(x) ... self.setAutoFeed(y) ... self.setInitOut(z) Since you have not overridden these methods you can call them directly. ... def generateClockPulse(self): ... Parallel.setSelect(self, 0) ... Parallel.setSelect(self, 1) Same here. a = LightsHandle() a.setData(0xF0) Traceback (most recent call last): File stdin, line 1, in module File stdin, line 5, in setData File /usr/lib/python2.5/site-packages/parallel/parallelppdev.py, line 563, in setData return self.PPWDATA(d) File /usr/lib/python2.5/site-packages/parallel/parallelppdev.py, line 465, in PPWDATA fcntl.ioctl(self._fd, PPWDATA,struct.pack('B',byte)) AttributeError: LightsHandle instance has no attribute '_fd' Does this mean I have to make '_fd' in class LightsHandle? I thought that it would be somewhat messy. And there might be other variables that werent accounted for. Probably it means you have to call the base class __init__(). a = LightsHandle() a.setData(0xF0) There were no errors thrown. But the problem is that it doesnt work. I already tried using the base class and it works just fine. from parallel import Parallel p = Parallel() p.setData(0xFF) Note this is a different value than you used above, is that significant? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with using methods in a base class
On Sat, Sep 6, 2008 at 12:04 PM, Roy Khristopher Bayot [EMAIL PROTECTED] wrote: Hi. I am having some difficulty using methods from a base class. I have 2 classes. The first one is Parallel which is inside the module parallel. The methods useful to me from this class are setDataStrobe(), setAutoFeed(), setInitOut(), setSelect(), and setData(). The second one is derived from the first. I called it LightsHandle. The code and traceback is written below: import parallel class LightsHandle(parallel.Parallel): ... def __init__(self): ... pass ... def setData(self, data): ... Parallel.setData(data) ... def setLatch(self, latch): ... Parallel.setDataStrobe(int(latch[0])) ... Parallel.setAutoFeed(int(latch[1])) ... Parallel.setInitOut(int(latch[2])) ... def generateClockPulse(self): ... Parallel.setSelect(0) ... Parallel.setSelect(1) ... a = LightsHandle() a.setData(0xF0) Traceback (most recent call last): File stdin, line 1, in module File stdin, line 5, in setData NameError: global name 'Parallel' is not defined This error is because you did not import Parallel, you imported parallel. You have to use the full name parallel.Parallel as you figured out. You could also use from parallel import Parallel and then just refer to Parallel. So I tried another one. And the code and traceback is written below. import parallel class LightsHandle(parallel.Parallel): ... def __init__(self): ... pass ... def setD(self, data): ... parallel.Parallel.setData(data) ... def setLatch(self, latch): ... parallel.Parallel.setDataStrobe(int(latch[0])) ... parallel.Parallel.setAutoFeed(int(latch[1])) ... parallel.Parallel.setInitOut(int(latch[2])) ... def generateClockPulse(self): ... parallel.Parallel.setSelect(0) ... parallel.Parallel.setSelect(1) ... a = LightsHandle() a.setD(0xF0) Traceback (most recent call last): File stdin, line 1, in module File stdin, line 5, in setD TypeError: unbound method setData() must be called with Parallel instance as first argument (got int instance instead) (Some notes: I changed setData() to setD() so that there wont be a confusion. Method setData() is from the base class Parallel. Although I think setData() could be overriden.) What have I been doing wrong? Why does it say that I need a Parallel instance? According to http://www.python.org/doc/2.5/tut/node11.html#SECTION001134 There is a simple way to call the base class method directly: just call BaseClassName.methodname(self, arguments). This is occasionally useful to clients as well. (Note that this only works if the base class is defined or imported directly in the global scope.) This quote has the answer to your question. When you call the base class method, you have to explicitly provide the self parameter, e.g. parallel.Parallel.setData(self, data) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with using methods in a base class
Roy Khristopher Bayot [EMAIL PROTECTED] wrote ... def generateClockPulse(self): ... parallel.Parallel.setSelect(0) ... parallel.Parallel.setSelect(1) ... a = LightsHandle() a.setD(0xF0) Traceback (most recent call last): File stdin, line 1, in module File stdin, line 5, in setD TypeError: unbound method setData() must be called with Parallel instance as first argument (got int instance instead) The error merssage gives the clue. Sonce you are calling a method on a classs not an object you need to pass in the instance reference explicitly. Its exactly as in calling the base constructor in __init__ class C(P): def __init__(self): P.__init__(self)# need to pass self here (Some notes: I changed setData() to setD() so that there wont be a confusion. Method setData() is from the base class Parallel. Although I think setData() could be overriden.) Thats not necessary, each class creates its owen namespace. What have I been doing wrong? Why does it say that I need a Parallel instance? Because the class needs to know where to find the instance variables. Since you are calling the class directly, not via an instance you have to pass self explicitly. Remember that in class C: def m(self, x): print x c = C(42) Then c.m() is the same as C.m(c,42). self takes on the value of the instance in the first case but in the second you have to pass it directly. When you are inside a methiod you always use the second form so you always need to pass self. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor