* adjusted past_format and future_format to past_tense and future_tense

* corrected all tests and documentation to reflect new keyword argument names
* changed example _tense strings to use {} instead of {0}
* cleaned up docstrings
This commit is contained in:
RussellBallestrini 2013-01-12 14:17:49 -05:00
parent 3cbd5101b3
commit fb0ffca26e
3 changed files with 27 additions and 27 deletions

View file

@ -40,18 +40,18 @@ You really only need to worry about *human*.
Here are all the available arguments and defaults:: Here are all the available arguments and defaults::
human(d, precision=2, past_format='{0} ago', future_format='in {0}'): human(date_time, precision=2, past_tense='{} ago', future_tense='in {}'):
d date_time
datetime object to make into human readable, required datetime object to make into human readable, required
precision precision
control how verbose the output should look, optional control how verbose the output should look, optional
past_format past_tense
format string used when d is a past datetime, optional format string used when d is a past datetime, optional
future_format future_tense
format string used when d is a future datetime, optional format string used when d is a future datetime, optional
@ -99,16 +99,16 @@ Now we will document how to use delta2human and delta2dict::
# create a dictionary out of the timedelta # create a dictionary out of the timedelta
print delta2dict( delta ) print delta2dict( delta )
Example past_format and future_format keyword arguments:: Example past_tense and future_tense keyword arguments::
output1 = human( PAST, output1 = human( PAST,
past_format = 'titanic sunk {0} ago', past_tense = 'titanic sunk {0} ago',
future_format = 'titanic will sink in {0} from now' future_tense = 'titanic will sink in {0} from now'
) )
output2 = human( FUTURE, output2 = human( FUTURE,
past_format = 'titanic sunk {0} ago', past_tense = 'titanic sunk {0} ago',
future_format = 'titanic will sink in {0} from now' future_tense = 'titanic will sink in {0} from now'
) )
print output1 print output1

20
ago.py
View file

@ -2,7 +2,7 @@ from datetime import datetime
from datetime import timedelta from datetime import timedelta
def delta2dict( delta ): def delta2dict( delta ):
"""return dictionary of delta""" """Accepts a delta, returns a dictionary of units"""
delta = abs( delta ) delta = abs( delta )
return { return {
'year' : delta.days / 365 , 'year' : delta.days / 365 ,
@ -13,10 +13,10 @@ def delta2dict( delta ):
'microsecond' : delta.microseconds 'microsecond' : delta.microseconds
} }
def delta2human(delta, precision=2, past_format='{0} ago', future_format='in {0}'): def delta2human(delta, precision=2, past_tense='{} ago', future_tense='in {}'):
"""return human readable delta string""" """Accepts a delta, returns a human readable delta string"""
the_format = past_format the_tense = past_tense
if delta < timedelta(0): the_format = future_format if delta < timedelta(0): the_tense = future_tense
d = delta2dict( delta ) d = delta2dict( delta )
hlist = [] hlist = []
count = 0 count = 0
@ -28,12 +28,12 @@ def delta2human(delta, precision=2, past_format='{0} ago', future_format='in {0}
hlist.append( '%s %s%s' % ( d[unit], unit, s ) ) hlist.append( '%s %s%s' % ( d[unit], unit, s ) )
count += 1 count += 1
human_delta = ', '.join( hlist ) human_delta = ', '.join( hlist )
return the_format.format(human_delta) return the_tense.format(human_delta)
def human(d, precision=2, past_format='{0} ago', future_format='in {0}'): def human(date_time, precision=2, past_tense='{} ago', future_tense='in {}'):
"""Pass datetime we will return human delta string""" """Accepts a datetime, returns a human readable delta string"""
delta = datetime.now() - d delta = datetime.now() - date_time
return delta2human( delta, precision, past_format, future_format ) return delta2human( delta, precision, past_tense, future_tense )
if __name__ == "__main__": if __name__ == "__main__":
from test_ago import test_output from test_ago import test_output

View file

@ -41,17 +41,17 @@ def test_one_day_singular():
def test_two_day_plural(): def test_two_day_plural():
assert 's' in delta2human( timedelta(2) ) assert 's' in delta2human( timedelta(2) )
def test_past_format(): def test_past_tense():
output = human( PAST, output = human( PAST,
past_format = 'titanic sunk {0} ago', past_tense = 'titanic sunk {} ago',
future_format = 'titanic will sink in {0} from now' future_tense = 'titanic will sink in {} from now'
) )
assert 'titanic sunk' in output assert 'titanic sunk' in output
def test_future_format(): def test_future_tense():
output = human( FUTURE, output = human( FUTURE,
past_format = 'titanic sunk {0} ago', past_tense = 'titanic sunk {} ago',
future_format = 'titanic will sink in {0} from now' future_tense = 'titanic will sink in {} from now'
) )
assert 'titanic will sink in' in output assert 'titanic will sink in' in output
@ -69,11 +69,11 @@ def test_output():
print '\nTesting past:\n' print '\nTesting past:\n'
print delta2dict( PAST_DELTA ) print delta2dict( PAST_DELTA )
print 'Commented ' + delta2human( PAST_DELTA, 3 ) print 'Commented ' + delta2human( PAST_DELTA, 3 )
print human( PAST, past_format = "Commented {0} ago" ) print human( PAST, past_tense = "Commented {} ago" )
print '\nTesting future:\n' print '\nTesting future:\n'
print delta2dict( FUTURE_DELTA ) print delta2dict( FUTURE_DELTA )
print 'Shutdown ' + delta2human( FUTURE_DELTA, 3 ) print 'Shutdown ' + delta2human( FUTURE_DELTA, 3 )
print human( FUTURE, future_format = 'Shutdown in {0} from now' ) print human( FUTURE, future_tense = 'Shutdown in {} from now' )
print '' print ''