Commit 401d8563 authored by Alexander Belopolsky's avatar Alexander Belopolsky

Issue #9151: Demo/classes/Dates.py does not work in 3.x Converted

descriptive comment into a docstring.  Cast attributes to int in
__init__.  Use __new__ instead of deleting attributes to
"uninitialize".
parent 2cf15854
# Class Date supplies date objects that support date arithmetic. """
# Class Date supplies date objects that support date arithmetic.
# Date(month,day,year) returns a Date object. An instance prints as,
# e.g., 'Mon 16 Aug 1993'. Date(month,day,year) returns a Date object. An instance prints as,
# e.g., 'Mon 16 Aug 1993'.
# Addition, subtraction, comparison operators, min, max, and sorting
# all work as expected for date objects: int+date or date+int returns Addition, subtraction, comparison operators, min, max, and sorting
# the date `int' days from `date'; date+date raises an exception; all work as expected for date objects: int+date or date+int returns
# date-int returns the date `int' days before `date'; date2-date1 returns the date `int' days from `date'; date+date raises an exception;
# an integer, the number of days from date1 to date2; int-date raises an date-int returns the date `int' days before `date'; date2-date1 returns
# exception; date1 < date2 is true iff date1 occurs before date2 (& an integer, the number of days from date1 to date2; int-date raises an
# similarly for other comparisons); min(date1,date2) is the earlier of exception; date1 < date2 is true iff date1 occurs before date2 (&
# the two dates and max(date1,date2) the later; and date objects can be similarly for other comparisons); min(date1,date2) is the earlier of
# used as dictionary keys. the two dates and max(date1,date2) the later; and date objects can be
# used as dictionary keys.
# Date objects support one visible method, date.weekday(). This returns
# the day of the week the date falls on, as a string. Date objects support one visible method, date.weekday(). This returns
# the day of the week the date falls on, as a string.
# Date objects also have 4 read-only data attributes:
# .month in 1..12 Date objects also have 4 read-only data attributes:
# .day in 1..31 .month in 1..12
# .year int or long int .day in 1..31
# .ord the ordinal of the date relative to an arbitrary staring point .year int or long int
# .ord the ordinal of the date relative to an arbitrary staring point
# The Dates module also supplies function today(), which returns the
# current date as a date object. The Dates module also supplies function today(), which returns the
# current date as a date object.
# Those entranced by calendar trivia will be disappointed, as no attempt
# has been made to accommodate the Julian (etc) system. On the other Those entranced by calendar trivia will be disappointed, as no attempt
# hand, at least this package knows that 2000 is a leap year but 2100 has been made to accommodate the Julian (etc) system. On the other
# isn't, and works fine for years with a hundred decimal digits <wink>. hand, at least this package knows that 2000 is a leap year but 2100
isn't, and works fine for years with a hundred decimal digits <wink>.
# Tim Peters tim@ksr.com
# not speaking for Kendall Square Research Corp Tim Peters tim@ksr.com
not speaking for Kendall Square Research Corp
# Adapted to Python 1.1 (where some hacks to overcome coercion are unnecessary)
# by Guido van Rossum Adapted to Python 1.1 (where some hacks to overcome coercion are unnecessary)
by Guido van Rossum
# Note that as of Python 2.3, a datetime module is included in the stardard
# library. Note that as of Python 2.3, a datetime module is included in the stardard
library.
"""
import functools import functools
...@@ -86,8 +88,9 @@ def _num2date(n): # return date with ordinal n ...@@ -86,8 +88,9 @@ def _num2date(n): # return date with ordinal n
if not isinstance(n, int): if not isinstance(n, int):
raise TypeError('argument must be integer: %r' % type(n)) raise TypeError('argument must be integer: %r' % type(n))
ans = Date(1,1,1) # arguments irrelevant; just getting a Date obj # Get uninitialized Date object. This is necesary because once
del ans.ord, ans.month, ans.day, ans.year # un-initialize it # attributes are set, they cannot be changed.
ans = Date.__new__(Date)
ans.ord = n ans.ord = n
n400 = (n-1)//_DI400Y # # of 400-year blocks preceding n400 = (n-1)//_DI400Y # # of 400-year blocks preceding
...@@ -97,11 +100,7 @@ def _num2date(n): # return date with ordinal n ...@@ -97,11 +100,7 @@ def _num2date(n): # return date with ordinal n
if dby >= n: if dby >= n:
more = more - 1 more = more - 1
dby = dby - _days_in_year(more) dby = dby - _days_in_year(more)
year, n = year + more, int(n - dby) year, n = year + more, n - dby
try: year = int(year) # chop to int, if it fits
except (ValueError, OverflowError): pass
month = min(n//29 + 1, 12) month = min(n//29 + 1, 12)
dbm = _days_before_month(month, year) dbm = _days_before_month(month, year)
if dbm >= n: if dbm >= n:
...@@ -112,7 +111,7 @@ def _num2date(n): # return date with ordinal n ...@@ -112,7 +111,7 @@ def _num2date(n): # return date with ordinal n
return ans return ans
def _num2day(n): # return weekday name of day with ordinal n def _num2day(n): # return weekday name of day with ordinal n
return _DAY_NAMES[ int(n % 7) ] return _DAY_NAMES[n % 7]
@functools.total_ordering @functools.total_ordering
class Date: class Date:
...@@ -122,7 +121,7 @@ class Date: ...@@ -122,7 +121,7 @@ class Date:
dim = _days_in_month(month, year) dim = _days_in_month(month, year)
if not 1 <= day <= dim: if not 1 <= day <= dim:
raise ValueError('day must be in 1..%r: %r' % (dim, day)) raise ValueError('day must be in 1..%r: %r' % (dim, day))
self.month, self.day, self.year = month, day, year self.month, self.day, self.year = map(int, (month, day, year))
self.ord = _date2num(self) self.ord = _date2num(self)
# don't allow setting existing attributes # don't allow setting existing attributes
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment