I'm doing some pyplotting of generic data and converting it from a power value to a dB value. Due to the system these values come from, 0 is used as a 'the useful data ends here' indicator (the nature of the mathematics, not a defined value).
My usual way of dealing with these is wrapping the conversion in a try/except and returning a default 'low' value, eg
def f(value):
try:
return convert(value)
except ValueError:
return -140 #implementation specific, don't worry
This is fine and dandy for 90% of use within my framework, except when it comes to graphing.
I'm lazy so what I do at the minute is:
pl.plot(xvals,map(f,yvals))
This draws the data correctly, and when the data ends, plunges off a cliff, which is the expected behaviour. But what I would like to happen is for the graph to just end when it meets a ValueError exception and do away with f() altogether.
Other than breaking the map up into a loop, anyone got any brilliant ideas?
UPDATES:
I'm using Pylab / MatplotLib "Endpoint" is execution dependent; sometimes the above doesn't matter atall as there are no 'bad' values. This is all in an effort for me to be lazy and use matplotlibs graph scaling instead of resetting dynamic ylim's based on the min of the ydata (which I don't do atm, just ylim(-140) in this case.)
Vaguely Important Update On Answer: unutbu's answer is what I'll actually be using for my implementation, due to (not mentioned in the question dependencies), as raising a StopIteration in this regularly used function wreaks havoc with unrelated-to-the-question control logic, without putting all of those other instances in try-excepts; sometimes -inf makes more sense than you would think.
Thanks to everyone for being awesomely fast, and I apologise to unutbu for the QuestionFail.