77
88import sys
99import datetime
10+ from enum import IntEnum , global_enum
1011import locale as _locale
1112from itertools import repeat
1213
1617 "timegm" , "month_name" , "month_abbr" , "day_name" , "day_abbr" ,
1718 "Calendar" , "TextCalendar" , "HTMLCalendar" , "LocaleTextCalendar" ,
1819 "LocaleHTMLCalendar" , "weekheader" ,
20+ "Day" , "Month" , "JANUARY" , "FEBRUARY" , "MARCH" ,
21+ "APRIL" , "MAY" , "JUNE" , "JULY" ,
22+ "AUGUST" , "SEPTEMBER" , "OCTOBER" , "NOVEMBER" , "DECEMBER" ,
1923 "MONDAY" , "TUESDAY" , "WEDNESDAY" , "THURSDAY" , "FRIDAY" ,
2024 "SATURDAY" , "SUNDAY" ]
2125
@@ -37,9 +41,35 @@ def __str__(self):
3741 return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self .weekday
3842
3943
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+
4373
4474# Number of days per month (except for February in leap years)
4575mdays = [0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ]
@@ -95,9 +125,6 @@ def __len__(self):
95125month_name = _localized_month ('%B' )
96126month_abbr = _localized_month ('%b' )
97127
98- # Constants for weekdays
99- (MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY , SATURDAY , SUNDAY ) = range (7 )
100-
101128
102129def isleap (year ):
103130 """Return True for leap years, False for non-leap years."""
@@ -125,12 +152,12 @@ def monthrange(year, month):
125152 if not 1 <= month <= 12 :
126153 raise IllegalMonthError (month )
127154 day1 = weekday (year , month , 1 )
128- ndays = mdays [month ] + (month == February and isleap (year ))
155+ ndays = mdays [month ] + (month == FEBRUARY and isleap (year ))
129156 return day1 , ndays
130157
131158
132159def _monthlen (year , month ):
133- return mdays [month ] + (month == February and isleap (year ))
160+ return mdays [month ] + (month == FEBRUARY and isleap (year ))
134161
135162
136163def _prevmonth (year , month ):
@@ -260,10 +287,7 @@ def yeardatescalendar(self, year, width=3):
260287 Each month contains between 4 and 6 weeks and each week contains 1-7
261288 days. Days are datetime.date objects.
262289 """
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 ]
267291 return [months [i :i + width ] for i in range (0 , len (months ), width ) ]
268292
269293 def yeardays2calendar (self , year , width = 3 ):
@@ -273,10 +297,7 @@ def yeardays2calendar(self, year, width=3):
273297 (day number, weekday number) tuples. Day numbers outside this month are
274298 zero.
275299 """
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 ]
280301 return [months [i :i + width ] for i in range (0 , len (months ), width ) ]
281302
282303 def yeardayscalendar (self , year , width = 3 ):
@@ -285,10 +306,7 @@ def yeardayscalendar(self, year, width=3):
285306 yeardatescalendar()). Entries in the week lists are day numbers.
286307 Day numbers outside this month are zero.
287308 """
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 ]
292310 return [months [i :i + width ] for i in range (0 , len (months ), width ) ]
293311
294312
@@ -509,7 +527,7 @@ def formatyear(self, theyear, width=3):
509527 a ('\n ' )
510528 a ('<tr><th colspan="%d" class="%s">%s</th></tr>' % (
511529 width , self .cssclass_year_head , theyear ))
512- for i in range (January , January + 12 , width ):
530+ for i in range (JANUARY , JANUARY + 12 , width ):
513531 # months in this row
514532 months = range (i , min (i + width , 13 ))
515533 a ('<tr>' )
0 commit comments