Skip to content

Conversation

@ev-br
Copy link
Contributor

@ev-br ev-br commented Jan 15, 2018

Just a syntax sugar over the get_printoptions/set_printoptions pair:

>>> import numpy as np
>>> with np.printoptions(precision=2):
...     print(np.array([2.0]) / 3)
... 
[0.67]
>>> print(np.array([2.0]) / 3)
[0.66666667]

Something like this is probably present in everyone's personal toolbox. One can argue this is where this belongs: personal toolboxes and SO answers (there's at least one, likely more).

The argument for having it in numpy is mostly convenience and ease of introducing to newbees: I can see this used fairly early in teaching, especially non-CS students; the implementation is a bit more magical even if is a mere half dozen lines.

EDIT: closes gh-8344

@pv
Copy link
Member

pv commented Jan 15, 2018 via email

Just a syntax sugar over the get_printoptions/set_printoptions pair.
@eric-wieser
Copy link
Member

eric-wieser commented Jan 15, 2018

This is a duplicate of #3987, but the original is a little overreaching.

@pv: global, unfortunately

@ghost
Copy link

ghost commented Jan 15, 2018

Would this work if two threads used the context manager at the same time?

@eric-wieser
Copy link
Member

@xoviat: no, but nor does manually calling the get and set like users might currently do. Worth warning against in the docstrings though.

Also whatever happens, we fall afoul of PEP550 not being implemented, when it comes to coroutines

@mhvk
Copy link
Contributor

mhvk commented Jan 15, 2018

Since I don't think it is possible to set print options locally now, it seems we cannot blame the context manager to be global in scope too. I think this is good to have.



@contextlib.contextmanager
def printoptions(*args, **kwds):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: **kwargs is a more common spelling

--------
`printoptions` sets print options temporarily, and restores them back
at the end of the `with`-block:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this belongs in the main doc section above this heading. Maybe the one-line summary should be

A context manager that sets print options for the scope of the with block, restoring the old options at the end

@charris
Copy link
Member

charris commented Jan 16, 2018

i wonder if we should move all the context managers into their own file at some point? Having them scattered about make it a bit trickier to find them.

@eric-wieser eric-wieser added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Jan 16, 2018
@ev-br
Copy link
Contributor Author

ev-br commented Jan 16, 2018

Ugh, missed #3987 completely. I pushed a version which hopefully addresses review comments, the decision is with you guys :-).

Making array printing options thread-local is separate, and I'd prefer to defer it to a separate PR.

Copy link
Member

@eric-wieser eric-wieser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RST formatting might be a little off, but I don't actually know it well enough to be sure. We can always fix it when the release-notes get cleaned up for release

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think backticks are markdown-only. Two colons after the word "block" might do the right thing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, fixed and force-pushed.

@charris charris merged commit 9749eec into numpy:master Jan 20, 2018
@charris
Copy link
Member

charris commented Jan 20, 2018

Thanks Evgeni.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The method to undo numpy set_printoptions

5 participants