Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104195 > unrolled thread
| Started by | ZhangXiang <zhangyangyu0614@gmail.com> |
|---|---|
| First post | 2016-03-07 00:46 -0800 |
| Last post | 2016-03-07 17:26 +0000 |
| Articles | 8 — 6 participants |
Back to article view | Back to comp.lang.python
How to get which attribute causes the AttributeError except inspecting strings? ZhangXiang <zhangyangyu0614@gmail.com> - 2016-03-07 00:46 -0800
Re: How to get which attribute causes the AttributeError except inspecting strings? Peter Otten <__peter__@web.de> - 2016-03-07 10:48 +0100
Re: How to get which attribute causes the AttributeError except inspecting strings? Xiang Zhang <zhangyangyu0614@gmail.com> - 2016-03-07 02:15 -0800
Re: How to get which attribute causes the AttributeError except inspecting strings? jmp <jeanmichel@sequans.com> - 2016-03-07 11:50 +0100
Re: How to get which attribute causes the AttributeError except inspecting strings? Xiang Zhang <zhangyangyu0614@gmail.com> - 2016-03-07 08:25 -0800
Re: How to get which attribute causes the AttributeError except inspecting strings? Tim Golden <mail@timgolden.me.uk> - 2016-03-07 16:38 +0000
Re: How to get which attribute causes the AttributeError except inspecting strings? Xiang Zhang <zhangyangyu0614@gmail.com> - 2016-03-07 09:01 -0800
Re: How to get which attribute causes the AttributeError except inspecting strings? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-07 17:26 +0000
| From | ZhangXiang <zhangyangyu0614@gmail.com> |
|---|---|
| Date | 2016-03-07 00:46 -0800 |
| Subject | How to get which attribute causes the AttributeError except inspecting strings? |
| Message-ID | <be90a047-38ea-4108-adb8-17fd7483fee5@googlegroups.com> |
In python3, when I write code like this:
try:
fields = [getattr(Product, field) for field in fields.split(',')]
except AttributeError as e:
raise HTTPError(...)
I want to raise a new type of error giving a string telling the user which attribute is not valid. But I don't see any method I can use to get the attribute name except inspecting e.args[0].
Could anyone give me a hint? Maybe I miss something.
By the way, I don't quite want to change my code to a for-loop so I can access the field variable in exception handling.
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-03-07 10:48 +0100 |
| Message-ID | <mailman.12.1457344177.10335.python-list@python.org> |
| In reply to | #104195 |
ZhangXiang wrote:
> In python3, when I write code like this:
>
> try:
> fields = [getattr(Product, field) for field in fields.split(',')]
> except AttributeError as e:
> raise HTTPError(...)
>
> I want to raise a new type of error giving a string telling the user which
> attribute is not valid. But I don't see any method I can use to get the
> attribute name except inspecting e.args[0].
>
> Could anyone give me a hint? Maybe I miss something.
>
> By the way, I don't quite want to change my code to a for-loop so I can
> access the field variable in exception handling.
It seems that the name of the attribute is not made available. You have to
parse the error message or provide the name yourself:
>>> def mygetattr(object, name):
... try:
... return getattr(object, name)
... except AttributeError as err:
... err.name = name
... raise
...
>>> class Product:
... foo = 42
...
>>> fields = "foo,bar"
>>> try:
... fields = [mygetattr(Product, field) for field in fields.split(",")]
... except AttributeError as err:
... print(err)
... print("Missing attribute:", err.name)
...
type object 'Product' has no attribute 'bar'
Missing attribute: bar
Raising a subclass instead of mutating and reraising the original
AttributeError is probably cleaner...
[toc] | [prev] | [next] | [standalone]
| From | Xiang Zhang <zhangyangyu0614@gmail.com> |
|---|---|
| Date | 2016-03-07 02:15 -0800 |
| Message-ID | <56a79ef8-b083-4b71-a7a8-574490a11d0b@googlegroups.com> |
| In reply to | #104202 |
On Monday, March 7, 2016 at 5:49:48 PM UTC+8, Peter Otten wrote:
> ZhangXiang wrote:
>
> > In python3, when I write code like this:
> >
> > try:
> > fields = [getattr(Product, field) for field in fields.split(',')]
> > except AttributeError as e:
> > raise HTTPError(...)
> >
> > I want to raise a new type of error giving a string telling the user which
> > attribute is not valid. But I don't see any method I can use to get the
> > attribute name except inspecting e.args[0].
> >
> > Could anyone give me a hint? Maybe I miss something.
> >
> > By the way, I don't quite want to change my code to a for-loop so I can
> > access the field variable in exception handling.
>
> It seems that the name of the attribute is not made available. You have to
> parse the error message or provide the name yourself:
>
> >>> def mygetattr(object, name):
> ... try:
> ... return getattr(object, name)
> ... except AttributeError as err:
> ... err.name = name
> ... raise
> ...
> >>> class Product:
> ... foo = 42
> ...
> >>> fields = "foo,bar"
> >>> try:
> ... fields = [mygetattr(Product, field) for field in fields.split(",")]
> ... except AttributeError as err:
> ... print(err)
> ... print("Missing attribute:", err.name)
> ...
> type object 'Product' has no attribute 'bar'
> Missing attribute: bar
>
> Raising a subclass instead of mutating and reraising the original
> AttributeError is probably cleaner...
Yes. It is a way to achieving that. But is it reasonable to add an attribute to AttributeError so we can easily get the attribute name?
[toc] | [prev] | [next] | [standalone]
| From | jmp <jeanmichel@sequans.com> |
|---|---|
| Date | 2016-03-07 11:50 +0100 |
| Message-ID | <mailman.15.1457347821.10335.python-list@python.org> |
| In reply to | #104195 |
On 03/07/2016 09:46 AM, ZhangXiang wrote:
> In python3, when I write code like this:
>
> try:
> fields = [getattr(Product, field) for field in fields.split(',')]
> except AttributeError as e:
> raise HTTPError(...)
>
> I want to raise a new type of error giving a string telling the user which attribute is not valid. But I don't see any method I can use to get the attribute name except inspecting e.args[0].
>
> Could anyone give me a hint? Maybe I miss something.
>
> By the way, I don't quite want to change my code to a for-loop so I can access the field variable in exception handling.
>
Hi,
It is strange to morph an AttributeError in an HTTPError, anyway, you
could write
try:
fields = [getattr(Product, field) for field in fields.split(',')]
except AttributeError as e:
raise HTTPError(str(e))
Or if you really need to write some custom message, you could parse the
attribute error message for the attribute name.
# I've not tested this code !!
import re
try:
fields = [getattr(Product, field) for field in fields.split(',')]
except AttributeError as e:
# e.message should look like "class Foo has no attribute 'a'"
attr = re.search(r"'(\w+)'", e.message).group(1)
raise HTTPError('Custom message for the attribute %s' % attr)
Tbh I don't like it but it could do the job until someone raises you an
attribute error with a different string pattern.
jm
[toc] | [prev] | [next] | [standalone]
| From | Xiang Zhang <zhangyangyu0614@gmail.com> |
|---|---|
| Date | 2016-03-07 08:25 -0800 |
| Message-ID | <7915b82e-12f4-4652-999e-a1cfaca717b6@googlegroups.com> |
| In reply to | #104205 |
Hi, I know I can get the attribute name in some way, but since I just want the attribute name when an AttributeError caused by it raised, I really don't want to inspect the string or introduce one more layer over getattr. I hope I can get the attribute which causes the exception from the AttributeError raised directly. But it seems I can't.
[toc] | [prev] | [next] | [standalone]
| From | Tim Golden <mail@timgolden.me.uk> |
|---|---|
| Date | 2016-03-07 16:38 +0000 |
| Message-ID | <mailman.27.1457368693.10335.python-list@python.org> |
| In reply to | #104222 |
On 07/03/2016 16:25, Xiang Zhang wrote: > Hi, > > I know I can get the attribute name in some way, but since I just > want the attribute name when an AttributeError caused by it raised, I > really don't want to inspect the string or introduce one more layer > over getattr. I hope I can get the attribute which causes the > exception from the AttributeError raised directly. But it seems I > can't. > As things stand, you can't. But if you were to chime in on this issue: https://bugs.python.org/issue18156 then you might bring discussion there back to life again and see some forward movement. There seemed to be a consensus (and involving some active developers) so it's possible that new interest might revive interest. TJG
[toc] | [prev] | [next] | [standalone]
| From | Xiang Zhang <zhangyangyu0614@gmail.com> |
|---|---|
| Date | 2016-03-07 09:01 -0800 |
| Message-ID | <1db91516-ecae-4e73-adb5-78d6b728e7d8@googlegroups.com> |
| In reply to | #104225 |
On Tuesday, March 8, 2016 at 12:38:31 AM UTC+8, Tim Golden wrote: > On 07/03/2016 16:25, Xiang Zhang wrote: > > Hi, > > > > I know I can get the attribute name in some way, but since I just > > want the attribute name when an AttributeError caused by it raised, I > > really don't want to inspect the string or introduce one more layer > > over getattr. I hope I can get the attribute which causes the > > exception from the AttributeError raised directly. But it seems I > > can't. > > > > As things stand, you can't. But if you were to chime in on this issue: > > https://bugs.python.org/issue18156 > > then you might bring discussion there back to life again and see some > forward movement. There seemed to be a consensus (and involving some > active developers) so it's possible that new interest might revive interest. > > TJG Thanks for mentioning the issue. I'll ping it to see if there is still anyone interested in it.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2016-03-07 17:26 +0000 |
| Message-ID | <mailman.33.1457371807.10335.python-list@python.org> |
| In reply to | #104222 |
On 07/03/2016 16:38, Tim Golden wrote: > On 07/03/2016 16:25, Xiang Zhang wrote: >> Hi, >> >> I know I can get the attribute name in some way, but since I just >> want the attribute name when an AttributeError caused by it raised, I >> really don't want to inspect the string or introduce one more layer >> over getattr. I hope I can get the attribute which causes the >> exception from the AttributeError raised directly. But it seems I >> can't. >> > > As things stand, you can't. But if you were to chime in on this issue: > > https://bugs.python.org/issue18156 > > then you might bring discussion there back to life again and see some > forward movement. There seemed to be a consensus (and involving some > active developers) so it's possible that new interest might revive interest. > > TJG > Thanks, you saved me searching for the link that I knew existed :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web