ago 0.0.5 version

* consolidated human and delta2human
* fixed docs and tests to reflect consolidation
This commit is contained in:
RussellBallestrini 2013-01-12 15:06:09 -05:00
parent 1e4fe16b72
commit a2574a05d8
4 changed files with 48 additions and 45 deletions

View file

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

17
ago.py
View file

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

View file

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

View file

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