jenkins-bot has submitted this change and it was merged.
Change subject: [LIB] use calendar method for days of months ......................................................................
[LIB] use calendar method for days of months
- default value 2000 is used of a leap year. - additional generator tests are provided
Bug: T104787 Change-Id: I584650a862dd6302ec7c46389fdf0cb1efc0828f --- M pywikibot/date.py M pywikibot/pagegenerators.py M tests/pagegenerators_tests.py 3 files changed, 58 insertions(+), 16 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/date.py b/pywikibot/date.py index 92ef02b..163b720 100644 --- a/pywikibot/date.py +++ b/pywikibot/date.py @@ -20,7 +20,7 @@ import re import sys
-from pywikibot.tools import first_lower, first_upper +from pywikibot.tools import first_lower, first_upper, deprecated
if sys.version_info[0] > 2: unicode = str @@ -2313,9 +2313,15 @@ formatLimits[dayMnthFmts[monthId]] = _formatLimit_DayOfMonth30
+@deprecated("calendar.monthrange") def getNumberOfDaysInMonth(month): - """Return the number of days in a given month, 1 being January, etc.""" - return formatLimits[dayMnthFmts[month - 1]][2] - 1 + """ + Return the maximum number of days in a given month, 1 being January, etc. + + For February alway 29 will be given, even it is not a leap year. + """ + # use year 2000 which is a leap year + return calendar.monthrange(2000, month)[1]
def getAutoFormat(lang, title, ignoreFirstLetterCase=True): diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py index 0489d59..4fe6fad 100644 --- a/pywikibot/pagegenerators.py +++ b/pywikibot/pagegenerators.py @@ -23,6 +23,7 @@ __version__ = '$Id$' #
+import calendar import codecs import datetime import itertools @@ -2305,12 +2306,14 @@ yield pywikibot.Page(pywikibot.Link(current_year, site))
-def DayPageGenerator(startMonth=1, endMonth=12, site=None): +def DayPageGenerator(startMonth=1, endMonth=12, site=None, year=2000): """ Day page generator.
@param site: Site for generator results. @type site: L{pywikibot.site.BaseSite} + @param year: considering leap year. + @type year: int """ if site is None: site = pywikibot.Site() @@ -2318,7 +2321,7 @@ firstPage = pywikibot.Page(site, fd(startMonth, 1)) pywikibot.output(u"Starting with %s" % firstPage.title(asLink=True)) for month in range(startMonth, endMonth + 1): - for day in range(1, date.getNumberOfDaysInMonth(month) + 1): + for day in range(1, calendar.monthrange(year, month)[1] + 1): yield pywikibot.Page(pywikibot.Link(fd(month, day), site))
diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py index 5e20097..5411b9c 100755 --- a/tests/pagegenerators_tests.py +++ b/tests/pagegenerators_tests.py @@ -2,13 +2,14 @@ # -*- coding: utf-8 -*- """Test pagegenerators module.""" # -# (C) Pywikibot team, 2014 +# (C) Pywikibot team, 2014-2015 # # Distributed under the terms of the MIT license. from __future__ import unicode_literals
__version__ = '$Id$'
+import calendar import datetime import os import sys @@ -318,24 +319,56 @@
"""Test the day page generator."""
- def test_basic(self): - site = self.get_site() - fd = date.FormatDate(site) - startMonth = 1 - endMonth = 12 + @classmethod + def setUpClass(cls): + super(TestDayPageGenerator, cls).setUpClass() + cls.site = cls.get_site() + cls.fd = date.FormatDate(cls.site)
- gen = pagegenerators.DayPageGenerator(startMonth, endMonth, site) + def _run_test(self, startMonth=1, endMonth=12, year=2000): + params = { + 'startMonth': startMonth, + 'endMonth': endMonth, + 'site': self.site, + } + if year != 2000: + params['year'] = year + # use positional parameter + gen1 = pagegenerators.DayPageGenerator(startMonth, endMonth, self.site, + year) + # use keyworded parameter and default for year + gen2 = pagegenerators.DayPageGenerator(**params)
- for page in pagegenerators.DayPageGenerator(startMonth, endMonth, site): + for page in gen1: self.assertIsInstance(page, pywikibot.Page) self.assertTrue(page.isAutoTitle)
expected = [] for month in range(startMonth, endMonth + 1): - for day in range(1, date.getNumberOfDaysInMonth(month) + 1): - expected.append(fd(month, day)) + for day in range(1, calendar.monthrange(year, month)[1] + 1): + expected.append(self.fd(month, day))
- self.assertPageTitlesEqual(gen, expected) + self.assertPageTitlesEqual(gen2, expected) + + def test_basic(self): + """General test for day page generator.""" + self._run_test() + + def test_year_2001(self): + """Test for day page generator of year 2001.""" + self._run_test(2, year=2001) + + def test_year_2100(self): + """Test for day page generator of year 2100.""" + self._run_test(endMonth=2, year=2100) + + def test_start_0(self): + """Test for day page generator with startMonth 0.""" + self.assertRaises(calendar.IllegalMonthError, self._run_test, 0) + + def test_end_13(self): + """Test for day page generator with endMonth 13.""" + self.assertRaises(calendar.IllegalMonthError, self._run_test, 12, 13)
class TestPreloadingGenerator(DefaultSiteTestCase):
pywikibot-commits@lists.wikimedia.org