diff --git a/humanfriendly.py b/humanfriendly.py index edb9758..06aab68 100644 --- a/humanfriendly.py +++ b/humanfriendly.py @@ -1,11 +1,11 @@ # Human friendly input/output in Python. # # Author: Peter Odding -# Last Change: November 15, 2014 +# Last Change: November 16, 2014 # URL: https://humanfriendly.readthedocs.org # Semi-standard module versioning. -__version__ = '1.11' +__version__ = '1.12' # Standard library modules. import math @@ -187,8 +187,7 @@ def format_timespan(num_seconds): """ if num_seconds < 60: # Fast path. - rounded_number = round_number(num_seconds, num_seconds < 10) - return pluralize(rounded_number, 'second') + return pluralize(round_number(num_seconds), 'second') else: # Slow path. result = [] @@ -423,6 +422,11 @@ def elapsed_time(self): elapsed_time += time.time() - self.start_time return elapsed_time + @property + def rounded(self): + """Human readable timespan rounded to seconds (a string).""" + return format_timespan(round(self.elapsed_time)) + def __str__(self): """ When a :py:class:`Timer` is coerced to a string it will show the diff --git a/humanfriendly_tests.py b/humanfriendly_tests.py index 3f65ccd..735abf1 100644 --- a/humanfriendly_tests.py +++ b/humanfriendly_tests.py @@ -3,7 +3,7 @@ # Tests for the 'humanfriendly' module. # # Author: Peter Odding -# Last Change: November 15, 2014 +# Last Change: November 16, 2014 # URL: https://humanfriendly.readthedocs.org # Standard library modules. @@ -43,9 +43,9 @@ def test_format_timespan(self): day = hour * 24 week = day * 7 year = week * 52 - self.assertEqual('0.00 seconds', humanfriendly.format_timespan(0)) + self.assertEqual('0 seconds', humanfriendly.format_timespan(0)) self.assertEqual('0.54 seconds', humanfriendly.format_timespan(0.54321)) - self.assertEqual('1.00 second', humanfriendly.format_timespan(1)) + self.assertEqual('1 second', humanfriendly.format_timespan(1)) self.assertEqual('3.14 seconds', humanfriendly.format_timespan(math.pi)) self.assertEqual('1 minute', humanfriendly.format_timespan(minute)) self.assertEqual('1 minute and 20 seconds', humanfriendly.format_timespan(80)) @@ -103,8 +103,8 @@ def test_concatenate(self): self.assertEqual(humanfriendly.concatenate(['one', 'two', 'three']), 'one, two and three') def test_timer(self): - for seconds, text in ((1, '1.00 second'), - (2, '2.00 seconds'), + for seconds, text in ((1, '1 second'), + (2, '2 seconds'), (60, '1 minute'), (60*2, '2 minutes'), (60*60, '1 hour'),