From 51b63aa57603e7f02dea7bf8e53717d6222b5e83 Mon Sep 17 00:00:00 2001 From: RussellBallestrini Date: Sun, 29 Jul 2012 09:15:03 -0400 Subject: [PATCH] moved human to delta2human and created a new wrapper function called human which works on datetime objects instead of timedelta objects. Updated README to document new function usage. --- README | 39 ++++++++++++++++++++++++--------------- ago.py | 13 +++++++++++-- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/README b/README index 11001c1..65ea42e 100644 --- a/README +++ b/README @@ -7,9 +7,15 @@ easy_install ago How to use ================== -Import ago and create a timedelta object:: +The ago module comes with three functions: - from ago import delta2dict, human +#. human +#. delta2human +#. delta2dict + +You really only need to worry about *human*:: + + from ago import human, delta2human, delta2dict from datetime import datetime @@ -24,25 +30,28 @@ Import ago and create a timedelta object:: microsecond=4000 ) - # this is right now - d2 = datetime.now() + # to find out how long ago, use the human function + print 'Created ' + human( d1 ) + ' ago.' + + # optionally pass a precision + print 'Created ' + human( d1, 3 ) + ' ago.' + print 'Created ' + human( d1, 6 ) + ' ago.' - # subtract the two for a timedelta object +Now we will document how to use delta2human and delta2dict:: + + # pretend we already have a timedelta object + + # subtract two datetime objects for a timedelta object + d2 = datetime.now() delta = d2 - d1 - # create a dictionary out of the timedelta - # not needed but accessable + # display a human readable timedelta from a timedelta + print 'Created ' + delta2human( delta ) + ' ago.' + + # create a dictionary out of the timedelta delta_dict = delta2dict( delta ) print delta_dict - # display a human readable timedelta - print 'Created ' + human( delta ) + ' ago.' - - # optionally pass a precision - print 'Created ' + human( delta, 3 ) + ' ago.' - print 'Created ' + human( delta, 6 ) + ' ago.' - - How do I thank you? ========================== diff --git a/ago.py b/ago.py index 6e1515e..ecf36c5 100644 --- a/ago.py +++ b/ago.py @@ -9,7 +9,7 @@ def delta2dict( delta ): 'microsecond' : delta.microseconds } -def human( delta, precision = 1 ): +def delta2human( delta, precision = 2 ): """return human readable delta string""" units = ( 'year', 'day', 'hour', 'minute', 'second', 'microsecond' ) d = delta2dict( delta ) @@ -23,6 +23,14 @@ def human( delta, precision = 1 ): count += 1 return ', '.join( hlist ) +def human( d1, precision = 2 ): + """Pass datetime we will return human delta string""" + from datetime import datetime + d2 = datetime.now() + delta = d2 - d1 + return delta2human( delta, precision ) + + def test(): """Test and example usage""" from datetime import datetime @@ -39,6 +47,7 @@ def test(): delta = d2 - d1 print 'This is just the ago.py test:' print delta2dict( delta ) - print 'Commented ' + human( delta, 3 ) + ' ago' + print 'Commented ' + delta2human( delta, 3 ) + ' ago' + print 'Commented ' + human( d1 ) + ' ago' if __name__ == "__main__": test()