From 2440a0c70f002c7c9801876cb41d1e3c19c78b88 2013-02-03 19:06:49 From: Jan Schulz Date: 2013-02-03 19:06:49 Subject: [PATCH] time(it) magic: reorganize time formatting code Signed-off-by: Jan Schulz --- diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index 3cdacc2..a0af1e4 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -1076,22 +1076,6 @@ def parse_breakpoint(text, current_file): 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. - # See bug: https://bugs.launchpad.net/ipython/+bug/348466 - - # This is trying to prevent crashes, so it's more secure than it needs to - # E.g. eclipse is able to print a µ, but has no sys.stdout.encoding set. - - - units = [u"s", u"ms",u'us',"ns"] # the save value - if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: - try: - u'\xb5'.encode(sys.stdout.encoding) - units = [u"s", u"ms",u'\xb5s',"ns"] - except: - pass - scaling = [1, 1e3, 1e6, 1e9] if timespan >= 60.0: # we have more than a minute, format that in a human readable form @@ -1107,7 +1091,22 @@ def _format_time(timespan, precision=3): if leftover < 1: break return " ".join(time) + + # Unfortunately the unicode 'micro' symbol can cause problems in + # certain terminals. + # See bug: https://bugs.launchpad.net/ipython/+bug/348466 + # Try to prevent crashes by being more secure than it needs to + # E.g. eclipse is able to print a µ, but has no sys.stdout.encoding set. + units = [u"s", u"ms",u'us',"ns"] # the save value + if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: + try: + u'\xb5'.encode(sys.stdout.encoding) + units = [u"s", u"ms",u'\xb5s',"ns"] + except: + pass + scaling = [1, 1e3, 1e6, 1e9] + if timespan > 0.0: order = min(-int(math.floor(math.log10(timespan)) // 3), 3) else: