[Python-ideas] Replace option set/get methods through the standard library with a ChainMap; add a context manager to ChainMap
Neil Girdhar
mistersheik at gmail.com
Sat Sep 14 22:10:00 CEST 2013
ChainMap supports the pattern of a "dictionary that supports temporarily
overriding items". The method I'm suggesting is as follows:
class ChainMap:
@contextmanager
def child_contex(self, **kwargs):
self.add_child(**kwargs)
try:
yield
finally:
self = self.parents
Then, when updating numpy.printoptions:
with numpy.printoptions.child_context(precision=23):
... # do something
With a regular dict, numpy would end up implementing the necessary context
manager once for each set of options instead of factoring that code out
into ChainMap.
On Thu, Sep 12, 2013 at 6:24 AM, Oscar Benjamin
<oscar.j.benjamin at gmail.com>wrote:
> On 12 September 2013 11:14, Neil Girdhar <mistersheik at gmail.com> wrote:
> >
> > Thank you. I will ask there about adding numpy context managers.
> However,
> > the extra member function to ChainMap to use it as a context manager
> would
> > be a question for this mailing list, right?
>
> Perhaps you could spell out that part of the idea in more detail then.
> Why in particular would it need to be a ChainMap and not a regular
> dict? Does the method return a new ChainMap instance? What would be
> seen by other code that holds references to the same ChainMap?
>
>
> Oscar
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130914/4bf47e7b/attachment.html>
More information about the Python-ideas
mailing list