-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
time(it) magic: Implement minutes/hour formatting and "%%time" cell magic #2855
Conversation
Just curious: under what circumstances would you use |
I have a network analysis where I put several steps in one cell and then run the whole cell with An example cell (of abut 10 in that notebook, some now commented out): %%timeit -r1 -n1
eigen = g_giant.eigenvector_centrality()
g_giant.vs["eigen"] = eigen |
So would it be easier to just have a |
If that would do the same as Oups, now I see why @minrk was asking (one '%'...): yep, for line magics, the |
And yes, %%time should absolutely be registered as a cell magic, I've typed it a million times - that should be a one-liner, if you want to add that here. |
The only advantage left would be the difference in how %time and %timeit report the time (%timeit shows loops, which are irrelevant for one run, but %time doesn't reformat the times if they are significantly less than one second). |
Ok, so
I can do the first two but the last is something I haven't yet an idea how to do it... lets see... |
* Refactor time formatting of timeit into a new function * Add minutes and hours for long running cells/lines * Convert time and timeit magic to use the new function Minutes and hours are float formatted, so 1.5 h is "1 hour, 30 minutes". Signed-off-by: Jan Schulz <jasc@gmx.net>
Convert time to be usable as cell level magic. Also fix some examples, which didn't have a '%' in front of the magic. Signed-off-by: Jan Schulz <jasc@gmx.net>
Hm, that was easier than I thought... |
# auto-detecting if the terminal can deal with it, use plain 'us' for | ||
# microseconds. I am really NOT happy about disabling the proper | ||
# 'micro' prefix, but crashing is worse... If anyone knows what the | ||
# right solution for this is, I'm all ears... |
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.
While we're working on this, it would be nice to get the proper µ
prefix again - this comment is ancient. I think that checking u'µ'.encode(sys.stdin.encoding)
should be OK.
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.
It works in a windows command prompt (open cmd, -> ipython)... But in ipythonqt, sys.stdin.encoding
is "NoneType"...
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.
What about sys.stdout.encoding
- that's UTF-8
in the terminal and the Qt console for me. Plus it makes more sense to check it for output. There's some reason we look at stdin elsewhere, but I forget what it is.
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 just read https://bugs.launchpad.net/ipython/+bug/348466 And I'm not sure what happens here: if some user has the terminal encoding set to ascii, it will result in an unicode error.
For example eclipse set the default stdout encoding in the embedded console to None
but print works...:
>>> import sys
>>> print(sys.stdout.encoding)
None
>>> print(u'µ'.encode(sys.stdout.encoding))
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: encode() argument 1 must be string, not None
>>> print(u'µ')
µ
I think the only "working" solution would be to try to print a "µ" and if hat fails use "u" instead, but I've no idea how to do that without actually printing a char to the console...
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.
False negatives don't matter much - users in Eclipse will still see 'u'. We
want to avoid false positives, i.e. trying to display µ when we can't do
so. I think using sys.stdout.encoding is an improvement for all the users
where it is set. But if you're not sure, leave it, and we'll do it in a
separate pull request.
On 3 February 2013 12:59, JanSchulz notifications@github.com wrote:
In IPython/core/magics/execution.py:
@@ -1091,3 +1071,43 @@ def parse_breakpoint(text, current_file):
return current_file, int(text)
else:
return text[:colon], int(text[colon+1:])
+
+def _format_time(timespan, precision=3):
- """Formats the timespan in a human readable form"""
- import math
XXX: Unfortunately the unicode 'micro' symbol can cause problems in
certain terminals. Until we figure out a robust way of
auto-detecting if the terminal can deal with it, use plain 'us' for
microseconds. I am really NOT happy about disabling the proper
'micro' prefix, but crashing is worse... If anyone knows what the
right solution for this is, I'm all ears...
I just read https://bugs.launchpad.net/ipython/+bug/348466 And I'm not
sure what happens here: if some user has the terminal encoding set to
ascii, it will result in an unicode error.For example eclipse set the default stdout encoding in the embedded
console to None but print works...:import sys>>> print(sys.stdout.encoding)None>>> print(u'µ'.encode(sys.stdout.encoding))Traceback (most recent call last):
File "", line 1, in TypeError: encode() argument 1 must be string, not None>>> print(u'µ')µI think the only "working" solution would be to try to print a "µ" and if
hat fails use "u" instead, but I've no idea how to do that without actually
printing a char to the console...—
Reply to this email directly or view it on GitHubhttps://github.com//pull/2855/files#r2870793.
Another implementation, which does hour:sec formatting: http://snipplr.com/view/5713/ |
The unicode version is guarded so that it won't crash in terminals which do not support that sign. Signed-off-by: Jan Schulz <jasc@gmx.net>
I've added a commit to print real "µs" instead of "us". Please test as I'm not sure if that won't break on funny terminals... |
Signed-off-by: Jan Schulz <jasc@gmx.net>
speaking of funny terminals, Travis apparently uses an ascii locale. |
So one more case is when stdout is overridden with a StringIO, in which case it may have no encoding attribute. |
time is now reported as '0 ns' and not '0.00 s' Signed-off-by: Jan Schulz <jasc@gmx.net>
Signed-off-by: Jan Schulz <jasc@gmx.net>
Signed-off-by: Jan Schulz <jasc@gmx.net>
Looks good to me now. |
This works in my tests, so I'll merge it. Thanks! |
time(it) magic: Implement minutes/hour formatting and "%%time" cell magic
time(it) magic: Implement minutes/hour formatting and "%%time" cell magic
%%time cell magic and better formatting for longer times (minutes/hours)in both timeit and time magic.
Currently minutes and hours use simple float formatting, not "hours:minutes:seconds" (1.5 min is 1 minute 30 secs).
fixes #2751 (partly)