Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4c83495

Browse files
author
Skip Montanaro
committed
make _localized_name instances work more like the tuples they replaced. In
particular, negative indexes work and they are limited by the actual length of the names they represent (weekday and month names). This closes bug #503202.
1 parent 693c6c4 commit 4c83495

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

Lib/calendar.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,25 @@
2525
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
2626

2727
class _localized_name:
28-
def __init__(self, format):
28+
def __init__(self, format, len):
2929
self.format = format
30+
self.len = len
3031
def __getitem__(self, item):
32+
if item > self.len-1 or item < -self.len:
33+
raise IndexError
34+
if item < 0:
35+
item += self.len
3136
return strftime(self.format, (item,)*9).capitalize()
37+
def __len__(self):
38+
return self.len
3239

3340
# Full and abbreviated names of weekdays
34-
day_name = _localized_name('%A')
35-
day_abbr = _localized_name('%a')
41+
day_name = _localized_name('%A', 7)
42+
day_abbr = _localized_name('%a', 7)
3643

3744
# Full and abbreviated names of months (1-based arrays!!!)
38-
month_name = _localized_name('%B')
39-
month_abbr = _localized_name('%b')
45+
month_name = _localized_name('%B', 12)
46+
month_abbr = _localized_name('%b', 12)
4047

4148
# Constants for weekdays
4249
(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)

0 commit comments

Comments
 (0)