Mercurial > cpython-lru_cache-weakref
annotate Demo/classes/Dates.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | bb3d1bd1e293 |
| children |
| rev | line source |
|---|---|
| 1378 | 1 # Class Date supplies date objects that support date arithmetic. |
| 2 # | |
| 3 # Date(month,day,year) returns a Date object. An instance prints as, | |
| 4 # e.g., 'Mon 16 Aug 1993'. | |
| 5 # | |
| 6 # Addition, subtraction, comparison operators, min, max, and sorting | |
| 7 # all work as expected for date objects: int+date or date+int returns | |
| 8 # the date `int' days from `date'; date+date raises an exception; | |
| 9 # date-int returns the date `int' days before `date'; date2-date1 returns | |
| 10 # an integer, the number of days from date1 to date2; int-date raises an | |
| 11 # exception; date1 < date2 is true iff date1 occurs before date2 (& | |
| 12 # similarly for other comparisons); min(date1,date2) is the earlier of | |
| 13 # the two dates and max(date1,date2) the later; and date objects can be | |
| 14 # used as dictionary keys. | |
| 15 # | |
| 16 # Date objects support one visible method, date.weekday(). This returns | |
| 17 # the day of the week the date falls on, as a string. | |
| 18 # | |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
19 # Date objects also have 4 read-only data attributes: |
| 1378 | 20 # .month in 1..12 |
| 21 # .day in 1..31 | |
| 22 # .year int or long int | |
| 23 # .ord the ordinal of the date relative to an arbitrary staring point | |
| 24 # | |
| 25 # The Dates module also supplies function today(), which returns the | |
| 26 # current date as a date object. | |
| 27 # | |
| 28 # Those entranced by calendar trivia will be disappointed, as no attempt | |
| 29 # has been made to accommodate the Julian (etc) system. On the other | |
| 30 # hand, at least this package knows that 2000 is a leap year but 2100 | |
| 31 # isn't, and works fine for years with a hundred decimal digits <wink>. | |
| 32 | |
| 33 # Tim Peters tim@ksr.com | |
| 34 # not speaking for Kendall Square Research Corp | |
| 35 | |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
36 # Adapted to Python 1.1 (where some hacks to overcome coercion are unnecessary) |
|
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
37 # by Guido van Rossum |
|
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
38 |
|
29371
8c6d4da5ccd3
Patch #748846: Let Demo/classes/Date.py mention DateTime module.
Martin v. Löwis <martin@v.loewis.de>
parents:
9097
diff
changeset
|
39 # Note that as of Python 2.3, a datetime module is included in the stardard |
|
8c6d4da5ccd3
Patch #748846: Let Demo/classes/Date.py mention DateTime module.
Martin v. Löwis <martin@v.loewis.de>
parents:
9097
diff
changeset
|
40 # library. |
|
8c6d4da5ccd3
Patch #748846: Let Demo/classes/Date.py mention DateTime module.
Martin v. Löwis <martin@v.loewis.de>
parents:
9097
diff
changeset
|
41 |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
42 # vi:set tabsize=8: |
|
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
43 |
| 1378 | 44 _MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May', |
| 9097 | 45 'June', 'July', 'August', 'September', 'October', |
| 46 'November', 'December' ] | |
| 1378 | 47 |
| 48 _DAY_NAMES = [ 'Friday', 'Saturday', 'Sunday', 'Monday', | |
| 9097 | 49 'Tuesday', 'Wednesday', 'Thursday' ] |
| 1378 | 50 |
| 51 _DAYS_IN_MONTH = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] | |
| 52 | |
| 53 _DAYS_BEFORE_MONTH = [] | |
| 54 dbm = 0 | |
| 55 for dim in _DAYS_IN_MONTH: | |
| 56 _DAYS_BEFORE_MONTH.append(dbm) | |
| 57 dbm = dbm + dim | |
| 58 del dbm, dim | |
| 59 | |
| 60 _INT_TYPES = type(1), type(1L) | |
| 61 | |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
62 def _is_leap(year): # 1 if leap year, else 0 |
| 1378 | 63 if year % 4 != 0: return 0 |
| 64 if year % 400 == 0: return 1 | |
| 65 return year % 100 != 0 | |
| 66 | |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
67 def _days_in_year(year): # number of days in year |
| 1378 | 68 return 365 + _is_leap(year) |
| 69 | |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
70 def _days_before_year(year): # number of days before year |
|
49162
bb3d1bd1e293
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents:
38627
diff
changeset
|
71 return year*365L + (year+3)//4 - (year+99)//100 + (year+399)//400 |
| 1378 | 72 |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
73 def _days_in_month(month, year): # number of days in month of year |
| 1378 | 74 if month == 2 and _is_leap(year): return 29 |
| 75 return _DAYS_IN_MONTH[month-1] | |
| 76 | |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
77 def _days_before_month(month, year): # number of days in year before month |
| 1378 | 78 return _DAYS_BEFORE_MONTH[month-1] + (month > 2 and _is_leap(year)) |
| 79 | |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
80 def _date2num(date): # compute ordinal of date.month,day,year |
|
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
81 return _days_before_year(date.year) + \ |
|
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
82 _days_before_month(date.month, date.year) + \ |
| 9097 | 83 date.day |
| 1378 | 84 |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
85 _DI400Y = _days_before_year(400) # number of days in 400 years |
| 1378 | 86 |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
87 def _num2date(n): # return date with ordinal n |
| 1378 | 88 if type(n) not in _INT_TYPES: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29371
diff
changeset
|
89 raise TypeError, 'argument must be integer: %r' % type(n) |
| 1378 | 90 |
| 9097 | 91 ans = Date(1,1,1) # arguments irrelevant; just getting a Date obj |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
92 del ans.ord, ans.month, ans.day, ans.year # un-initialize it |
| 1378 | 93 ans.ord = n |
| 94 | |
|
49162
bb3d1bd1e293
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents:
38627
diff
changeset
|
95 n400 = (n-1)//_DI400Y # # of 400-year blocks preceding |
| 1378 | 96 year, n = 400 * n400, n - _DI400Y * n400 |
|
49162
bb3d1bd1e293
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents:
38627
diff
changeset
|
97 more = n // 365 |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
98 dby = _days_before_year(more) |
| 1378 | 99 if dby >= n: |
| 9097 | 100 more = more - 1 |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
101 dby = dby - _days_in_year(more) |
| 1378 | 102 year, n = year + more, int(n - dby) |
| 103 | |
| 9097 | 104 try: year = int(year) # chop to int, if it fits |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
105 except (ValueError, OverflowError): pass |
| 1378 | 106 |
|
49162
bb3d1bd1e293
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents:
38627
diff
changeset
|
107 month = min(n//29 + 1, 12) |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
108 dbm = _days_before_month(month, year) |
| 1378 | 109 if dbm >= n: |
| 9097 | 110 month = month - 1 |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
111 dbm = dbm - _days_in_month(month, year) |
| 1378 | 112 |
| 113 ans.month, ans.day, ans.year = month, n-dbm, year | |
| 114 return ans | |
| 115 | |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
116 def _num2day(n): # return weekday name of day with ordinal n |
| 1378 | 117 return _DAY_NAMES[ int(n % 7) ] |
| 118 | |
| 119 | |
| 120 class Date: | |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
121 def __init__(self, month, day, year): |
| 9097 | 122 if not 1 <= month <= 12: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29371
diff
changeset
|
123 raise ValueError, 'month must be in 1..12: %r' % (month,) |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
124 dim = _days_in_month(month, year) |
| 9097 | 125 if not 1 <= day <= dim: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29371
diff
changeset
|
126 raise ValueError, 'day must be in 1..%r: %r' % (dim, day) |
| 9097 | 127 self.month, self.day, self.year = month, day, year |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
128 self.ord = _date2num(self) |
| 1378 | 129 |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
130 # don't allow setting existing attributes |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
131 def __setattr__(self, name, value): |
| 9097 | 132 if self.__dict__.has_key(name): |
| 133 raise AttributeError, 'read-only attribute ' + name | |
| 134 self.__dict__[name] = value | |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
135 |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
136 def __cmp__(self, other): |
|
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
137 return cmp(self.ord, other.ord) |
| 1378 | 138 |
| 139 # define a hash function so dates can be used as dictionary keys | |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
140 def __hash__(self): |
|
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
141 return hash(self.ord) |
| 1378 | 142 |
| 143 # print as, e.g., Mon 16 Aug 1993 | |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
144 def __repr__(self): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29371
diff
changeset
|
145 return '%.3s %2d %.3s %r' % ( |
| 9097 | 146 self.weekday(), |
| 147 self.day, | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29371
diff
changeset
|
148 _MONTH_NAMES[self.month-1], |
|
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29371
diff
changeset
|
149 self.year) |
| 1378 | 150 |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
151 # Python 1.1 coerces neither int+date nor date+int |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
152 def __add__(self, n): |
| 9097 | 153 if type(n) not in _INT_TYPES: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29371
diff
changeset
|
154 raise TypeError, 'can\'t add %r to date' % type(n) |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
155 return _num2date(self.ord + n) |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
156 __radd__ = __add__ # handle int+date |
| 1378 | 157 |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
158 # Python 1.1 coerces neither date-int nor date-date |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
159 def __sub__(self, other): |
| 9097 | 160 if type(other) in _INT_TYPES: # date-int |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
161 return _num2date(self.ord - other) |
| 9097 | 162 else: |
| 163 return self.ord - other.ord # date-date | |
| 1378 | 164 |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
165 # complain about int-date |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
166 def __rsub__(self, other): |
| 9097 | 167 raise TypeError, 'Can\'t subtract date from integer' |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
168 |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
169 def weekday(self): |
|
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
170 return _num2day(self.ord) |
| 1378 | 171 |
| 172 def today(): | |
| 173 import time | |
| 174 local = time.localtime(time.time()) | |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
175 return Date(local[1], local[2], local[0]) |
| 1378 | 176 |
|
49162
bb3d1bd1e293
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents:
38627
diff
changeset
|
177 class DateTestError(Exception): |
|
bb3d1bd1e293
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents:
38627
diff
changeset
|
178 pass |
|
bb3d1bd1e293
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents:
38627
diff
changeset
|
179 |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
180 def test(firstyear, lastyear): |
| 1378 | 181 a = Date(9,30,1913) |
| 182 b = Date(9,30,1914) | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29371
diff
changeset
|
183 if repr(a) != 'Tue 30 Sep 1913': |
| 9097 | 184 raise DateTestError, '__repr__ failure' |
|
1966
dd7be8128410
adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1378
diff
changeset
|
185 if (not a < b) or a == b or a > b or b != b: |
| 9097 | 186 raise DateTestError, '__cmp__ failure' |
| 1378 | 187 if a+365 != b or 365+a != b: |
| 9097 | 188 raise DateTestError, '__add__ failure' |
| 1378 | 189 if b-a != 365 or b-365 != a: |
| 9097 | 190 raise DateTestError, '__sub__ failure' |
| 1378 | 191 try: |
| 9097 | 192 x = 1 - a |
| 193 raise DateTestError, 'int-date should have failed' | |
| 1378 | 194 except TypeError: |
| 9097 | 195 pass |
| 1378 | 196 try: |
| 9097 | 197 x = a + b |
| 198 raise DateTestError, 'date+date should have failed' | |
| 1378 | 199 except TypeError: |
| 9097 | 200 pass |
| 1378 | 201 if a.weekday() != 'Tuesday': |
| 9097 | 202 raise DateTestError, 'weekday() failure' |
| 1378 | 203 if max(a,b) is not b or min(a,b) is not a: |
| 9097 | 204 raise DateTestError, 'min/max failure' |
| 1378 | 205 d = {a-1:b, b:a+1} |
| 206 if d[b-366] != b or d[a+(b-a)] != Date(10,1,1913): | |
| 9097 | 207 raise DateTestError, 'dictionary failure' |
| 1378 | 208 |
| 209 # verify date<->number conversions for first and last days for | |
| 210 # all years in firstyear .. lastyear | |
| 211 | |
|
38627
98e81af40007
Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents:
31515
diff
changeset
|
212 lord = _days_before_year(firstyear) |
| 1378 | 213 y = firstyear |
| 214 while y <= lastyear: | |
| 9097 | 215 ford = lord + 1 |
| 216 lord = ford + _days_in_year(y) - 1 | |
| 217 fd, ld = Date(1,1,y), Date(12,31,y) | |
| 218 if (fd.ord,ld.ord) != (ford,lord): | |
| 219 raise DateTestError, ('date->num failed', y) | |
| 220 fd, ld = _num2date(ford), _num2date(lord) | |
| 221 if (1,1,y,12,31,y) != \ | |
| 222 (fd.month,fd.day,fd.year,ld.month,ld.day,ld.year): | |
| 223 raise DateTestError, ('num->date failed', y) | |
| 224 y = y + 1 | |
|
49162
bb3d1bd1e293
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents:
38627
diff
changeset
|
225 |
|
bb3d1bd1e293
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents:
38627
diff
changeset
|
226 if __name__ == '__main__': |
|
bb3d1bd1e293
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents:
38627
diff
changeset
|
227 test(1850, 2150) |
