From a2574a05d87196bb48e17162dfe3e40d55c8f4ec Mon Sep 17 00:00:00 2001 From: RussellBallestrini Date: Sat, 12 Jan 2013 15:06:09 -0500 Subject: [PATCH] ago 0.0.5 version * consolidated human and delta2human * fixed docs and tests to reflect consolidation --- README.rst | 45 +++++++++++++++++++++------------------------ ago.py | 17 +++++++++-------- setup.py | 2 +- test_ago.py | 29 +++++++++++++++++------------ 4 files changed, 48 insertions(+), 45 deletions(-) diff --git a/README.rst b/README.rst index d9b11fe..f116ee4 100644 --- a/README.rst +++ b/README.rst @@ -1,14 +1,14 @@ -What are human readable timedeltas? | ago.py +What are human readable timedeltas? =============================================== ago.py makes customizable human readable timedeltas, for example: -Testing past:: +Testing past tense:: Russell commented 1 year, 127 days, 16 hours ago You replied 1 year, 127 days ago -Testing future:: +Testing future tense:: Program will shutdown in 2 days, 3 hours, 27 minutes Job will run 2 days, 3 hours from now @@ -30,34 +30,34 @@ or specify *ago* under the *setup_requires* list within your How to use ================== -The ago module comes with three functions: +The ago module comes with two functions: #. human -#. delta2human #. delta2dict You really only need to worry about *human*. Here are all the available arguments and defaults:: - human(date_time, precision=2, past_tense='{} ago', future_tense='in {}'): + human(dt, precision=2, past_tense='{} ago', future_tense='in {}'): -date_time - datetime object to make into human readable, required +dt + either a datetime or timedelta object to become human readable, required precision control how verbose the output should look, optional past_tense - format string used when d is a past datetime, optional + format string used when dt is a past datetime, optional future_tense - format string used when d is a future datetime, optional + format string used when dt is a future datetime, optional Here is an example on how to use *human*:: - from ago import human, delta2human, delta2dict + from ago import human + from ago import delta2dict from datetime import datetime from datetime import timedelta @@ -83,22 +83,11 @@ Here is an example on how to use *human*:: We also support future dates and times:: PRESENT = datetime.now() - PAST = present - timedelta( 492, 58711, 45 ) # days, secs, ms - FUTURE = present + timedelta( 2, 12447, 963 ) # days, secs, ms + PAST = PRESENT - timedelta( 492, 58711, 45 ) # days, secs, ms + FUTURE = PRESENT + timedelta( 2, 12447, 963 ) # days, secs, ms print human( FUTURE ) -Now we will document how to use delta2human and delta2dict:: - - # subtract two datetime objects for a timedelta object - delta = PRESENT - db_date - - # display a human readable timedelta from a timedelta - print 'Created ' + delta2human( delta ) - - # create a dictionary out of the timedelta - print delta2dict( delta ) - Example past_tense and future_tense keyword arguments:: output1 = human( PAST, @@ -116,6 +105,14 @@ Example past_tense and future_tense keyword arguments:: print output2 # titanic will sink in 2 days, 3 hours from now +Now we will document how to use delta2dict:: + + # subtract two datetime objects for a timedelta object + delta = PRESENT - db_date + + # create a dictionary of units out of the timedelta + print delta2dict( delta ) + Need more examples? ========================== diff --git a/ago.py b/ago.py index 54a484b..f39d579 100644 --- a/ago.py +++ b/ago.py @@ -13,10 +13,16 @@ def delta2dict( delta ): 'microsecond' : delta.microseconds } -def delta2human(delta, precision=2, past_tense='{} ago', future_tense='in {}'): - """Accepts a delta, returns a human readable delta string""" +def human(dt, precision=2, past_tense='{} ago', future_tense='in {}'): + """Accept a datetime or timedelta, return a human readable delta string""" + delta = dt + if type(dt) is not type(timedelta()): + delta = datetime.now() - dt + the_tense = past_tense - if delta < timedelta(0): the_tense = future_tense + if delta < timedelta(0): + the_tense = future_tense + d = delta2dict( delta ) hlist = [] count = 0 @@ -30,11 +36,6 @@ def delta2human(delta, precision=2, past_tense='{} ago', future_tense='in {}'): human_delta = ', '.join( hlist ) return the_tense.format(human_delta) -def human(date_time, precision=2, past_tense='{} ago', future_tense='in {}'): - """Accepts a datetime, returns a human readable delta string""" - delta = datetime.now() - date_time - return delta2human( delta, precision, past_tense, future_tense ) - if __name__ == "__main__": from test_ago import test_output test_output() diff --git a/setup.py b/setup.py index 37de001..19d7879 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup setup( name = 'ago', - version = '0.0.4', + version = '0.0.5', description = 'ago: Human readable timedeltas', keywords = 'ago human readable time deltas timedelta datetime', long_description = open('README.rst').read(), diff --git a/test_ago.py b/test_ago.py index 09842d2..79dcc74 100644 --- a/test_ago.py +++ b/test_ago.py @@ -1,10 +1,10 @@ from datetime import datetime from datetime import timedelta -from types import * +from types import StringType +from types import DictType from ago import human -from ago import delta2human from ago import delta2dict PRESENT = datetime.now() @@ -17,9 +17,6 @@ FUTURE_DELTA = PRESENT - FUTURE def test_human_is_string(): assert type(human( PAST )) is StringType -def test_delta2human_is_string(): - assert type(delta2human( PAST_DELTA )) is StringType - def test_delta2dict_is_dict(): assert type(delta2dict( PAST_DELTA )) is DictType @@ -38,10 +35,10 @@ def test_coma_in_three_precision(): assert ',' in human( FUTURE, precision = 3 ) def test_one_day_singular(): - assert 's' not in delta2human( timedelta(1) ) + assert 's' not in human( timedelta(1) ) def test_two_day_plural(): - assert 's' in delta2human( timedelta(2) ) + assert 's' in human( timedelta(2) ) def test_past_tense(): output = human( PAST, @@ -57,25 +54,33 @@ def test_future_tense(): ) assert 'titanic will sink in' in output -def test_valid_dict(): +def test_valid_past_dict(): past_dict = delta2dict( PAST_DELTA ) assert past_dict['year'] == 1 assert past_dict['day'] == 127 assert past_dict['hour'] == 16 assert past_dict['minute'] == 18 + assert past_dict['microsecond'] == 45 +def test_valid_future_dict(): + past_dict = delta2dict( FUTURE_DELTA ) + assert past_dict['year'] == 0 + assert past_dict['day'] == 2 + assert past_dict['hour'] == 3 + assert past_dict['minute'] == 27 + assert past_dict['microsecond'] == 963 def test_output(): """Test and example usage""" - print '\nTesting past:\n' + print '\nTest past tense:\n' print delta2dict( PAST_DELTA ) - print 'Commented ' + delta2human( PAST_DELTA, 3 ) + print 'Commented ' + human( PAST_DELTA, 1 ) print human( PAST, past_tense = "Commented {} ago" ) - print '\nTesting future:\n' + print '\nTest future tense:\n' print delta2dict( FUTURE_DELTA ) - print 'Shutdown ' + delta2human( FUTURE_DELTA, 3 ) + print 'Shutdown ' + human( FUTURE_DELTA, 5 ) print human( FUTURE, future_tense = 'Shutdown in {} from now' ) print ''