From fedaaf7f6c6e616edd51ebed6eb049e6babe9d94 Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Thu, 8 Dec 2011 16:21:03 +0100 Subject: Add Stopwatch.start_stop_seconds(), don't use timedelta.total_seconds(). This function is only available in Python >= 2.7. On http://docs.python.org/library/datetime.html#timedelta-objects it says total_seconds() is equal to (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 with true division enabled. We are using td.days * 24 * 3600 + td.seconds + operator.truediv(td.microseconds, 10**6) now in Stopwatch.start_stop_seconds(), which I find more readable. --- morphlib/builder.py | 3 +-- morphlib/stopwatch.py | 8 ++++++++ morphlib/stopwatch_tests.py | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) (limited to 'morphlib') diff --git a/morphlib/builder.py b/morphlib/builder.py index 416216d3..31fc617f 100644 --- a/morphlib/builder.py +++ b/morphlib/builder.py @@ -93,11 +93,10 @@ class BinaryBlob(object): 'build-times': {} } for stage in self.build_watch.ticks.iterkeys(): - delta = self.build_watch.start_stop_delta(stage) meta['build-times'][stage] = { 'start': '%s' % self.build_watch.start_time(stage), 'stop': '%s' % self.build_watch.stop_time(stage), - 'delta': delta.total_seconds() + 'delta': self.build_watch.start_stop_seconds(stage) } self.write_cache_metadata(meta) diff --git a/morphlib/stopwatch.py b/morphlib/stopwatch.py index bcdc9291..492738d5 100644 --- a/morphlib/stopwatch.py +++ b/morphlib/stopwatch.py @@ -14,6 +14,8 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import operator + from datetime import datetime @@ -48,3 +50,9 @@ class Stopwatch(object): def start_stop_delta(self, reference_object): return (self.stop_time(reference_object) - self.start_time(reference_object)) + + def start_stop_seconds(self, reference_object): + delta = self.start_stop_delta(reference_object) + return (delta.days * 24 * 3600 + + delta.seconds + + operator.truediv(delta.microseconds, 10**6)) diff --git a/morphlib/stopwatch_tests.py b/morphlib/stopwatch_tests.py index 7441eac3..1a899f41 100644 --- a/morphlib/stopwatch_tests.py +++ b/morphlib/stopwatch_tests.py @@ -53,6 +53,6 @@ class StopwatchTests(unittest.TestCase): our_delta = stop - start watch_delta = self.stopwatch.start_stop_delta('start-stop') - - assert our_delta.total_seconds() > 0 self.assertEqual(our_delta, watch_delta) + + assert self.stopwatch.start_stop_seconds('start-stop') > 0 -- cgit v1.2.1