Only printing error messages once per loop
Question:
I have some Python that looks like this:
for row in reader:
if # something
# do things
else:
try:
# do more things
except IndexError:
logger.info('message')
What ends up happening is I get a huge wall of output that says INFO:...message
for each iteration of the loop. There’s no need for it to appear what could be over 1,000 times. I could use a ‘flag’ of sorts after I leave the loop, kind of like this:
for row in reader
# if/else/try
except IndexError:
foo = True
if foo:
logger.info('message')
But I’m wondering if there’s a more elegant way to do this. The important bits are that I do want to show the error, but only once. And I can’t break out of the loop on the error, because I need to continue processing the rest of the row
s in reader
. The IndexError
appears when I try to create a variable from a list that might not exist. I have to do it this way because if the variable does not exist I have to skip over it instead of providing even a blank value.
Thus my weird little predicament. Is there a better way of doing this? Preferably the most ‘Pythonic’, as speed and what not isn’t such a big issue in this case.
Answers:
Just keep a counter, sometimes the simplest ways are the best. you could even add a nifty notice at the end.
bad = 0
for row in reader:
if # something
# do things
else:
try:
# do more things
except IndexError:
bad += 1
if bad == 1:
logger.info('message')
if bad:
logger.info('%d bad things happened' % bad)
sys.exc_info() and sys.exc_clear() may help to store and clean the errors here. You can refer to How sys.exc_info() works? for more mechanism of these two functions.
I have some Python that looks like this:
for row in reader:
if # something
# do things
else:
try:
# do more things
except IndexError:
logger.info('message')
What ends up happening is I get a huge wall of output that says INFO:...message
for each iteration of the loop. There’s no need for it to appear what could be over 1,000 times. I could use a ‘flag’ of sorts after I leave the loop, kind of like this:
for row in reader
# if/else/try
except IndexError:
foo = True
if foo:
logger.info('message')
But I’m wondering if there’s a more elegant way to do this. The important bits are that I do want to show the error, but only once. And I can’t break out of the loop on the error, because I need to continue processing the rest of the row
s in reader
. The IndexError
appears when I try to create a variable from a list that might not exist. I have to do it this way because if the variable does not exist I have to skip over it instead of providing even a blank value.
Thus my weird little predicament. Is there a better way of doing this? Preferably the most ‘Pythonic’, as speed and what not isn’t such a big issue in this case.
Just keep a counter, sometimes the simplest ways are the best. you could even add a nifty notice at the end.
bad = 0
for row in reader:
if # something
# do things
else:
try:
# do more things
except IndexError:
bad += 1
if bad == 1:
logger.info('message')
if bad:
logger.info('%d bad things happened' % bad)
sys.exc_info() and sys.exc_clear() may help to store and clean the errors here. You can refer to How sys.exc_info() works? for more mechanism of these two functions.