jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/831600 )
Change subject: [IMPR] Improvements for dae module ......................................................................
[IMPR] Improvements for dae module
- add _en_period to Create century and millenium format function for 'en' language to reduce similar code - predefine brMonthNames like enMonthNames and waMonthNames - simplify makeMonthNamedList - update documentation
Change-Id: I3c2d190e5696c52a42f142072c22f98f3f573dbc --- M pywikibot/date.py 1 file changed, 28 insertions(+), 44 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/date.py b/pywikibot/date.py index bd37377..65d533e 100644 --- a/pywikibot/date.py +++ b/pywikibot/date.py @@ -33,12 +33,13 @@ # # Different collections of well known formats # +brMonthNames = ['Genver', "C'hwevrer", 'Meurzh', 'Ebrel', 'Mae', 'Mezheven', + 'Gouere', 'Eost', 'Gwengolo', 'Here', 'Du', 'Kerzu'] enMonthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] waMonthNames = ['djanvî', 'fevrî', 'måss', 'avri', 'may', 'djun', 'djulete', 'awousse', 'setimbe', 'octôbe', 'nôvimbe', 'decimbe'] - dayMnthFmts = ['Day_' + str(s) for s in enMonthNames] # e.g. 'Day_January' yrMnthFmts = ['Year_' + str(s) for s in enMonthNames] # e.g. 'Year_January'
@@ -558,9 +559,7 @@
# Predefined month names which are needed at import time months = { - 'br': lambda v: slh(v, ['Genver', "C'hwevrer", 'Meurzh', 'Ebrel', - 'Mae', 'Mezheven', 'Gouere', 'Eost', - 'Gwengolo', 'Here', 'Du', 'Kerzu']), + 'br': lambda v: slh(v, brMonthNames), 'en': lambda v: slh(v, enMonthNames), 'ja': lambda v: slh(v, makeMonthList('%d月')), 'ko': lambda v: slh(v, makeMonthList('%d월')), @@ -703,6 +702,18 @@ return len(self.data)
+def _en_period(period: str): + """Create century and millenium format function for ``en`` language.""" + return lambda m: multi(m, [ + (lambda v: dh_centuryAD(v, '%dst ' + period), + lambda p: p == 1 or (p > 20 and p % 10 == 1)), + (lambda v: dh_centuryAD(v, '%dnd ' + period), + lambda p: p == 2 or (p > 20 and p % 10 == 2)), + (lambda v: dh_centuryAD(v, '%drd ' + period), + lambda p: p == 3 or (p > 20 and p % 10 == 3)), + (lambda v: dh_centuryAD(v, '%dth ' + period), alwaysTrue)]) + + formats = { 'MonthName': MonthNames(), 'Number': { @@ -1064,14 +1075,7 @@ 'el': lambda m: multi(m, [ (lambda v: dh_centuryAD(v, '%dός αιώνας'), lambda p: p == 20), (lambda v: dh_centuryAD(v, '%dος αιώνας'), alwaysTrue)]), - 'en': lambda m: multi(m, [ - (lambda v: dh_centuryAD(v, '%dst century'), - lambda p: p == 1 or (p > 20 and p % 10 == 1)), - (lambda v: dh_centuryAD(v, '%dnd century'), - lambda p: p == 2 or (p > 20 and p % 10 == 2)), - (lambda v: dh_centuryAD(v, '%drd century'), - lambda p: p == 3 or (p > 20 and p % 10 == 3)), - (lambda v: dh_centuryAD(v, '%dth century'), alwaysTrue)]), + 'en': _en_period('century'), 'eo': lambda v: dh_centuryAD(v, '%d-a jarcento'), 'es': lambda v: dh_centuryAD(v, 'Siglo %R'), 'et': lambda v: dh_centuryAD(v, '%d. sajand'), @@ -1192,14 +1196,7 @@ 'da': lambda v: dh_centuryBC(v, '%d. århundrede f.Kr.'), 'de': lambda v: dh_centuryBC(v, '%d. Jahrhundert v. Chr.'), 'el': lambda v: dh_centuryBC(v, '%dος αιώνας π.Χ.'), - 'en': lambda m: multi(m, [ - (lambda v: dh_centuryBC(v, '%dst century BC'), - lambda p: p == 1 or (p > 20 and p % 10 == 1)), - (lambda v: dh_centuryBC(v, '%dnd century BC'), - lambda p: p == 2 or (p > 20 and p % 10 == 2)), - (lambda v: dh_centuryBC(v, '%drd century BC'), - lambda p: p == 3 or (p > 20 and p % 10 == 3)), - (lambda v: dh_centuryBC(v, '%dth century BC'), alwaysTrue)]), + 'en': _en_period('century BC'), 'eo': lambda v: dh_centuryBC(v, '%d-a jarcento a.K.'), 'es': lambda v: dh_centuryBC(v, 'Siglo %R adC'), 'et': lambda v: dh_centuryBC(v, '%d. aastatuhat eKr'), @@ -1276,15 +1273,7 @@ 'cs': lambda v: dh_millenniumAD(v, '%d. tisíciletí'), 'de': lambda v: dh_millenniumAD(v, '%d. Jahrtausend'), 'el': lambda v: dh_millenniumAD(v, '%dη χιλιετία'), - 'en': lambda m: multi(m, [ - (lambda v: dh_millenniumAD(v, '%dst millennium'), - lambda p: p == 1 or (p > 20 and p % 10 == 1)), - (lambda v: dh_millenniumAD(v, '%dnd millennium'), - lambda p: p == 2 or (p > 20 and p % 10 == 2)), - (lambda v: dh_millenniumAD(v, '%drd millennium'), - lambda p: p == 3 or (p > 20 and p % 10 == 3)), - (lambda v: dh_millenniumAD(v, '%dth millennium'), - alwaysTrue)]), + 'en': _en_period('millennium'), 'es': lambda v: dh_millenniumAD(v, '%R milenio'),
'fa': lambda v: dh_millenniumAD(v, 'هزاره %R (میلادی)'), @@ -1715,23 +1704,20 @@ return [pattern % m for m in range(1, 13)]
-def makeMonthNamedList(lang: str, pattern: str, +def makeMonthNamedList(lang: str, pattern: str = '%s', makeUpperCase: Optional[bool] = None) -> List[str]: """Create a list of 12 elements based on the name of the month.
- The language-dependent month name is used as a formatting argument to the - pattern. The pattern must be have one %s that will be replaced by the - localized month name. - Use %%d for any other parameters that should be preserved. - + The language-dependent month name is used as a formatting argument + to the *pattern*. The *pattern* must be have one ``%s`` that will be + replaced by the localized month name. Use ``%%`` for any other + parameters that should be preserved. """ if makeUpperCase is None: return [pattern % monthName(lang, m) for m in range(1, 13)] - if makeUpperCase: - f = first_upper - else: - f = first_lower - return [pattern % f(monthName(lang, m)) for m in range(1, 13)] + + func = first_upper if makeUpperCase else first_lower + return [pattern % func(monthName(lang, m)) for m in range(1, 13)]
# Add day of the month formats to the formatting table: "en:May 15" @@ -1864,13 +1850,11 @@ '(lambda v: dh_dayOfMnth(v, "%d d' {mname}"), alwaysTrue)])' )
-# Brazil uses '1añ' for the 1st of every month, and number without suffix for -# all other days -brMonthNames = makeMonthNamedList('br', '%s', True) - for i in range(12): pattern = _vowel_pattern if i in (3, 7, 9) else _consonant_pattern formats[dayMnthFmts[i]]['wa'] = eval(pattern.format(mname=waMonthNames[i])) + # Brazil uses '1añ' for the 1st of every month, and number without suffix + # for all other days formats[dayMnthFmts[i]]['br'] = eval( 'lambda m: multi(m, [' '(lambda v: dh_dayOfMnth(v, "%dañ {mname}"), lambda p: p == 1), '