new release, adds abbreviate parameter!
This commit is contained in:
parent
770fb5f3c4
commit
f586eaa8cb
5 changed files with 21 additions and 8 deletions
|
|
@ -7,3 +7,4 @@ syntax: glob
|
|||
*.egg*
|
||||
*.gz
|
||||
build/*
|
||||
.cache/*
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ You really only need to worry about *human*.
|
|||
|
||||
Here are all the available arguments and defaults::
|
||||
|
||||
human(dt, precision=2, past_tense='{} ago', future_tense='in {}'):
|
||||
human(dt, precision=2, past_tense='{} ago', future_tense='in {}', abbreviate=False):
|
||||
|
||||
dt
|
||||
either a datetime or timedelta object to become human readable, required
|
||||
|
|
@ -53,6 +53,9 @@ past_tense
|
|||
future_tense
|
||||
format string used when dt is a future datetime, optional
|
||||
|
||||
abbreviate
|
||||
boolean to abbreviate the units, defaults to False
|
||||
|
||||
|
||||
Here is an example on how to use *human*::
|
||||
|
||||
|
|
|
|||
14
ago.py
14
ago.py
|
|
@ -13,7 +13,7 @@ def delta2dict( delta ):
|
|||
'microsecond' : delta.microseconds
|
||||
}
|
||||
|
||||
def human(dt, precision=2, past_tense='{} ago', future_tense='in {}'):
|
||||
def human(dt, precision=2, past_tense='{} ago', future_tense='in {}', abbreviate=False):
|
||||
"""Accept a datetime or timedelta, return a human readable delta string"""
|
||||
delta = dt
|
||||
if type(dt) is not type(timedelta()):
|
||||
|
|
@ -24,15 +24,19 @@ def human(dt, precision=2, past_tense='{} ago', future_tense='in {}'):
|
|||
the_tense = future_tense
|
||||
|
||||
d = delta2dict( delta )
|
||||
|
||||
hlist = []
|
||||
count = 0
|
||||
units = ( 'year', 'day', 'hour', 'minute', 'second', 'microsecond' )
|
||||
for unit in units:
|
||||
if count >= precision: break # met precision
|
||||
if d[ unit ] == 0: continue # skip 0's
|
||||
s = '' if d[ unit ] == 1 else 's' # handle plurals
|
||||
hlist.append( '%s %s%s' % ( d[unit], unit, s ) )
|
||||
if abbreviate:
|
||||
abr = 'ms' if unit == 'microsecond' else unit[0]
|
||||
hlist.append('{}{}'.format(d[unit], abr))
|
||||
else:
|
||||
s = '' if d[ unit ] == 1 else 's' # handle plurals
|
||||
hlist.append('{} {}{}'.format(d[unit], unit, s))
|
||||
count += 1
|
||||
human_delta = ', '.join( hlist )
|
||||
return the_tense.format(human_delta)
|
||||
|
||||
return the_tense.format(', '.join(hlist))
|
||||
|
|
|
|||
2
setup.py
2
setup.py
|
|
@ -4,7 +4,7 @@ from setuptools import setup
|
|||
|
||||
setup(
|
||||
name = 'ago',
|
||||
version = '0.0.6',
|
||||
version = '0.0.7',
|
||||
description = 'ago: Human readable timedeltas',
|
||||
keywords = 'ago human readable time deltas timedelta datetime',
|
||||
long_description = open('README.rst').read(),
|
||||
|
|
|
|||
|
|
@ -55,6 +55,12 @@ def test_one_day_singular():
|
|||
def test_two_day_plural():
|
||||
assert 's' in human( timedelta(2) )
|
||||
|
||||
def test_abbreviation():
|
||||
assert '2d ago' in human( timedelta(2), abbreviate=True )
|
||||
assert '3d, 12h ago' in human( timedelta(3.5), abbreviate=True )
|
||||
assert '2h, 24m ago' in human( timedelta(.1), abbreviate=True )
|
||||
assert '1y, 35d ago' in human( timedelta(400), abbreviate=True )
|
||||
|
||||
def test_past_tense():
|
||||
output = human( PAST,
|
||||
past_tense = 'titanic sunk {} ago',
|
||||
|
|
@ -85,7 +91,6 @@ def test_valid_future_dict():
|
|||
assert past_dict['minute'] == 27
|
||||
assert past_dict['microsecond'] == 963
|
||||
|
||||
|
||||
def example_usage():
|
||||
"""Test and example usage"""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue