diff options
author | Tim Peters <tim.peters@gmail.com> | 2004-11-13 16:18:32 +0000 |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2004-11-13 16:18:32 +0000 |
commit | b1601d2bb17434b4f1a9629e556188501ce9ceeb (patch) | |
tree | 774d4a92c1d87b1698df1b7d8839eacfe8f9ede6 /Lib/calendar.py | |
parent | bf55814e931aff453f72dff32091b80d183c0a04 (diff) | |
download | cpython-b1601d2bb17434b4f1a9629e556188501ce9ceeb.tar.gz |
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.py | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/Lib/calendar.py b/Lib/calendar.py index c2a9775ebf..70813896d1 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 |