summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael W. Hudson <mwh@python.net>2002-03-16 18:01:05 (GMT)
committerMichael W. Hudson <mwh@python.net>2002-03-16 18:01:05 (GMT)
commit2e6cc334aa9175b090ae97691e7d4f5d46a87a42 (patch)
treedbb3ab1a3a23d96a5dceb3ec57ab51d5c2c2e777
parent023db775f7efc5598a823fc43dc2cc3e401bcda1 (diff)
downloadcpython-2e6cc334aa9175b090ae97691e7d4f5d46a87a42.zip
cpython-2e6cc334aa9175b090ae97691e7d4f5d46a87a42.tar.gz
cpython-2e6cc334aa9175b090ae97691e7d4f5d46a87a42.tar.bz2
This checkin backport two checkins by Skip.
backport montanaro's checkin of revision 1.24 of calendar.py 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. [and then] Corrected _localized_name.__getitem__ based on code in patch 503202 (which I thought was just a bug report, so didn't notice - doh!). This handles slicing, which v 1.23 didn't.
-rw-r--r--Lib/calendar.py21
1 files changed, 15 insertions, 6 deletions
diff --git a/Lib/calendar.py b/Lib/calendar.py
index 0fc60be..9af2c93 100644
--- a/Lib/calendar.py
+++ b/Lib/calendar.py
@@ -25,18 +25,27 @@ February = 2
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
class _localized_name:
- def __init__(self, format):
+ def __init__(self, format, len):
self.format = format
+ self.len = len
def __getitem__(self, item):
- return strftime(self.format, (item,)*9).capitalize()
+ if isinstance(item, int):
+ if item < 0: item += self.len
+ if not 0 <= item < self.len:
+ raise IndexError, "out of range"
+ return strftime(self.format, (item,)*9).capitalize()
+ elif isinstance(item, type(slice(0))):
+ return [self[e] for e in range(self.len)].__getslice__(item.start, item.stop)
+ def __len__(self):
+ return self.len
# Full and abbreviated names of weekdays
-day_name = _localized_name('%A')
-day_abbr = _localized_name('%a')
+day_name = _localized_name('%A', 7)
+day_abbr = _localized_name('%a', 7)
# Full and abbreviated names of months (1-based arrays!!!)
-month_name = _localized_name('%B')
-month_abbr = _localized_name('%b')
+month_name = _localized_name('%B', 13)
+month_abbr = _localized_name('%b', 13)
# Constants for weekdays
(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)