[prev in list] [next in list] [prev in thread] [next in thread]
List: python-ideas
Subject: Re: [Python-ideas] Replace numpy get_printoptions/set_printoptions, and similar patterns with a Chai
From: Neil Girdhar <mistersheik () gmail ! com>
Date: 2013-09-11 22:07:45
Message-ID: ab9df868-00a6-495b-9296-5a0d4c6c8125 () googlegroups ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
Just to be clear, my proposal is to replace all such get/set options
patterns throughout Python's standard library.
On Wednesday, September 11, 2013 5:13:55 PM UTC-4, Neil Girdhar wrote:
>
> With numpy, the usual pattern is to get_printoptions, set some of them,
> and then restore the old options. Why not expose the options in a ChainMap
> as numpy.printoptions? ChainMap could then expose a context manager that
> pushes a new dictionary on entry and pops it on exit via, say,
> child_context that accepts a dictionary. Now, instead of:
>
> saved_precision = np.get_printoptions()['precision']
> np.set_printoptions(precision=23)
> do_something()
> np.set_printoptions(precision=saved_precision)
>
> You can do the same with a context manager, which I think is stylistically
> better (as it's impossible to forget to reset the option, and no explicit
> temporary invades the local variables):
>
> with np.printoptions.child_context({'precision', 23}):
> do_something()
>
> Best,
>
> Neil
>
[Attachment #5 (text/html)]
<div dir="ltr">Just to be clear, my proposal is to replace all such get/set options \
patterns throughout Python's standard library.<br><br>On Wednesday, September 11, \
2013 5:13:55 PM UTC-4, Neil Girdhar wrote:<blockquote class="gmail_quote" \
style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: \
1ex;"><div dir="ltr">With numpy, the usual pattern is to get_printoptions, set some \
of them, and then restore the old options. Why not expose the options in a \
ChainMap as numpy.printoptions? ChainMap could then expose a context manager \
that pushes a new dictionary on entry and pops it on exit via, say, child_context \
that accepts a dictionary. Now, instead of:<div><br></div><div>saved_precision \
= np.get_printoptions()['<wbr>precision']</div><div>np.set_printoptions(precision=<wbr \
>23)</div><div>do_something()</div><div>np.set_printoptions(precision=<wbr>saved_precision)</div><div><br></div><div>You \
> can do the same with a context manager, which I think is stylistically better (as \
> it's impossible to forget to reset the option, and no explicit temporary invades \
> the local variables):</div><div><br></div><div>with \
> np.printoptions.child_context(<wbr>{'precision', 23}):</div><div> \
> do_something()</div><div><br></div><div>Best,</div><div><br></div><div>Neil</div></div></blockquote></div>
>
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic