Permalink
Browse files

Merge pull request #1608 from minrk/ar_sugar_2.6

don't rely on timedelta.total_seconds in AsyncResult (timedelta is new in python 2.7).  

This maintains compatibility with Python 2.6; the utility function introduced can be removed if/when we drop 2.6 support.
  • Loading branch information...
2 parents 9b9fb9e + 63ca28c commit 5750e2dd08aa43fdea03e80168e15d40d3b8d772 @fperez fperez committed Apr 16, 2012
Showing with 15 additions and 3 deletions.
  1. +15 −3 IPython/parallel/client/asyncresult.py
@@ -25,6 +25,18 @@
from IPython.external.decorator import decorator
from IPython.parallel import error
+#-----------------------------------------------------------------------------
+# Functions
+#-----------------------------------------------------------------------------
+
+def _total_seconds(td):
+ """timedelta.total_seconds was added in 2.7"""
+ try:
+ # Python >= 2.7
+ return td.total_seconds()
+ except AttributeError:
+ # Python 2.6
+ return 1e-6 * (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6)
#-----------------------------------------------------------------------------
# Classes
@@ -300,7 +312,7 @@ def timedelta(self, start, end, start_key=min, end_key=max):
# handle single_result AsyncResults, where ar.stamp is single object,
# not a list
end = end_key(end)
- return (end - start).total_seconds()
+ return _total_seconds(end - start)
@property
def progress(self):
@@ -323,7 +335,7 @@ def elapsed(self):
stamp = self._client.metadata[msg_id]['submitted']
if stamp and stamp < submitted:
submitted = stamp
- return (now-submitted).total_seconds()
+ return _total_seconds(now-submitted)
@property
@check_ready
@@ -334,7 +346,7 @@ def serial_time(self):
"""
t = 0
for md in self._metadata:
- t += (md['completed'] - md['started']).total_seconds()
+ t += _total_seconds(md['completed'] - md['started'])
return t
@property

0 comments on commit 5750e2d

Please sign in to comment.