Re: Why do I always get an exception raised in this __init__()?
On Thu, Aug 31, 2023 at 3:19 PM Chris Green via Python-list 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 "", line 1, in > 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
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 "", line 1, in 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__()?
Chris Green wrote: [snip code and question] Sorry folks, it was a caching problem, I wasn't running the code I thought I was running! When I made sure I had cleared everything out and tried again it all worked as I expected. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What sort of exception when a class can't find something?
Several helpful replies, thank you all. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What sort of exception when a class can't find something?
On Fri, 1 Sept 2023 at 06:39, Chris Green via Python-list wrote: > > What sort of exception should a class raise in __init__() when it > can't find an appropriate set of data for the parameter passed in to > the class instantiation? > > E.g. I have a database with some names and address in and have a > class Person that gets all the details for a person given their > name. > > > > person.Person('Fred') > ... > ... > > > If Fred doesn't exist in the database what sort of exception should > there be? Is it maybe a ValueError? > There's no clear answer to this, because you aren't really constructing a Person here. So there are a few options that seem pretty reasonable: 1) As you say, raise ValueError. The problem is the value passed in (it's the right type, but the value wasn't found), so, ValueError. 2) KeyError. This emphasizes the fact that you're effectively looking up in a mapping. Quite odd for a constructor though. 3) A custom RecordNotFound exception. You're doing something unusual, so make it your own exception. TBH I would suggest making a slightly different API: person.Person.from_name('Fred') ie a classmethod alternate constructor. These can most definitely raise ValueError when the value given isn't appropriate: >>> datetime.datetime.fromordinal(-1) Traceback (most recent call last): File "", line 1, in ValueError: ordinal must be >= 1 and it makes good sense for a method like this to be doing lookups, rather than construction per se. (At a technical level, it's presumably constructing new objects.) To help with making that decision, what happens if you construct two Person objects for the same actual person? Would you return the same object (ie maintain a cache and deduplicate)? Or does each one take a snapshot of the data at the instant of construction, and thus you can observe changes through time by constructing more? Both are reasonable and make sense, but they lend themselves to slightly different approaches. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: What sort of exception when a class can't find something?
On 2023-08-31 21:32:04 +0100, Chris Green via Python-list wrote: > What sort of exception should a class raise in __init__() when it > can't find an appropriate set of data for the parameter passed in to > the class instantiation? > > E.g. I have a database with some names and address in and have a > class Person that gets all the details for a person given their > name. > > > > person.Person('Fred') > ... > ... > > > If Fred doesn't exist in the database what sort of exception should > there be? Is it maybe a ValueError? It you are going for a builtin exception, I think KeyError is the most appropriate: It should be a LookupError, since the lookup failed and a database is more like a mapping than a sequence. But it would probably be best to define your own exception for that. hp -- _ | Peter J. Holzer| Story must make more sense than reality. |_|_) || | | | h...@hjp.at |-- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" signature.asc Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
What sort of exception when a class can't find something?
What sort of exception should a class raise in __init__() when it can't find an appropriate set of data for the parameter passed in to the class instantiation? E.g. I have a database with some names and address in and have a class Person that gets all the details for a person given their name. person.Person('Fred') ... ... If Fred doesn't exist in the database what sort of exception should there be? Is it maybe a ValueError? -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list