From 7f8c29572dfe4f5139e442a1c962fcb2f663d51e Mon Sep 17 00:00:00 2001 From: LindyBalboa Date: Thu, 18 Aug 2016 18:50:17 +0200 Subject: [PATCH 1/4] Add parameter checks to DayLocator initiator Check that interval parameter is an integer greater than zero. Delete unuseful 'optimization' meant to prevent exceeding the MAXTICKS variable. During testing it seemed ineffective. The following Locator.raise_if_exceeds exception was triggered first anyways. resolves #6935 --- lib/matplotlib/dates.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index 60bd12ec7179..6bee478e0081 100755 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -821,19 +821,6 @@ def tick_values(self, vmin, vmax): self.rule.set(dtstart=start, until=stop) - # estimate the number of ticks very approximately so we don't - # have to do a very expensive (and potentially near infinite) - # 'between' calculation, only to find out it will fail. - nmax, nmin = date2num((vmax, vmin)) - estimate = (nmax - nmin) / (self._get_unit() * self._get_interval()) - # This estimate is only an estimate, so be really conservative - # about bailing... - if estimate > self.MAXTICKS * 2: - raise RuntimeError( - 'RRuleLocator estimated to generate %d ticks from %s to %s: ' - 'exceeds Locator.MAXTICKS * 2 (%d) ' % (estimate, vmin, vmax, - self.MAXTICKS * 2)) - dates = self.rule.between(vmin, vmax, True) if len(dates) == 0: return date2num([vmin, vmax]) @@ -1254,6 +1241,12 @@ def __init__(self, bymonthday=None, interval=1, tz=None): Default is to tick every day of the month: ``bymonthday=range(1,32)`` """ + if interval < 1: + raise ValueError("The interval parameter must be an integer " + "greater than or equal to one.") + if not isinstance(interval, int): + raise ValueError("The interval parameter must be an integer " + "greater than or equal to one.") if bymonthday is None: bymonthday = range(1, 32) elif isinstance(bymonthday, np.ndarray): From b5c9aa416956918447220c338a8933c2d5cb77f4 Mon Sep 17 00:00:00 2001 From: LindyBalboa Date: Sun, 21 Aug 2016 12:05:29 +0200 Subject: [PATCH 2/4] Consolidate DayLocator interval checks --- lib/matplotlib/dates.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index 6bee478e0081..265538e7a74c 100755 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -1241,12 +1241,8 @@ def __init__(self, bymonthday=None, interval=1, tz=None): Default is to tick every day of the month: ``bymonthday=range(1,32)`` """ - if interval < 1: - raise ValueError("The interval parameter must be an integer " - "greater than or equal to one.") - if not isinstance(interval, int): - raise ValueError("The interval parameter must be an integer " - "greater than or equal to one.") + if not interval == int(interval) or interval < 1: + raise ValueError("interval must be an integer greater than 0") if bymonthday is None: bymonthday = range(1, 32) elif isinstance(bymonthday, np.ndarray): From a2a8dc24860ad6e28d24c0283ddb8c857e1b4447 Mon Sep 17 00:00:00 2001 From: LindyBalboa Date: Mon, 22 Aug 2016 12:00:17 +0200 Subject: [PATCH 3/4] Add tests for dates.DayLocator --- lib/matplotlib/tests/test_dates.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_dates.py b/lib/matplotlib/tests/test_dates.py index 3a60c05048bf..54e5e7a9e77b 100644 --- a/lib/matplotlib/tests/test_dates.py +++ b/lib/matplotlib/tests/test_dates.py @@ -442,7 +442,6 @@ def tz_convert(dt_list, tzinfo): _test_date2num_dst(date_range, tz_convert) - def test_date2num_dst_pandas(): # Test for github issue #3896, but in date2num around DST transitions # with a timezone-aware pandas date_range object. @@ -456,6 +455,13 @@ def tz_convert(*args): _test_date2num_dst(pd.date_range, tz_convert) +def test_DayLocator(): + assert_raises(ValueError, mdates.DayLocator, interval=-1) + assert_raises(ValueError, mdates.DayLocator, interval=-1.5) + assert_raises(ValueError, mdates.DayLocator, interval=0) + assert_raises(ValueError, mdates.DayLocator, interval=1.3) + mdates.DayLocator(interval=1.0) + if __name__ == '__main__': import nose From 160c55df13138e6bff6965ac39779f8d8bebad0b Mon Sep 17 00:00:00 2001 From: LindyBalboa Date: Mon, 22 Aug 2016 12:33:29 +0200 Subject: [PATCH 4/4] PEP8 fixes --- lib/matplotlib/tests/test_dates.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/tests/test_dates.py b/lib/matplotlib/tests/test_dates.py index 54e5e7a9e77b..7e0335566ebf 100644 --- a/lib/matplotlib/tests/test_dates.py +++ b/lib/matplotlib/tests/test_dates.py @@ -442,6 +442,7 @@ def tz_convert(dt_list, tzinfo): _test_date2num_dst(date_range, tz_convert) + def test_date2num_dst_pandas(): # Test for github issue #3896, but in date2num around DST transitions # with a timezone-aware pandas date_range object. @@ -455,6 +456,7 @@ def tz_convert(*args): _test_date2num_dst(pd.date_range, tz_convert) + def test_DayLocator(): assert_raises(ValueError, mdates.DayLocator, interval=-1) assert_raises(ValueError, mdates.DayLocator, interval=-1.5)