From: Tim H. <tim...@ie...> - 2006-10-11 22:31:35
|
Travis Oliphant wrote: > Sven Schreiber wrote: > > >>> This is user adjustable. You change the error mode to raise on >>> 'invalid' instead of pass silently which is now the default. >>> >>> -Travis >>> >>> >>> >>> >> Could you please explain how this adjustment is done, or point to the >> relevant documentation. >> >> >> > > numpy.sqrt(-1) > > old = seterr(invalid='raise') > numpy.sqrt(-1) # should raise an error > > seterr(**old) # restores error-modes for current thread > numpy.sqrt(-1) > > With python 2.5 out now, perhaps it's time to come up with a with statement context manager. Something like: from __future__ import with_statement import numpy class errstate(object): def __init__(self, **kwargs): self.kwargs = kwargs def __enter__(self): self.oldstate = numpy.seterr(**self.kwargs) def __exit__(self, *exc_info): numpy.seterr(**self.oldstate) a = numpy.arange(10) a/a # ignores divide by zero with errstate(divide='raise'): a/a # raise exception on divide by zer # Would ignore divide by zero again if we got here. -tim |