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.
This commit is contained in:
RussellBallestrini 2012-07-29 09:15:03 -04:00
parent 389ee21762
commit 51b63aa576
2 changed files with 35 additions and 17 deletions

39
README
View file

@ -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?
==========================

13
ago.py
View file

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