From 49ff9951308a6d073e32f518fa8e6581917de9ee Mon Sep 17 00:00:00 2001 From: RussellBallestrini Date: Wed, 22 May 2013 23:17:53 -0400 Subject: [PATCH] Removed need to keep track of counter, which reduces complexity and chance of introducing errors, by slicing the the units tuple by the percision. Removed __main__ because nosetest --with-coverage was flagging it. It was un-nessasary. Special thanks to Eric Rasmussen who inspired this commit. --- ago.py | 8 +------- test_ago.py | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/ago.py b/ago.py index f39d579..de4dbb1 100644 --- a/ago.py +++ b/ago.py @@ -25,17 +25,11 @@ def human(dt, precision=2, past_tense='{} ago', future_tense='in {}'): d = delta2dict( delta ) hlist = [] - count = 0 units = ( 'year', 'day', 'hour', 'minute', 'second', 'microsecond' ) - for unit in units: - if count >= precision: break # met precision + for unit in units[:precision]: # strip units to precision if d[ unit ] == 0: continue # skip 0's s = '' if d[ unit ] == 1 else 's' # handle plurals hlist.append( '%s %s%s' % ( d[unit], unit, s ) ) - count += 1 human_delta = ', '.join( hlist ) return the_tense.format(human_delta) -if __name__ == "__main__": - from test_ago import test_output - test_output() diff --git a/test_ago.py b/test_ago.py index 79dcc74..29ea8b2 100644 --- a/test_ago.py +++ b/test_ago.py @@ -70,7 +70,7 @@ def test_valid_future_dict(): assert past_dict['minute'] == 27 assert past_dict['microsecond'] == 963 -def test_output(): +def example_usage(): """Test and example usage""" print '\nTest past tense:\n'