Mailing List Archive

Why do I always get an exception raised in this __init__()?
I'm obviously doing something very silly here but at the moment I
can't see what.

Here's the code:-

#!/usr/bin/python3
#
#
# GPIO
#
import gpiod
#
#
# Simple wrapper class for gpiod to make set and clearing outputs
easier
#
class Gpiopin:

def __init__(self, pin):
#
#
# scan through the GPIO chips to find the line/pin we want
#
for c in ['gpiochip0', 'gpiochip1', 'gpiochip2', 'gpiochip3']:

chip = gpiod.Chip(c)
for l in range(32):
line = chip.get_line(l)
if pin in line.name():
print("Found: ", line.name())
return
else:
raise ValueError("Can't find pin '" + pin + "'")

def print_name(self):
print (self.line.name())

def set(self):
self.line.set_value(1)

def clear(self):
self.line.set_value(0)


This is by no means the final code, the print() in the __init__() is
just a diagnostic for example. However I really can't understand why I
see the following when I try it:-

>>> import ngp
>>> ngp.Gpiopin("P9_23")
Found: P9_23
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/chris/.cfg/hosts/bbb/bin/ngp.py", line 24, in __init__
return
ValueError: Can't find pin 'P9_23'
>>>

Does a return in __init__() not do what I think it does?

How else could/should I do this?





--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list
Re: Why do I always get an exception raised in this __init__()? [ In reply to ]
On Thu, Aug 31, 2023 at 3:19?PM Chris Green via Python-list
<python-list@python.org> wrote:
>
> I'm obviously doing something very silly here but at the moment I
> can't see what.
>
> Here's the code:-
>
> #!/usr/bin/python3
> #
> #
> # GPIO
> #
> import gpiod
> #
> #
> # Simple wrapper class for gpiod to make set and clearing outputs
> easier
> #
> class Gpiopin:
>
> def __init__(self, pin):
> #
> #
> # scan through the GPIO chips to find the line/pin we want
> #
> for c in ['gpiochip0', 'gpiochip1', 'gpiochip2', 'gpiochip3']:
>
> chip = gpiod.Chip(c)
> for l in range(32):
> line = chip.get_line(l)
> if pin in line.name():
> print("Found: ", line.name())
> return
> else:
> raise ValueError("Can't find pin '" + pin + "'")
>
> def print_name(self):
> print (self.line.name())
>
> def set(self):
> self.line.set_value(1)
>
> def clear(self):
> self.line.set_value(0)
>
>
> This is by no means the final code, the print() in the __init__() is
> just a diagnostic for example. However I really can't understand why I
> see the following when I try it:-
>
> >>> import ngp
> >>> ngp.Gpiopin("P9_23")
> Found: P9_23
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/home/chris/.cfg/hosts/bbb/bin/ngp.py", line 24, in __init__
> return
> ValueError: Can't find pin 'P9_23'
> >>>
>
> Does a return in __init__() not do what I think it does?
>
> How else could/should I do this?

Change the return to a break
--
https://mail.python.org/mailman/listinfo/python-list
Re: Why do I always get an exception raised in this __init__()? [ In reply to ]
On 31/08/2023 22:15, Chris Green via Python-list wrote:

> class Gpiopin:
>
> def __init__(self, pin):
> #
> #
> # scan through the GPIO chips to find the line/pin we want
> #
> for c in ['gpiochip0', 'gpiochip1', 'gpiochip2', 'gpiochip3']:
>
> chip = gpiod.Chip(c)
> for l in range(32):
> line = chip.get_line(l)
> if pin in line.name():
> print("Found: ", line.name())
> return
> else:
> raise ValueError("Can't find pin '" + pin + "'")

You don't store the line anywhere.
You need to use self.line
self.line = chip.get_line(l)
if pin...

> def print_name(self):
> print (self.line.name())
>
> def set(self):
> self.line.set_value(1)
>
> def clear(self):
> self.line.set_value(0)

As you do here.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


--
https://mail.python.org/mailman/listinfo/python-list
Re: Why do I always get an exception raised in this __init__()? [ In reply to ]
Alan Gauld <learn2program@gmail.com> wrote:
> On 31/08/2023 22:15, Chris Green via Python-list wrote:
>
> > class Gpiopin:
> >
> > def __init__(self, pin):
> > #
> > #
> > # scan through the GPIO chips to find the line/pin we want
> > #
> > for c in ['gpiochip0', 'gpiochip1', 'gpiochip2', 'gpiochip3']:
> >
> > chip = gpiod.Chip(c)
> > for l in range(32):
> > line = chip.get_line(l)
> > if pin in line.name():
> > print("Found: ", line.name())
> > return
> > else:
> > raise ValueError("Can't find pin '" + pin + "'")
>
> You don't store the line anywhere.
> You need to use self.line
> self.line = chip.get_line(l)
> if pin...
>
> > def print_name(self):
> > print (self.line.name())
> >
> > def set(self):
> > self.line.set_value(1)
> >
> > def clear(self):
> > self.line.set_value(0)
>
> As you do here.
>
Yes, OK, absolutely. However that wasn't my original rather basic
problem which was, as I said, that I wasn't running the code I was
looking at.

The above was just a quick hack from some even cruder code doing the
same job, trying to develop it into something better and more general.

It's all on a headless Beaglebone Black (bit like a Raspberry Pi) so
I'm doing everything via multiple ssh connections and sometimes this
results in "the left hand not knowing what the right hand is doing"!

--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list