7
7
8
8
import sys
9
9
import datetime
10
+ from enum import IntEnum , global_enum
10
11
import locale as _locale
11
12
from itertools import repeat
12
13
16
17
"timegm" , "month_name" , "month_abbr" , "day_name" , "day_abbr" ,
17
18
"Calendar" , "TextCalendar" , "HTMLCalendar" , "LocaleTextCalendar" ,
18
19
"LocaleHTMLCalendar" , "weekheader" ,
20
+ "Day" , "Month" , "JANUARY" , "FEBRUARY" , "MARCH" ,
21
+ "APRIL" , "MAY" , "JUNE" , "JULY" ,
22
+ "AUGUST" , "SEPTEMBER" , "OCTOBER" , "NOVEMBER" , "DECEMBER" ,
19
23
"MONDAY" , "TUESDAY" , "WEDNESDAY" , "THURSDAY" , "FRIDAY" ,
20
24
"SATURDAY" , "SUNDAY" ]
21
25
@@ -37,9 +41,35 @@ def __str__(self):
37
41
return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self .weekday
38
42
39
43
40
- # Constants for months referenced later
41
- January = 1
42
- February = 2
44
+ # Constants for months
45
+ @global_enum
46
+ class Month (IntEnum ):
47
+ JANUARY = 1
48
+ FEBRUARY = 2
49
+ MARCH = 3
50
+ APRIL = 4
51
+ MAY = 5
52
+ JUNE = 6
53
+ JULY = 7
54
+ AUGUST = 8
55
+ SEPTEMBER = 9
56
+ OCTOBER = 10
57
+ NOVEMBER = 11
58
+ DECEMBER = 12
59
+
60
+
61
+ # Constants for days
62
+ @global_enum
63
+ class Day (IntEnum ):
64
+ MONDAY = 0
65
+ TUESDAY = 1
66
+ WEDNESDAY = 2
67
+ THURSDAY = 3
68
+ FRIDAY = 4
69
+ SATURDAY = 5
70
+ SUNDAY = 6
71
+
72
+
43
73
44
74
# Number of days per month (except for February in leap years)
45
75
mdays = [0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ]
@@ -95,9 +125,6 @@ def __len__(self):
95
125
month_name = _localized_month ('%B' )
96
126
month_abbr = _localized_month ('%b' )
97
127
98
- # Constants for weekdays
99
- (MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY , SATURDAY , SUNDAY ) = range (7 )
100
-
101
128
102
129
def isleap (year ):
103
130
"""Return True for leap years, False for non-leap years."""
@@ -125,12 +152,12 @@ def monthrange(year, month):
125
152
if not 1 <= month <= 12 :
126
153
raise IllegalMonthError (month )
127
154
day1 = weekday (year , month , 1 )
128
- ndays = mdays [month ] + (month == February and isleap (year ))
155
+ ndays = mdays [month ] + (month == FEBRUARY and isleap (year ))
129
156
return day1 , ndays
130
157
131
158
132
159
def _monthlen (year , month ):
133
- return mdays [month ] + (month == February and isleap (year ))
160
+ return mdays [month ] + (month == FEBRUARY and isleap (year ))
134
161
135
162
136
163
def _prevmonth (year , month ):
@@ -260,10 +287,7 @@ def yeardatescalendar(self, year, width=3):
260
287
Each month contains between 4 and 6 weeks and each week contains 1-7
261
288
days. Days are datetime.date objects.
262
289
"""
263
- months = [
264
- self .monthdatescalendar (year , i )
265
- for i in range (January , January + 12 )
266
- ]
290
+ months = [self .monthdatescalendar (year , m ) for m in Month ]
267
291
return [months [i :i + width ] for i in range (0 , len (months ), width ) ]
268
292
269
293
def yeardays2calendar (self , year , width = 3 ):
@@ -273,10 +297,7 @@ def yeardays2calendar(self, year, width=3):
273
297
(day number, weekday number) tuples. Day numbers outside this month are
274
298
zero.
275
299
"""
276
- months = [
277
- self .monthdays2calendar (year , i )
278
- for i in range (January , January + 12 )
279
- ]
300
+ months = [self .monthdays2calendar (year , m ) for m in Month ]
280
301
return [months [i :i + width ] for i in range (0 , len (months ), width ) ]
281
302
282
303
def yeardayscalendar (self , year , width = 3 ):
@@ -285,10 +306,7 @@ def yeardayscalendar(self, year, width=3):
285
306
yeardatescalendar()). Entries in the week lists are day numbers.
286
307
Day numbers outside this month are zero.
287
308
"""
288
- months = [
289
- self .monthdayscalendar (year , i )
290
- for i in range (January , January + 12 )
291
- ]
309
+ months = [self .monthdayscalendar (year , m ) for m in Month ]
292
310
return [months [i :i + width ] for i in range (0 , len (months ), width ) ]
293
311
294
312
@@ -509,7 +527,7 @@ def formatyear(self, theyear, width=3):
509
527
a ('\n ' )
510
528
a ('<tr><th colspan="%d" class="%s">%s</th></tr>' % (
511
529
width , self .cssclass_year_head , theyear ))
512
- for i in range (January , January + 12 , width ):
530
+ for i in range (JANUARY , JANUARY + 12 , width ):
513
531
# months in this row
514
532
months = range (i , min (i + width , 13 ))
515
533
a ('<tr>' )
0 commit comments