Added Python 3 compatibility.

This commit is contained in:
sbraz 2014-02-21 22:44:06 +01:00
parent c1991cd380
commit cd08476137
2 changed files with 17 additions and 19 deletions

7
ago.py
View file

@ -1,3 +1,4 @@
from __future__ import division
from datetime import datetime
from datetime import timedelta
@ -5,10 +6,10 @@ def delta2dict( delta ):
"""Accepts a delta, returns a dictionary of units"""
delta = abs( delta )
return {
'year' : delta.days / 365 ,
'year' : delta.days // 365 ,
'day' : delta.days % 365 ,
'hour' : delta.seconds / 3600 ,
'minute' : (delta.seconds / 60) % 60 ,
'hour' : delta.seconds // 3600 ,
'minute' : (delta.seconds // 60) % 60 ,
'second' : delta.seconds % 60 ,
'microsecond' : delta.microseconds
}

View file

@ -1,9 +1,6 @@
from datetime import datetime
from datetime import timedelta
from types import StringType
from types import DictType
from ago import human
from ago import delta2dict
@ -18,13 +15,13 @@ FUTURE_DELTA = PRESENT - FUTURE
ONE_YEAR_FOUR_HOURS_DELTA = timedelta( 365, 14400, 0 )
def test_human_passed_datetime_is_string():
assert type(human( PAST )) is StringType
assert type(human( PAST )) is builtins.str
def test_human_passed_timedelta_is_string():
assert type(human( PAST_DELTA )) is StringType
assert type(human( PAST_DELTA )) is builtins.str
def test_delta2dict_is_dict():
assert type(delta2dict( PAST_DELTA )) is DictType
assert type(delta2dict( PAST_DELTA )) is builtins.dict
def test_ago_in_past_human():
assert 'ago' in human( PAST )
@ -90,18 +87,18 @@ def test_valid_future_dict():
def example_usage():
"""Test and example usage"""
print '\nTest past tense:\n'
print delta2dict( PAST_DELTA )
print 'Commented ' + human( PAST_DELTA, 1 )
print human( PAST, past_tense = "Commented {} ago" )
print('\nTest past tense:\n')
print(delta2dict( PAST_DELTA ))
print('Commented ' + human( PAST_DELTA, 1 ))
print(human( PAST, past_tense = "Commented {} ago" ))
print human( ONE_YEAR_FOUR_HOURS_DELTA, past_tense = "Posted {} ago" )
print(human( ONE_YEAR_FOUR_HOURS_DELTA, past_tense = "Posted {} ago" ))
print '\nTest future tense:\n'
print delta2dict( FUTURE_DELTA )
print 'Shutdown ' + human( FUTURE_DELTA, 5 )
print human( FUTURE, future_tense = 'Shutdown in {} from now' )
print ''
print('\nTest future tense:\n')
print(delta2dict( FUTURE_DELTA ))
print('Shutdown ' + human( FUTURE_DELTA, 5 ))
print(human( FUTURE, future_tense = 'Shutdown in {} from now' ))
print('')
if __name__ == '__main__':
example_usage()