-
-
Notifications
You must be signed in to change notification settings - Fork 11.9k
ENH: add np.printoptions, a context manager
#10406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Are printoptions threadlocal or global? Should they be threadlocal?
|
Just a syntax sugar over the get_printoptions/set_printoptions pair.
|
Would this work if two threads used the context manager at the same time? |
|
@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 |
|
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. |
numpy/core/arrayprint.py
Outdated
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def printoptions(*args, **kwds): |
There was a problem hiding this comment.
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
numpy/core/arrayprint.py
Outdated
| -------- | ||
| `printoptions` sets print options temporarily, and restores them back | ||
| at the end of the `with`-block: |
There was a problem hiding this comment.
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
withblock, restoring the old options at the end
|
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. |
|
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. |
eric-wieser
left a comment
There was a problem hiding this 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
doc/release/1.15.0-notes.rst
Outdated
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct.
There was a problem hiding this comment.
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.
|
Thanks Evgeni. |
Just a syntax sugar over the
get_printoptions/set_printoptionspair: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