From cd08476137dc6fc7713f2868857ca3e0f0058713 Mon Sep 17 00:00:00 2001 From: sbraz Date: Fri, 21 Feb 2014 22:44:06 +0100 Subject: [PATCH] Added Python 3 compatibility. --- ago.py | 7 ++++--- test_ago.py | 29 +++++++++++++---------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/ago.py b/ago.py index 7efe64c..4ba3615 100644 --- a/ago.py +++ b/ago.py @@ -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 } diff --git a/test_ago.py b/test_ago.py index c8dfac9..a564ef4 100644 --- a/test_ago.py +++ b/test_ago.py @@ -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()