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.
This commit is contained in:
RussellBallestrini 2013-05-22 23:17:53 -04:00
parent a2574a05d8
commit 49ff995130
2 changed files with 2 additions and 8 deletions

8
ago.py
View file

@ -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()

View file

@ -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'