Skip to content

Commit bb3565d

Browse files
committed
Issue #9151: Demo/classes/Dates.py does not work in 3.x
Made minimal changes to make included test pass.
1 parent 6c96bfe commit bb3565d

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

Demo/classes/Dates.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
# Note that as of Python 2.3, a datetime module is included in the stardard
4040
# library.
4141

42-
# vi:set tabsize=8:
42+
import functools
4343

4444
_MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May',
4545
'June', 'July', 'August', 'September', 'October',
@@ -57,8 +57,6 @@
5757
dbm = dbm + dim
5858
del dbm, dim
5959

60-
_INT_TYPES = type(1), type(1)
61-
6260
def _is_leap(year): # 1 if leap year, else 0
6361
if year % 4 != 0: return 0
6462
if year % 400 == 0: return 1
@@ -85,7 +83,7 @@ def _date2num(date): # compute ordinal of date.month,day,year
8583
_DI400Y = _days_before_year(400) # number of days in 400 years
8684

8785
def _num2date(n): # return date with ordinal n
88-
if type(n) not in _INT_TYPES:
86+
if not isinstance(n, int):
8987
raise TypeError('argument must be integer: %r' % type(n))
9088

9189
ans = Date(1,1,1) # arguments irrelevant; just getting a Date obj
@@ -116,7 +114,7 @@ def _num2date(n): # return date with ordinal n
116114
def _num2day(n): # return weekday name of day with ordinal n
117115
return _DAY_NAMES[ int(n % 7) ]
118116

119-
117+
@functools.total_ordering
120118
class Date:
121119
def __init__(self, month, day, year):
122120
if not 1 <= month <= 12:
@@ -133,8 +131,11 @@ def __setattr__(self, name, value):
133131
raise AttributeError('read-only attribute ' + name)
134132
self.__dict__[name] = value
135133

136-
def __cmp__(self, other):
137-
return cmp(self.ord, other.ord)
134+
def __eq__(self, other):
135+
return self.ord == other.ord
136+
137+
def __lt__(self, other):
138+
return self.ord < other.ord
138139

139140
# define a hash function so dates can be used as dictionary keys
140141
def __hash__(self):
@@ -150,14 +151,14 @@ def __repr__(self):
150151

151152
# Python 1.1 coerces neither int+date nor date+int
152153
def __add__(self, n):
153-
if type(n) not in _INT_TYPES:
154+
if not isinstance(n, int):
154155
raise TypeError('can\'t add %r to date' % type(n))
155156
return _num2date(self.ord + n)
156157
__radd__ = __add__ # handle int+date
157158

158159
# Python 1.1 coerces neither date-int nor date-date
159160
def __sub__(self, other):
160-
if type(other) in _INT_TYPES: # date-int
161+
if isinstance(other, int): # date-int
161162
return _num2date(self.ord - other)
162163
else:
163164
return self.ord - other.ord # date-date

0 commit comments

Comments
 (0)