summaryrefslogtreecommitdiffstats
path: root/Demo
diff options
context:
space:
mode:
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-07-03 21:42:47 (GMT)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-07-03 21:42:47 (GMT)
commitbb3565d412c01d4ec8613500293aec66a6743568 (patch)
tree919fc2cf740e4fccff9254df0b2ec7b4a72ea2ac /Demo
parent6c96bfe004f3721e6a4c2030d7742e335ed8e7dc (diff)
downloadcpython-bb3565d412c01d4ec8613500293aec66a6743568.zip
cpython-bb3565d412c01d4ec8613500293aec66a6743568.tar.gz
cpython-bb3565d412c01d4ec8613500293aec66a6743568.tar.bz2
Issue #9151: Demo/classes/Dates.py does not work in 3.x
Made minimal changes to make included test pass.
Diffstat (limited to 'Demo')
-rw-r--r--Demo/classes/Dates.py19
1 files changed, 10 insertions, 9 deletions
diff --git a/Demo/classes/Dates.py b/Demo/classes/Dates.py
index e1b054f..857bc90 100644
--- a/Demo/classes/Dates.py
+++ b/Demo/classes/Dates.py
@@ -39,7 +39,7 @@
# Note that as of Python 2.3, a datetime module is included in the stardard
# library.
-# vi:set tabsize=8:
+import functools
_MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October',
@@ -57,8 +57,6 @@ for dim in _DAYS_IN_MONTH:
dbm = dbm + dim
del dbm, dim
-_INT_TYPES = type(1), type(1)
-
def _is_leap(year): # 1 if leap year, else 0
if year % 4 != 0: return 0
if year % 400 == 0: return 1
@@ -85,7 +83,7 @@ def _date2num(date): # compute ordinal of date.month,day,year
_DI400Y = _days_before_year(400) # number of days in 400 years
def _num2date(n): # return date with ordinal n
- if type(n) not in _INT_TYPES:
+ if not isinstance(n, int):
raise TypeError('argument must be integer: %r' % type(n))
ans = Date(1,1,1) # arguments irrelevant; just getting a Date obj
@@ -116,7 +114,7 @@ def _num2date(n): # return date with ordinal n
def _num2day(n): # return weekday name of day with ordinal n
return _DAY_NAMES[ int(n % 7) ]
-
+@functools.total_ordering
class Date:
def __init__(self, month, day, year):
if not 1 <= month <= 12:
@@ -133,8 +131,11 @@ class Date:
raise AttributeError('read-only attribute ' + name)
self.__dict__[name] = value
- def __cmp__(self, other):
- return cmp(self.ord, other.ord)
+ def __eq__(self, other):
+ return self.ord == other.ord
+
+ def __lt__(self, other):
+ return self.ord < other.ord
# define a hash function so dates can be used as dictionary keys
def __hash__(self):
@@ -150,14 +151,14 @@ class Date:
# Python 1.1 coerces neither int+date nor date+int
def __add__(self, n):
- if type(n) not in _INT_TYPES:
+ if not isinstance(n, int):
raise TypeError('can\'t add %r to date' % type(n))
return _num2date(self.ord + n)
__radd__ = __add__ # handle int+date
# Python 1.1 coerces neither date-int nor date-date
def __sub__(self, other):
- if type(other) in _INT_TYPES: # date-int
+ if isinstance(other, int): # date-int
return _num2date(self.ord - other)
else:
return self.ord - other.ord # date-date