From: Travis O. <oli...@ee...> - 2006-10-11 23:47:04
|
Tim Hochberg wrote: >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 > > > This looks great. I think most people aren't aware of the with statement and what it can do (I'm only aware because of your posts, for example). So, what needs to be added to your example in order to just add it to numpy? -Travis |