summaryrefslogtreecommitdiffstats
path: root/Lib/calendar.py
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2004-11-13 16:18:32 (GMT)
committerTim Peters <tim.peters@gmail.com>2004-11-13 16:18:32 (GMT)
commitbbc0d4409c4edc43d9f9b38bff089fd63f43734a (patch)
treee6a74fe2b0266d8fd9ab8fea8f34f1092aef374c /Lib/calendar.py
parentfba73698240660d9154b6917b87dd333d6fb8284 (diff)
downloadcpython-bbc0d4409c4edc43d9f9b38bff089fd63f43734a.zip
cpython-bbc0d4409c4edc43d9f9b38bff089fd63f43734a.tar.gz
cpython-bbc0d4409c4edc43d9f9b38bff089fd63f43734a.tar.bz2
SF bug 1065388: calendar day/month name lookup too slow
__getitem__() methods: compute only the new spellings needed to satisfy the given indexing object. This is purely an optimization (it should have no effect on visible semantics).
Diffstat (limited to 'Lib/calendar.py')
-rw-r--r--Lib/calendar.py26
1 files changed, 18 insertions, 8 deletions
diff --git a/Lib/calendar.py b/Lib/calendar.py
index c2a9775..7081389 100644
--- a/Lib/calendar.py
+++ b/Lib/calendar.py
@@ -28,27 +28,37 @@ mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# fresh on each call, in case the user changes locale between calls.
class _localized_month:
+
+ _months = [datetime.date(2001, i+1, 1).strftime for i in range(12)]
+ _months.insert(0, lambda x: "")
+
def __init__(self, format):
self.format = format
def __getitem__(self, i):
- data = [datetime.date(2001, j, 1).strftime(self.format)
- for j in range(1, 13)]
- data.insert(0, "")
- return data[i]
+ funcs = self._months[i]
+ if isinstance(i, slice):
+ return [f(self.format) for f in funcs]
+ else:
+ return funcs(self.format)
def __len__(self):
return 13
class _localized_day:
+
+ # January 1, 2001, was a Monday.
+ _days = [datetime.date(2001, 1, i+1).strftime for i in range(7)]
+
def __init__(self, format):
self.format = format
def __getitem__(self, i):
- # January 1, 2001, was a Monday.
- data = [datetime.date(2001, 1, j+1).strftime(self.format)
- for j in range(7)]
- return data[i]
+ funcs = self._days[i]
+ if isinstance(i, slice):
+ return [f(self.format) for f in funcs]
+ else:
+ return funcs(self.format)
def __len__(self):
return 7