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
13
+ import warnings
12
14
13
15
__all__ = ["IllegalMonthError" , "IllegalWeekdayError" , "setfirstweekday" ,
14
16
"firstweekday" , "isleap" , "leapdays" , "weekday" , "monthrange" ,
15
17
"monthcalendar" , "prmonth" , "month" , "prcal" , "calendar" ,
16
18
"timegm" , "month_name" , "month_abbr" , "day_name" , "day_abbr" ,
17
19
"Calendar" , "TextCalendar" , "HTMLCalendar" , "LocaleTextCalendar" ,
18
20
"LocaleHTMLCalendar" , "weekheader" ,
21
+ "Day" , "Month" , "JANUARY" , "FEBRUARY" , "MARCH" ,
22
+ "APRIL" , "MAY" , "JUNE" , "JULY" ,
23
+ "AUGUST" , "SEPTEMBER" , "OCTOBER" , "NOVEMBER" , "DECEMBER" ,
19
24
"MONDAY" , "TUESDAY" , "WEDNESDAY" , "THURSDAY" , "FRIDAY" ,
20
25
"SATURDAY" , "SUNDAY" ]
21
26
@@ -37,9 +42,46 @@ def __str__(self):
37
42
return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self .weekday
38
43
39
44
40
- # Constants for months referenced later
41
- January = 1
42
- February = 2
45
+ def __getattr__ (name ):
46
+ if name in ('January' , 'February' ):
47
+ warnings .warn (f"The '{ name } ' attribute is deprecated, use '{ name .upper ()} ' instead" ,
48
+ DeprecationWarning , stacklevel = 2 )
49
+ if name == 'January' :
50
+ return 1
51
+ else :
52
+ return 2
53
+
54
+ raise AttributeError (f"module '{ __name__ } ' has no attribute '{ name } '" )
55
+
56
+
57
+ # Constants for months
58
+ @global_enum
59
+ class Month (IntEnum ):
60
+ JANUARY = 1
61
+ FEBRUARY = 2
62
+ MARCH = 3
63
+ APRIL = 4
64
+ MAY = 5
65
+ JUNE = 6
66
+ JULY = 7
67
+ AUGUST = 8
68
+ SEPTEMBER = 9
69
+ OCTOBER = 10
70
+ NOVEMBER = 11
71
+ DECEMBER = 12
72
+
73
+
74
+ # Constants for days
75
+ @global_enum
76
+ class Day (IntEnum ):
77
+ MONDAY = 0
78
+ TUESDAY = 1
79
+ WEDNESDAY = 2
80
+ THURSDAY = 3
81
+ FRIDAY = 4
82
+ SATURDAY = 5
83
+ SUNDAY = 6
84
+
43
85
44
86
# Number of days per month (except for February in leap years)
45
87
mdays = [0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ]
@@ -95,9 +137,6 @@ def __len__(self):
95
137
month_name = _localized_month ('%B' )
96
138
month_abbr = _localized_month ('%b' )
97
139
98
- # Constants for weekdays
99
- (MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY , SATURDAY , SUNDAY ) = range (7 )
100
-
101
140
102
141
def isleap (year ):
103
142
"""Return True for leap years, False for non-leap years."""
@@ -116,7 +155,7 @@ def weekday(year, month, day):
116
155
"""Return weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31)."""
117
156
if not datetime .MINYEAR <= year <= datetime .MAXYEAR :
118
157
year = 2000 + year % 400
119
- return datetime .date (year , month , day ).weekday ()
158
+ return Day ( datetime .date (year , month , day ).weekday () )
120
159
121
160
122
161
def monthrange (year , month ):
@@ -125,12 +164,12 @@ def monthrange(year, month):
125
164
if not 1 <= month <= 12 :
126
165
raise IllegalMonthError (month )
127
166
day1 = weekday (year , month , 1 )
128
- ndays = mdays [month ] + (month == February and isleap (year ))
167
+ ndays = mdays [month ] + (month == FEBRUARY and isleap (year ))
129
168
return day1 , ndays
130
169
131
170
132
171
def _monthlen (year , month ):
133
- return mdays [month ] + (month == February and isleap (year ))
172
+ return mdays [month ] + (month == FEBRUARY and isleap (year ))
134
173
135
174
136
175
def _prevmonth (year , month ):
@@ -260,10 +299,7 @@ def yeardatescalendar(self, year, width=3):
260
299
Each month contains between 4 and 6 weeks and each week contains 1-7
261
300
days. Days are datetime.date objects.
262
301
"""
263
- months = [
264
- self .monthdatescalendar (year , i )
265
- for i in range (January , January + 12 )
266
- ]
302
+ months = [self .monthdatescalendar (year , m ) for m in Month ]
267
303
return [months [i :i + width ] for i in range (0 , len (months ), width ) ]
268
304
269
305
def yeardays2calendar (self , year , width = 3 ):
@@ -273,10 +309,7 @@ def yeardays2calendar(self, year, width=3):
273
309
(day number, weekday number) tuples. Day numbers outside this month are
274
310
zero.
275
311
"""
276
- months = [
277
- self .monthdays2calendar (year , i )
278
- for i in range (January , January + 12 )
279
- ]
312
+ months = [self .monthdays2calendar (year , m ) for m in Month ]
280
313
return [months [i :i + width ] for i in range (0 , len (months ), width ) ]
281
314
282
315
def yeardayscalendar (self , year , width = 3 ):
@@ -285,10 +318,7 @@ def yeardayscalendar(self, year, width=3):
285
318
yeardatescalendar()). Entries in the week lists are day numbers.
286
319
Day numbers outside this month are zero.
287
320
"""
288
- months = [
289
- self .monthdayscalendar (year , i )
290
- for i in range (January , January + 12 )
291
- ]
321
+ months = [self .monthdayscalendar (year , m ) for m in Month ]
292
322
return [months [i :i + width ] for i in range (0 , len (months ), width ) ]
293
323
294
324
@@ -509,7 +539,7 @@ def formatyear(self, theyear, width=3):
509
539
a ('\n ' )
510
540
a ('<tr><th colspan="%d" class="%s">%s</th></tr>' % (
511
541
width , self .cssclass_year_head , theyear ))
512
- for i in range (January , January + 12 , width ):
542
+ for i in range (JANUARY , JANUARY + 12 , width ):
513
543
# months in this row
514
544
months = range (i , min (i + width , 13 ))
515
545
a ('<tr>' )
@@ -693,7 +723,7 @@ def main(args):
693
723
parser .add_argument (
694
724
"-L" , "--locale" ,
695
725
default = None ,
696
- help = "locale to be used from month and weekday names"
726
+ help = "locale to use for month and weekday names"
697
727
)
698
728
parser .add_argument (
699
729
"-e" , "--encoding" ,
0 commit comments