From 5750e2dd08aa43fdea03e80168e15d40d3b8d772 2012-04-16 01:11:24 From: Fernando Perez Date: 2012-04-16 01:11:24 Subject: [PATCH] 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. --- diff --git a/IPython/parallel/client/asyncresult.py b/IPython/parallel/client/asyncresult.py index a55bd2f..5931b6f 100644 --- a/IPython/parallel/client/asyncresult.py +++ b/IPython/parallel/client/asyncresult.py @@ -25,6 +25,18 @@ from IPython.core.display import clear_output 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 @@ class AsyncResult(object): # 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 @@ class AsyncResult(object): 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 @@ class AsyncResult(object): """ t = 0 for md in self._metadata: - t += (md['completed'] - md['started']).total_seconds() + t += _total_seconds(md['completed'] - md['started']) return t @property