mirror of
https://github.com/python/cpython.git
synced 2025-12-04 16:43:27 +00:00
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).
This commit is contained in:
parent
fba7369824
commit
bbc0d4409c
3 changed files with 35 additions and 16 deletions
|
|
@ -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.
|
# fresh on each call, in case the user changes locale between calls.
|
||||||
|
|
||||||
class _localized_month:
|
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):
|
def __init__(self, format):
|
||||||
self.format = format
|
self.format = format
|
||||||
|
|
||||||
def __getitem__(self, i):
|
def __getitem__(self, i):
|
||||||
data = [datetime.date(2001, j, 1).strftime(self.format)
|
funcs = self._months[i]
|
||||||
for j in range(1, 13)]
|
if isinstance(i, slice):
|
||||||
data.insert(0, "")
|
return [f(self.format) for f in funcs]
|
||||||
return data[i]
|
else:
|
||||||
|
return funcs(self.format)
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return 13
|
return 13
|
||||||
|
|
||||||
class _localized_day:
|
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):
|
def __init__(self, format):
|
||||||
self.format = format
|
self.format = format
|
||||||
|
|
||||||
def __getitem__(self, i):
|
def __getitem__(self, i):
|
||||||
# January 1, 2001, was a Monday.
|
funcs = self._days[i]
|
||||||
data = [datetime.date(2001, 1, j+1).strftime(self.format)
|
if isinstance(i, slice):
|
||||||
for j in range(7)]
|
return [f(self.format) for f in funcs]
|
||||||
return data[i]
|
else:
|
||||||
|
return funcs(self.format)
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return 7
|
return 7
|
||||||
|
|
|
||||||
|
|
@ -37,10 +37,9 @@ class CalendarTestCase(unittest.TestCase):
|
||||||
self.assertEqual(len(value), 7)
|
self.assertEqual(len(value), 7)
|
||||||
self.assertEqual(len(value[:]), 7)
|
self.assertEqual(len(value[:]), 7)
|
||||||
# ensure they're all unique
|
# ensure they're all unique
|
||||||
d = {}
|
self.assertEqual(len(set(value)), 7)
|
||||||
for v in value:
|
# verify it "acts like a sequence" in two forms of iteration
|
||||||
d[v] = 1
|
self.assertEqual(value[::-1], list(reversed(value)))
|
||||||
self.assertEqual(len(d), 7)
|
|
||||||
|
|
||||||
def test_months(self):
|
def test_months(self):
|
||||||
for attr in "month_name", "month_abbr":
|
for attr in "month_name", "month_abbr":
|
||||||
|
|
@ -49,10 +48,9 @@ class CalendarTestCase(unittest.TestCase):
|
||||||
self.assertEqual(len(value[:]), 13)
|
self.assertEqual(len(value[:]), 13)
|
||||||
self.assertEqual(value[0], "")
|
self.assertEqual(value[0], "")
|
||||||
# ensure they're all unique
|
# ensure they're all unique
|
||||||
d = {}
|
self.assertEqual(len(set(value)), 13)
|
||||||
for v in value:
|
# verify it "acts like a sequence" in two forms of iteration
|
||||||
d[v] = 1
|
self.assertEqual(value[::-1], list(reversed(value)))
|
||||||
self.assertEqual(len(d), 13)
|
|
||||||
|
|
||||||
|
|
||||||
class MonthCalendarTestCase(unittest.TestCase):
|
class MonthCalendarTestCase(unittest.TestCase):
|
||||||
|
|
|
||||||
11
Misc/NEWS
11
Misc/NEWS
|
|
@ -32,6 +32,17 @@ Library
|
||||||
- ``doctest``'s new support for adding ``pdb.set_trace()`` calls to
|
- ``doctest``'s new support for adding ``pdb.set_trace()`` calls to
|
||||||
doctests was broken in a dramatic but shallow way. Fixed.
|
doctests was broken in a dramatic but shallow way. Fixed.
|
||||||
|
|
||||||
|
- Bug 1065388: ``calendar``'s ``day_name``, ``day_abbr``, ``month_name``,
|
||||||
|
and ``month_abbr`` attributes emulate sequences of locale-correct
|
||||||
|
spellings of month and day names. Because the locale can change at
|
||||||
|
any time, the correct spelling is recomputed whenever one of these is
|
||||||
|
indexed. In the worst case, the index may be a slice object, so these
|
||||||
|
recomputed every day or month name each time they were indexed. This is
|
||||||
|
much slower than necessary in the usual case, when the index is just an
|
||||||
|
integer. In that case, only the single spelling needed is recomputed
|
||||||
|
now; and, when the index is a slice object, only the spellings needed
|
||||||
|
by the slice are recomputed now.
|
||||||
|
|
||||||
- Patch 1061679: Added ``__all__`` to pickletools.py.
|
- Patch 1061679: Added ``__all__`` to pickletools.py.
|
||||||
|
|
||||||
Build
|
Build
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue