[Tutor] Need help with using methods in a base class
Roy Khristopher Bayot
roybayot at gmail.com
Mon Sep 8 15:21:20 CEST 2008
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 <kent37 at tds.net> wrote:
> On Sun, Sep 7, 2008 at 11:07 AM, Roy Khristopher Bayot
> <roybayot at gmail.com> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080908/a23d8dc6/attachment.htm>
More information about the Tutor
mailing list