idiomatic way to collect and report multiple exceptions?

B

Ben Cohen

Is there a pythonic way to collect and display multiple exceptions at the same time?

For example let's say you're trying to validate the elements of a list and you'd like to validate as many of the elements as possible in one run and still report exception's raised while validating a failed element.

eg -- I'd like to do something like this:

errors = []
for item in data:
try:
process(item)
except ValidationError as e:
errors.append(e)
raise MultipleValidationErrors(*errors)

where if the raised MultipleValidationErrors exception goes uncaught the interpreter will print a nice traceback that includes the tracebacks of each raised ValidationError. But I don't know how MultipleValidationErrors should be written ... I'm targeting python 2.6 at the moment -- googling around for advice I see some pep's relating to exception handling changes in python3 that might be relevant but I've ended up more confused.

Thanks in advance,

Ben
 
A

Aahz

eg -- I'd like to do something like this:

errors = []
for item in data:
try:
process(item)
except ValidationError as e:
errors.append(e)
raise MultipleValidationErrors(*errors)

First of all, please DO NOT post code with TABs in it. In fact, you
shouldn't even be writing code with TABs -- TAB characters are obsolete
for new Python code.

I would write your code this way:

errors = []
for item in data:
try:
process(item)
except ValidationError as e:
errors.append(str(e))
if errors:
raise MultipleValidationErrors(errors)
 
S

Steven D'Aprano

Ben said:
eg -- I'd like to do something like this:

errors = []
for item in data:
try:
process(item)
except ValidationError as e:
errors.append(e)
raise MultipleValidationErrors(*errors)

First of all, please DO NOT post code with TABs in it. In fact, you
shouldn't even be writing code with TABs -- TAB characters are obsolete
for new Python code.

I don't believe this is correct. Do you have a source for this?

Four spaces are recommended by PEP 8, and are compulsory for patches to
the standard library, but Python continues to accept either multi-space
indents or tabs, and in your own personal code you can use whichever you
like.

The documentation for Python 3 continues to state that either spaces or
tabs will be accepted (and warns against mixing them):

http://docs.python.org/py3k/reference/lexical_analysis.html#indentation



According to my tests, Python 3.1 continues to accept both spaces and
tabs. Tabs on their own are accepted without warning:


[steve@sylar ~]$ cat tabs.py
def f(x):
# This function uses tabs for indents.
return x


print(f("tabs"))

[steve@sylar ~]$ python3.1 tabs.py
tabs


Even mixing tabs and spaces in the one indent is allowed:

[steve@sylar ~]$ cat mixed.py
def f(x):
# This uses mixed tabs and spaces.
return x


print(f("mixed tabs and spaces"))

[steve@sylar ~]$ python3.1 mixed.py
mixed tabs and spaces


But using both tab-based indents and space-based indents is not:

[steve@sylar ~]$ cat both.py
def f(x):
# This uses both tabs and spaces.
pass # space-indent
return x # tab-indent


print(f("both tabs and spaces"))

[steve@sylar ~]$ python3.1 both.py
File "both.py", line 4
return x # tab-indent
^
TabError: inconsistent use of tabs and spaces in indentation
 
B

Ben Cohen

Apologies for the TABs -- I wrote that example for demonstration purposes in my mail client -- I'll copy and paste from a real code editor in the future.
Ben

eg -- I'd like to do something like this:

errors = []
for item in data:
try:
process(item)
except ValidationError as e:
errors.append(e)
raise MultipleValidationErrors(*errors)

First of all, please DO NOT post code with TABs in it. In fact, you
shouldn't even be writing code with TABs -- TAB characters are obsolete
for new Python code.

I would write your code this way:

errors = []
for item in data:
try:
process(item)
except ValidationError as e:
errors.append(str(e))
if errors:
raise MultipleValidationErrors(errors)
 
J

Jean-Michel Pichavant

Ben said:
Apologies for the TABs -- I wrote that example for demonstration purposes in my mail client -- I'll copy and paste from a real code editor in the future.
Ben

There's nothing to apologies for. Be wary of those trying to get you out
of the right path, they will lie to you stating python does not support
Tabs while it surely does. Did you know that before generating bytecode,
python replace all 4 spaces by a tabulations ? If that is not a proof
that TABS is the all mighty indentation, I'm Mickey Mouse.

Feel free to join our group for promoting TABS (monthly fee 50$). You an
also purchase the book 'TABS Are Beautiful' (12 pages, 380$).

JM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top