jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1339995?usp=email )
Change subject: date: Use functools.cache for date patterns
......................................................................
date: Use functools.cache for date patterns
Replace the manual pattern cache with functools.cache so repeated calls
return before constructing and registering the local singledispatch
helpers. Preserve the existing pattern conversion and cached results.
Change-Id: I1b05b12958ec90bb2e1708ff9d983e2f1d86e987
---
M pywikibot/date.py
1 file changed, 20 insertions(+), 25 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/date.py b/pywikibot/date.py
index 7887ad5..56bcab0 100644
--- a/pywikibot/date.py
+++ b/pywikibot/date.py
@@ -12,7 +12,7 @@
from collections import abc, defaultdict
from collections.abc import Callable, Iterator, Mapping, Sequence
from contextlib import suppress
-from functools import singledispatch
+from functools import cache, singledispatch
from string import digits as _decimalDigits # noqa: N812
from typing import TYPE_CHECKING, Any
@@ -393,10 +393,8 @@
# the size of zero-padding for numbers
_reParameters = re.compile('|'.join(f'(%[1-9]?{s})' for s in _digitDecoders))
-# A map of sitecode+pattern to (re matching object and corresponding decoders)
-_escPtrnCache2: dict[str, tuple[re.Pattern[str], str, list[decoder_type]]] = {}
-
+@cache
def escapePattern2(
pattern: str
) -> tuple[re.Pattern[str], str, list[decoder_type]]:
@@ -435,28 +433,25 @@
f'in {subpattern}!')
return newpattern + re.escape(dec), strpattern + subpattern
- if pattern not in _escPtrnCache2:
- newPattern = '' # match starts at the beginning of the string
- strPattern = ''
- decoders: list[decoder_type] = []
- for s in _reParameters.split(pattern):
- if s is None:
- continue
- if (len(s) in (2, 3) and s[0] == '%'
- and s[-1] in _digitDecoders
- and (len(s) == 2 or s[1] in _decimalDigits)):
- # Must match a "%2d" or "%d" style
- dec = _digitDecoders[s[-1]]
- newPattern, strPattern = decode(dec, s, newPattern, strPattern)
- else:
- newPattern += re.escape(s)
- strPattern += s
+ newPattern = '' # match starts at the beginning of the string
+ strPattern = ''
+ decoders: list[decoder_type] = []
+ for s in _reParameters.split(pattern):
+ if s is None:
+ continue
+ if (len(s) in (2, 3) and s[0] == '%'
+ and s[-1] in _digitDecoders
+ and (len(s) == 2 or s[1] in _decimalDigits)):
+ # Must match a "%2d" or "%d" style
+ dec = _digitDecoders[s[-1]]
+ newPattern, strPattern = decode(dec, s, newPattern, strPattern)
+ else:
+ newPattern += re.escape(s)
+ strPattern += s
- newPattern += '$' # end of the string
- compiledPattern = re.compile(newPattern)
- _escPtrnCache2[pattern] = (compiledPattern, strPattern, decoders)
-
- return _escPtrnCache2[pattern]
+ newPattern += '$' # end of the string
+ compiledPattern = re.compile(newPattern)
+ return compiledPattern, strPattern, decoders
@singledispatch
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1339995?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I1b05b12958ec90bb2e1708ff9d983e2f1d86e987
Gerrit-Change-Number: 1339995
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]