From 873e91d6c9efd65fe965c9e3063787be31c9705b Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Tue, 13 May 2014 22:02:25 +0200 Subject: [PATCH 01/36] converted assert into exception --- lib/matplotlib/figure.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 986870c78140..6475d5c9e814 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -50,6 +50,8 @@ docstring.interpd.update(projection_names=get_projection_names()) +class AxesException(Exception): + pass class AxesStack(Stack): """ @@ -872,7 +874,9 @@ def add_axes(self, *args, **kwargs): if isinstance(args[0], Axes): a = args[0] - assert(a.get_figure() is self) + if a.get_figure() is not self: + msg = "The Axes must have been created in the present figure" + raise AxesException(msg) else: rect = args[0] projection_class, kwargs, key = process_projection_requirements( @@ -946,7 +950,9 @@ def add_subplot(self, *args, **kwargs): if isinstance(args[0], SubplotBase): a = args[0] - assert(a.get_figure() is self) + if a.get_figure() is not self: + msg = "The Subplot must have been created in the present figure" + raise AxesException(msg) # make a key for the subplot (which includes the axes object id # in the hash) key = self._make_key(*args, **kwargs) From b297f415639868d0894623cdcba4bb4dbc9c23eb Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Mon, 26 May 2014 17:01:58 +0200 Subject: [PATCH 02/36] Exception type modified according to @pelson comment --- lib/matplotlib/figure.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 6475d5c9e814..c26f00dba5ba 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -50,9 +50,6 @@ docstring.interpd.update(projection_names=get_projection_names()) -class AxesException(Exception): - pass - class AxesStack(Stack): """ Specialization of the Stack to handle all tracking of Axes in a Figure. @@ -876,7 +873,7 @@ def add_axes(self, *args, **kwargs): a = args[0] if a.get_figure() is not self: msg = "The Axes must have been created in the present figure" - raise AxesException(msg) + raise ValueError(msg) else: rect = args[0] projection_class, kwargs, key = process_projection_requirements( @@ -952,7 +949,7 @@ def add_subplot(self, *args, **kwargs): a = args[0] if a.get_figure() is not self: msg = "The Subplot must have been created in the present figure" - raise AxesException(msg) + raise ValueError(msg) # make a key for the subplot (which includes the axes object id # in the hash) key = self._make_key(*args, **kwargs) From 187f5846f6019525254e7bd3b0342daede98ca81 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Mon, 26 May 2014 17:05:01 +0200 Subject: [PATCH 03/36] removed assert in `draw_artist` and `redraw_in_frame` --- lib/matplotlib/axes/_base.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index c8e5b3cbb955..92093994653f 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2104,7 +2104,10 @@ def draw_artist(self, a): caches the renderer. It is used to efficiently update Axes data (axis ticks, labels, etc are not updated) """ - assert self._cachedRenderer is not None + if self._cachedRenderer is None: + msg = draw_artist.__name__ + ''' can only be used after an initial + draw which caches the render''' + raise AttributeError(msg) a.draw(self._cachedRenderer) def redraw_in_frame(self): @@ -2113,7 +2116,10 @@ def redraw_in_frame(self): caches the renderer. It is used to efficiently update Axes data (axis ticks, labels, etc are not updated) """ - assert self._cachedRenderer is not None + if self._cachedRenderer is None: + msg = redraw_in_frame.__name__ + ''' can only be used after an + initial draw which caches the render''' + raise AttributeError(msg) self.draw(self._cachedRenderer, inframe=True) def get_renderer_cache(self): From 34c6a5f4ce71e7e54383a87bea43e07b00ecaf4e Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Mon, 26 May 2014 22:34:25 +0200 Subject: [PATCH 04/36] most asserts modified to ValueError --- lib/matplotlib/axes/_axes.py | 62 ++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 94cda34a29d0..7902a19479aa 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -1978,19 +1978,19 @@ def make_iterable(x): if len(edgecolor) < nbars: edgecolor *= nbars - # FIXME: convert the following to proper input validation - # raising ValueError; don't use assert for this. - assert len(left) == nbars, ("incompatible sizes: argument 'left' must " - "be length %d or scalar" % nbars) - assert len(height) == nbars, ("incompatible sizes: argument 'height' " - "must be length %d or scalar" % - nbars) - assert len(width) == nbars, ("incompatible sizes: argument 'width' " - "must be length %d or scalar" % - nbars) - assert len(bottom) == nbars, ("incompatible sizes: argument 'bottom' " - "must be length %d or scalar" % - nbars) + # input validation + if len(left) != nbars: + raise ValueError("incompatible sizes: argument 'left' must " + "be length %d or scalar" % nbars) + if len(height) != nbars: + raise ValueError("incompatible sizes: argument 'height' " + "must be length %d or scalar" % nbars) + if len(width) != nbars: + raise ValueError("incompatible sizes: argument 'width' " + "must be length %d or scalar" % nbars) + if len(bottom) == nbars: + raise ValueError("incompatible sizes: argument 'bottom' " + "must be length %d or scalar" % nbars) patches = [] @@ -2428,8 +2428,10 @@ def pie(self, x, explode=None, labels=None, colors=None, labels = [''] * len(x) if explode is None: explode = [0] * len(x) - assert(len(x) == len(labels)) - assert(len(x) == len(explode)) + if len(x) != len(labels): + raise ValueError("'label' must be of length 'x'") + if len(x) != len(explode): + raise ValueError("'explode' must be of length 'x'") if colors is None: colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w') @@ -3686,8 +3688,9 @@ def scatter(self, x, y, s=20, c=None, marker='o', cmap=None, norm=None, collection.update(kwargs) if colors is None: - if norm is not None: - assert(isinstance(norm, mcolors.Normalize)) + if norm is not None and not isinstance(norm, mcolors.Normalize): + msg = "'norm' must be an instance of 'mcolors.Normalize'" + raise ValueError(msg) collection.set_array(np.asarray(c)) collection.set_cmap(cmap) collection.set_norm(norm) @@ -4057,8 +4060,9 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None, bins = np.sort(bins) accum = bins.searchsorted(accum) - if norm is not None: - assert(isinstance(norm, mcolors.Normalize)) + if norm is not None and not isinstance(norm, mcolors.Normalize): + msg = "'norm' must be an instance of 'mcolors.Normalize'" + raise ValueError(msg) collection.set_array(accum) collection.set_cmap(cmap) collection.set_norm(norm) @@ -4673,8 +4677,9 @@ def imshow(self, X, cmap=None, norm=None, aspect=None, if not self._hold: self.cla() - if norm is not None: - assert(isinstance(norm, mcolors.Normalize)) + if norm is not None and not isinstance(norm, mcolors.Normalize): + msg = "'norm' must be an instance of 'mcolors.Normalize'" + raise ValueError(msg) if aspect is None: aspect = rcParams['image.aspect'] self.set_aspect(aspect) @@ -5000,8 +5005,9 @@ def pcolor(self, *args, **kwargs): collection.set_alpha(alpha) collection.set_array(C) - if norm is not None: - assert(isinstance(norm, mcolors.Normalize)) + if norm is not None and not isinstance(norm, mcolors.Normalize): + msg = "'norm' must be an instance of 'mcolors.Normalize'" + raise ValueError(msg) collection.set_cmap(cmap) collection.set_norm(norm) collection.set_clim(vmin, vmax) @@ -5149,8 +5155,9 @@ def pcolormesh(self, *args, **kwargs): antialiased=antialiased, shading=shading, **kwargs) collection.set_alpha(alpha) collection.set_array(C) - if norm is not None: - assert(isinstance(norm, mcolors.Normalize)) + if norm is not None and not isinstance(norm, mcolors.Normalize): + msg = "'norm' must be an instance of 'mcolors.Normalize'" + raise ValueError(msg) collection.set_cmap(cmap) collection.set_norm(norm) collection.set_clim(vmin, vmax) @@ -5274,8 +5281,9 @@ def pcolorfast(self, *args, **kwargs): cmap = kwargs.pop('cmap', None) vmin = kwargs.pop('vmin', None) vmax = kwargs.pop('vmax', None) - if norm is not None: - assert(isinstance(norm, mcolors.Normalize)) + if norm is not None and not isinstance(norm, mcolors.Normalize): + msg = "'norm' must be an instance of 'mcolors.Normalize'" + raise ValueError(msg) C = args[-1] nr, nc = C.shape From 0c1f9f775a2c57fbafe4249b7e6fd1b05c428fd5 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Wed, 28 May 2014 19:01:12 +0200 Subject: [PATCH 05/36] All asserts substituted by ValueError --- lib/matplotlib/axis.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index dba6fd15f686..4aed76dc85b7 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1728,11 +1728,13 @@ def set_label_position(self, position): ACCEPTS: [ 'top' | 'bottom' ] """ - assert position == 'top' or position == 'bottom' if position == 'top': self.label.set_verticalalignment('baseline') - else: + elif position == 'bottom': self.label.set_verticalalignment('top') + else: + msg = "Position accepts only [ 'top' | 'bottom' ]" + raise ValueError(msg) self.label_position = position def _update_label_position(self, bboxes, bboxes2): @@ -2032,13 +2034,15 @@ def set_label_position(self, position): ACCEPTS: [ 'left' | 'right' ] """ - assert position == 'left' or position == 'right' self.label.set_rotation_mode('anchor') self.label.set_horizontalalignment('center') if position == 'left': self.label.set_verticalalignment('bottom') - else: + elif position == 'right': self.label.set_verticalalignment('top') + else: + msg = "Position accepts only [ 'left' | 'right' ]" + raise ValueError(msg) self.label_position = position def _update_label_position(self, bboxes, bboxes2): @@ -2083,13 +2087,14 @@ def _update_offset_text_position(self, bboxes, bboxes2): ) def set_offset_position(self, position): - assert position == 'left' or position == 'right' - x, y = self.offsetText.get_position() if position == 'left': x = 0 - else: + elif position == 'right': x = 1 + else: + msg = "Position accepts only [ 'left' | 'right' ]" + raise ValueError(msg) self.offsetText.set_ha(position) self.offsetText.set_position((x, y)) From 4cfa781ab566c5501aa45dfde5317edf250ccb4f Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Wed, 28 May 2014 19:03:09 +0200 Subject: [PATCH 06/36] method name explicitly written in the message --- lib/matplotlib/axes/_base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 92093994653f..9b5fa615c8c3 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2105,8 +2105,8 @@ def draw_artist(self, a): data (axis ticks, labels, etc are not updated) """ if self._cachedRenderer is None: - msg = draw_artist.__name__ + ''' can only be used after an initial - draw which caches the render''' + msg = '''draw_artist can only be used after an initial draw which + caches the render''' raise AttributeError(msg) a.draw(self._cachedRenderer) @@ -2117,8 +2117,8 @@ def redraw_in_frame(self): data (axis ticks, labels, etc are not updated) """ if self._cachedRenderer is None: - msg = redraw_in_frame.__name__ + ''' can only be used after an - initial draw which caches the render''' + msg = '''redraw_in_frame can only be used after an initial draw + which caches the render''' raise AttributeError(msg) self.draw(self._cachedRenderer, inframe=True) From aba9d99b9ff780eba9b4cf578dbf3d357dc12e69 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Wed, 28 May 2014 23:36:27 +0200 Subject: [PATCH 07/36] Most asserts changed to ValueErrors. Two changed to warning --- lib/matplotlib/backend_bases.py | 6 +++++- lib/matplotlib/blocking_input.py | 22 +++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index edcc81f95f89..ea8bb9e0bc02 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -969,7 +969,11 @@ def set_clip_path(self, path): Set the clip path and transformation. Path should be a :class:`~matplotlib.transforms.TransformedPath` instance. """ - assert path is None or isinstance(path, transforms.TransformedPath) + if path is not None and not isinstance(path, + transforms.TransformedPath): + msg = "Path should be a matplotlib.transforms.TransformedPath \ + instance." + raise ValueError(msg) self._clippath = path def set_dashes(self, dash_offset, dash_list): diff --git a/lib/matplotlib/blocking_input.py b/lib/matplotlib/blocking_input.py index 2ed22b918664..874a2a881ee8 100644 --- a/lib/matplotlib/blocking_input.py +++ b/lib/matplotlib/blocking_input.py @@ -30,6 +30,8 @@ from matplotlib.cbook import is_sequence_of_strings import matplotlib.lines as mlines +import warnings + class BlockingInput(object): """ @@ -38,8 +40,8 @@ class BlockingInput(object): """ def __init__(self, fig, eventslist=()): self.fig = fig - assert is_sequence_of_strings( - eventslist), "Requires a sequence of event name strings" + if not is_sequence_of_strings(eventslist): + raise ValueError("Requires a sequence of event name strings") self.eventslist = eventslist def on_event(self, event): @@ -95,7 +97,8 @@ def __call__(self, n=1, timeout=30): Blocking call to retrieve n events """ - assert isinstance(n, int), "Requires an integer argument" + if not isinstance(n, int): + raise ValueError("Requires an integer argument") self.n = n self.events = [] @@ -146,9 +149,9 @@ def post_event(self): """ This will be called to process events """ - assert len(self.events) > 0, "No events yet" - - if self.events[-1].name == 'key_press_event': + if len(self.events) <= 0: + warnings.warn("No events yet") + elif self.events[-1].name == 'key_press_event': self.key_event() else: self.mouse_event() @@ -359,9 +362,10 @@ def post_event(self): """ Determines if it is a key event """ - assert len(self.events) > 0, "No events yet" - - self.keyormouse = self.events[-1].name == 'key_press_event' + if len(self.events) <= 0: + warnings.warn("No events yet") + else: + self.keyormouse = self.events[-1].name == 'key_press_event' def __call__(self, timeout=30): """ From 8fbb652bf4ebe1c57da761246d746c1705b9e22e Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Fri, 30 May 2014 09:02:06 +0200 Subject: [PATCH 08/36] c* files done --- lib/matplotlib/contour.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 4bceb7417d60..b6db4e45aaac 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -857,10 +857,12 @@ def __init__(self, ax, *args, **kwargs): else: self.logscale = False - if self.origin is not None: - assert(self.origin in ['lower', 'upper', 'image']) - if self.extent is not None: - assert(len(self.extent) == 4) + if self.origin not in [None, 'lower', 'upper', 'image']: + raise ValueError("If given, *origin* must be one of ['lower', \ + 'upper', 'image']") + if self.extent is not None and len(self.extent) != 4: + raise ValueError("If give, *extent* must be '[ *None* | \ + (x0,x1,y0,y1) ]'") if self.colors is not None and cmap is not None: raise ValueError('Either colors or cmap must be None') if self.origin == 'image': From 5a450464a5a056c4ecb9d7b047bf2524cba5d5af Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Fri, 30 May 2014 10:15:28 +0200 Subject: [PATCH 09/36] assert removed --- lib/matplotlib/figure.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index c26f00dba5ba..96d22dcab17b 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1107,7 +1107,10 @@ def draw_artist(self, a): draw :class:`matplotlib.artist.Artist` instance *a* only -- this is available only after the figure is drawn """ - assert self._cachedRenderer is not None + if self._cachedRenderer is None: + msg = '''draw_artist can only be used after an initial draw which \ + caches the render''' + raise AttributeError(msg) a.draw(self._cachedRenderer) def get_axes(self): From 33fdabea7b0b2a09efb6203e48333dcc650e18d3 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Fri, 30 May 2014 10:17:06 +0200 Subject: [PATCH 10/36] Asserts removed. Side effect: function to check that the inputs have the right sizes added. --- lib/matplotlib/finance.py | 116 ++++++++++++++++++++++++++++++++++---- 1 file changed, 106 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/finance.py b/lib/matplotlib/finance.py index 02b020b58e08..a92bc5c1a4e2 100644 --- a/lib/matplotlib/finance.py +++ b/lib/matplotlib/finance.py @@ -816,6 +816,103 @@ def _candlestick(ax, quotes, width=0.2, colorup='k', colordown='r', return lines, patches +def _check_input(opens, closes, highs, lows, miss=-1): + """Checks that *opens*, *highs*, *lows* and *closes* have the same length. + NOTE: this code assumes if any value open, high, low, close is + missing (*-1*) they all are missing + + Parameters + ---------- + ax : `Axes` + an Axes instance to plot to + opens : sequence + sequence of opening values + highs : sequence + sequence of high values + lows : sequence + sequence of low values + closes : sequence + sequence of closing values + miss: + identifier of the missing data + """ + + def _missing(sequence, miss=-1): + """Returns the index in *sequence* of the missing data, identified by + *miss* + Parameters + ---------- + sequence: + sequence to evaluate + miss: + identifier of the missing data + + Returns + ------- + where_miss: numpy.ndarray + indices of the missing data + """ + return np.where(np.array(sequence)==miss)[0] + + same_length = (len(opens) == len(highs)) and (len(opens) == len(lows)) and + (len(opens) == len(closes)) + _missopens = _missing(opens) + same_missing = (_missopens == _missing(highs).all() and (_missopens == + _missing(lows)).all() and (_missopens == _missing(closes)).all() + + if not (same_length and same_missing): + msg = """ + *opens*, *highs*, *lows* and *closes* must have the same length. + NOTE: this code assumes if any value open, high, low, close is + missing (*-1*) they all must be missing + """ + raise ValueError(msg) + + +def plot_day_summary2(ax, opens, closes, highs, lows, ticksize=4, + colorup='k', colordown='r', + ): + """Represent the time, open, close, high, low, as a vertical line + ranging from low to high. The left tick is the open and the right + tick is the close. + + + This function has been deprecated in 1.4 in favor of + `plot_day_summary2_ochl`, which maintains the original argument + order, or `plot_day_summary2_ohlc`, which uses the + open-high-low-close order. This function will be removed in 1.5 + + + Parameters + ---------- + ax : `Axes` + an Axes instance to plot to + opens : sequence + sequence of opening values + closes : sequence + sequence of closing values + highs : sequence + sequence of high values + lows : sequence + sequence of low values + ticksize : int + size of open and close ticks in points + colorup : color + the color of the lines where close >= open + colordown : color + the color of the lines where close < open + + Returns + ------- + ret : list + a list of lines added to the axes + """ + + warnings.warn(_warn_str.format(fun='plot_day_summary2'), mplDeprecation) + return plot_day_summary2_ohlc(ax, opens, highs, lows, closes, ticksize, + colorup, colordown) + + def plot_day_summary2_ochl(ax, opens, closes, highs, lows, ticksize=4, colorup='k', colordown='r', ): @@ -860,6 +957,9 @@ def plot_day_summary2_ohlc(ax, opens, highs, lows, closes, ticksize=4, """Represent the time, open, high, low, close as a vertical line ranging from low to high. The left tick is the open and the right tick is the close. + *opens*, *highs*, *lows* and *closes* must have the same length. + NOTE: this code assumes if any value open, high, low, close is + missing (*-1*) they all are missing Parameters ---------- @@ -885,8 +985,8 @@ def plot_day_summary2_ohlc(ax, opens, highs, lows, closes, ticksize=4, ret : list a list of lines added to the axes """ - # note this code assumes if any value open, high, low, close is - # missing they all are missing + + _check_input(opens, highs, lows, closes) rangeSegments = [((i, low), (i, high)) for i, low, high in zip(xrange(len(lows)), lows, highs) if low != -1] @@ -919,10 +1019,6 @@ def plot_day_summary2_ohlc(ax, opens, highs, lows, closes, ticksize=4, colors = [colord[open < close] for open, close in zip(opens, closes) if open != -1 and close != -1] - assert(len(rangeSegments) == len(offsetsOpen)) - assert(len(offsetsOpen) == len(offsetsClose)) - assert(len(offsetsClose) == len(colors)) - useAA = 0, # use tuple here lw = 1, # and here rangeCollection = LineCollection(rangeSegments, @@ -1012,6 +1108,9 @@ def candlestick2_ohlc(ax, opens, highs, lows, closes, width=4, """Represent the open, close as a bar line and high low range as a vertical line. + NOTE: this code assumes if any value open, low, high, close is + missing they all are missing + Parameters ---------- @@ -1040,8 +1139,7 @@ def candlestick2_ohlc(ax, opens, highs, lows, closes, width=4, (lineCollection, barCollection) """ - # note this code assumes if any value open, low, high, close is - # missing they all are missing + _check_input(opens, highs, lows, closes) delta = width / 2. barVerts = [((i - delta, open), @@ -1066,8 +1164,6 @@ def candlestick2_ohlc(ax, opens, highs, lows, closes, width=4, for open, close in zip(opens, closes) if open != -1 and close != -1] - assert(len(barVerts) == len(rangeSegments)) - useAA = 0, # use tuple here lw = 0.5, # and here rangeCollection = LineCollection(rangeSegments, From 67bdfea8b73210a620866f69dcfddbc182555f90 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Tue, 3 Jun 2014 08:15:08 +0200 Subject: [PATCH 11/36] Asserts removed throughout the files m* --- lib/matplotlib/image.py | 3 ++- lib/matplotlib/mlab.py | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 2428147218bb..dd6da4883371 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -520,7 +520,8 @@ def set_filterrad(self, filterrad): ACCEPTS: positive float """ r = float(filterrad) - assert(r > 0) + if r<=0: + raise ValueError("The filter radius must be a positive number") self._filterrad = r def get_filterrad(self): diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index 2baedbb952aa..83d542261551 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -1485,7 +1485,8 @@ def cohere_pairs(X, ij, NFFT=256, Fs=2, detrend=detrend_none, # of every channel. If preferSpeedOverMemory, cache the conjugate # as well if cbook.iterable(window): - assert(len(window) == NFFT) + if len(window) != NFFT: + raise ValueError("The length of the window must be equal to NFFT") windowVals = window else: windowVals = window(np.ones(NFFT, X.dtype)) @@ -3575,9 +3576,10 @@ def stineman_interp(xi, x, y, yp=None): """ # Cast key variables as float. - x = np.asarray(x, np.float_) - y = np.asarray(y, np.float_) - assert x.shape == y.shape + x=np.asarray(x, np.float_) + y=np.asarray(y, np.float_) + if x.shape != y.shape: + raise ValueError("'x' and 'y' must be of same shape") if yp is None: yp = slopes(x, y) @@ -3824,7 +3826,8 @@ def poly_below(xmin, xs, ys): ys = numpy.asarray(ys) Nx = len(xs) Ny = len(ys) - assert(Nx == Ny) + if Nx!=Ny: + raise ValueError("'xs' and 'ys' must have the same length") x = xmin*numpy.ones(2*Nx) y = numpy.ones(2*Nx) x[:Nx] = xs From 531004c5de580673199371e0a46f8aade43248aa Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Tue, 3 Jun 2014 11:00:16 +0200 Subject: [PATCH 12/36] Asserts removed throughout the files r* --- lib/matplotlib/patches.py | 10 ++++++---- lib/matplotlib/path.py | 20 +++++++++++++------- lib/matplotlib/projections/geo.py | 8 ++++++-- lib/matplotlib/projections/polar.py | 8 ++++++-- lib/matplotlib/rcsetup.py | 14 ++++++++------ 5 files changed, 39 insertions(+), 21 deletions(-) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index fa82e91607cc..70adbf185e98 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -3094,10 +3094,12 @@ def ensure_quadratic_bezier(path): its control points if true. """ segments = list(path.iter_segments()) - assert len(segments) == 2 + if (len(segments) != 2) or (segments[0][1] != Path.MOVETO) or\ + (segments[1][1] != Path.CURVE3): + msg="'path' it's not a valid quadratice bezier curve" + raise ValueError(msg) + - assert segments[0][1] == Path.MOVETO - assert segments[1][1] == Path.CURVE3 return list(segments[0][0]) + list(segments[1][0]) @@ -3805,7 +3807,7 @@ def transmute(self, path, mutation_size, linewidth): class Wedge(_Base): """ - Wedge(?) shape. Only wokrs with a quadratic bezier curve. The + Wedge(?) shape. Only works with a quadratic bezier curve. The begin point has a width of the tail_width and the end point has a width of 0. At the middle, the width is shrink_factor*tail_width. diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index 90acebb999ae..5f71be309f92 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -136,20 +136,25 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1, else: vertices = np.asarray(vertices, np.float_) + if (vertices.ndim != 2) and (vertices.shape[1] != 2): + msg = "'vertices' must be a 2D list or array with shape Nx2" + raise ValueError(msg) + if codes is not None: codes = np.asarray(codes, self.code_type) - assert codes.ndim == 1 - assert len(codes) == len(vertices) - if len(codes): - assert codes[0] == self.MOVETO + if (codes.ndim != 1) or len(codes) != len(vertices): + msg = "'codes' must be a 1D list or array with the same length" + msg += "of 'vertices'" + raise ValueError(msg) + if codes[0] != self.MOVETO: + msg = "The first element of 'code' must be equal to 'MOVETO': {}" + raise ValueError(msg.format(self.MOVETO)) elif closed: codes = np.empty(len(vertices), dtype=self.code_type) codes[0] = self.MOVETO codes[1:-1] = self.LINETO codes[-1] = self.CLOSEPOLY - assert vertices.ndim == 2 - assert vertices.shape[1] == 2 self._vertices = vertices self._codes = codes @@ -320,7 +325,8 @@ def make_compound_path_from_polys(cls, XY): # the CLOSEPOLY; the vert for the closepoly is ignored but we still # need it to keep the codes aligned with the vertices numpolys, numsides, two = XY.shape - assert(two == 2) + if two != 2: + raise ValueError("The third dimension of 'XY' must be 2") stride = numsides + 1 nverts = numpolys * stride verts = np.zeros((nverts, 2)) diff --git a/lib/matplotlib/projections/geo.py b/lib/matplotlib/projections/geo.py index a81dad29782c..de2695f5e4a1 100644 --- a/lib/matplotlib/projections/geo.py +++ b/lib/matplotlib/projections/geo.py @@ -129,7 +129,9 @@ def _get_affine_transform(self): .translate(0.5, 0.5) def get_xaxis_transform(self,which='grid'): - assert which in ['tick1','tick2','grid'] + if which not in ['tick1','tick2','grid']: + msg = "'which' must be on of ['tick1','tick2','grid']" + raise ValueError(msg) return self._xaxis_transform def get_xaxis_text1_transform(self, pad): @@ -139,7 +141,9 @@ def get_xaxis_text2_transform(self, pad): return self._xaxis_text2_transform, 'top', 'center' def get_yaxis_transform(self,which='grid'): - assert which in ['tick1','tick2','grid'] + if which not in ['tick1','tick2','grid']: + msg = "'which' must be on of ['tick1','tick2','grid']" + raise ValueError(msg) return self._yaxis_transform def get_yaxis_text1_transform(self, pad): diff --git a/lib/matplotlib/projections/polar.py b/lib/matplotlib/projections/polar.py index 5e815a0654a6..f13751eba79b 100644 --- a/lib/matplotlib/projections/polar.py +++ b/lib/matplotlib/projections/polar.py @@ -340,7 +340,9 @@ def _set_lim_and_transforms(self): ) def get_xaxis_transform(self,which='grid'): - assert which in ['tick1','tick2','grid'] + if which not in ['tick1','tick2','grid']: + msg = "'which' must be on of ['tick1','tick2','grid']" + raise ValueError(msg) return self._xaxis_transform def get_xaxis_text1_transform(self, pad): @@ -350,7 +352,9 @@ def get_xaxis_text2_transform(self, pad): return self._xaxis_text2_transform, 'center', 'center' def get_yaxis_transform(self,which='grid'): - assert which in ['tick1','tick2','grid'] + if which not in ['tick1','tick2','grid']: + msg = "'which' must be on of ['tick1','tick2','grid']" + raise ValueError(msg) return self._yaxis_transform def get_yaxis_text1_transform(self, pad): diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 51289832c6b1..560f0be72ba1 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -272,19 +272,21 @@ def validate_colorlist(s): 'return a list of colorspecs' if isinstance(s, six.string_types): return [validate_color(c.strip()) for c in s.split(',')] - else: - assert type(s) in [list, tuple] + elif type(s) in (list, tuple): return [validate_color(c) for c in s] - + else: + msg = "'s' must be of type [string | list | tuple]" + raise ValueError(msg) def validate_stringlist(s): 'return a list' if isinstance(s, six.string_types): return [six.text_type(v.strip()) for v in s.split(',') if v.strip()] - else: - assert type(s) in [list, tuple] + elif type(s) in (list, tuple): return [six.text_type(v) for v in s if v] - + else: + msg = "'s' must be of type [string | list | tuple]" + raise ValueError(msg) validate_orientation = ValidateInStrings( 'orientation', ['landscape', 'portrait']) From eaca138b0ce2ca6d47150bf67eb0f816a484ec38 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Tue, 3 Jun 2014 17:18:40 +0200 Subject: [PATCH 13/36] SyntaxError from Travis corrected --- lib/matplotlib/finance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/finance.py b/lib/matplotlib/finance.py index a92bc5c1a4e2..801cb35f0de1 100644 --- a/lib/matplotlib/finance.py +++ b/lib/matplotlib/finance.py @@ -854,10 +854,10 @@ def _missing(sequence, miss=-1): """ return np.where(np.array(sequence)==miss)[0] - same_length = (len(opens) == len(highs)) and (len(opens) == len(lows)) and + same_length = (len(opens) == len(highs)) and (len(opens) == len(lows)) and\ (len(opens) == len(closes)) _missopens = _missing(opens) - same_missing = (_missopens == _missing(highs).all() and (_missopens == + same_missing = (_missopens == _missing(highs).all() and (_missopens ==\ _missing(lows)).all() and (_missopens == _missing(closes)).all() if not (same_length and same_missing): From 9e4b911e83eb7ac54c99ac267aba5b68da18fc3b Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Tue, 3 Jun 2014 21:51:50 +0200 Subject: [PATCH 14/36] assert removed from files s* --- lib/matplotlib/sankey.py | 51 ++++++++++++++++++++++++------------ lib/matplotlib/scale.py | 9 ++++--- lib/matplotlib/spines.py | 14 +++++++--- lib/matplotlib/streamplot.py | 39 ++++++++++++++++++--------- 4 files changed, 76 insertions(+), 37 deletions(-) diff --git a/lib/matplotlib/sankey.py b/lib/matplotlib/sankey.py index b5f8e735e066..2d3b0d283f82 100755 --- a/lib/matplotlib/sankey.py +++ b/lib/matplotlib/sankey.py @@ -29,7 +29,8 @@ # argument specifies the direction of the arrows. The "main" # inputs/outputs are now specified via an orientation of 0, and there may # be several of each. -# --Added assertions to catch common calling errors +# --Changed assertions to ValueError to catch common calling errors (by +# Francesco Montesano, franz.bergesung@gmail.com) # --Added the physical unit as a string argument to be used in the labels, so # that the values of the flows can usually be applied automatically # --Added an argument for a minimum magnitude below which flows are not shown @@ -150,17 +151,21 @@ def __init__(self, ax=None, scale=1.0, unit='', format='%G', gap=0.25, .. plot:: mpl_examples/api/sankey_demo_basics.py """ # Check the arguments. - assert gap >= 0, ( + if gap <0: + raise ValueError( "The gap is negative.\nThis isn't allowed because it " "would cause the paths to overlap.") - assert radius <= gap, ( + if radius > gap: + raise ValueError( "The inner radius is greater than the path spacing.\n" "This isn't allowed because it would cause the paths to overlap.") - assert head_angle >= 0, ( + if head_angle < 0: + raise ValueError( "The angle is negative.\nThis isn't allowed " "because it would cause inputs to look like " "outputs and vice versa.") - assert tolerance >= 0, ( + if tolerance < 0: + raise ValueError( "The tolerance is negative.\nIt must be a magnitude.") # Create axes if necessary. @@ -470,20 +475,23 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='', rotation /= 90.0 if orientations is None: orientations = [0, 0] - assert len(orientations) == n, ( + if len(orientations) != n: + raise ValueError( "orientations and flows must have the same length.\n" "orientations has length %d, but flows has length %d." % (len(orientations), n)) if labels != '' and getattr(labels, '__iter__', False): # iterable() isn't used because it would give True if labels is a # string - assert len(labels) == n, ( + if len(labels) != n: + raise ValueError( "If labels is a list, then labels and flows must have the " "same length.\nlabels has length %d, but flows has length %d." % (len(labels), n)) else: labels = [labels] * n - assert trunklength >= 0, ( + if trunklength < 0: + raise ValueError( "trunklength is negative.\nThis isn't allowed, because it would " "cause poor layout.") if np.absolute(np.sum(flows)) > self.tolerance: @@ -504,28 +512,35 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='', "cause poor layout.\nConsider changing the scale so" " that the scaled sum is approximately 1.0." % gain, 'helpful') if prior is not None: - assert prior >= 0, "The index of the prior diagram is negative." - assert min(connect) >= 0, ( + if prior < 0: + raise ValueError("The index of the prior diagram is negative.") + if min(connect) < 0: + raise ValueError( "At least one of the connection indices is negative.") - assert prior < len(self.diagrams), ( + if prior >= len(self.diagrams): + raise ValueError( "The index of the prior diagram is %d, but there are " "only %d other diagrams.\nThe index is zero-based." % (prior, len(self.diagrams))) - assert connect[0] < len(self.diagrams[prior].flows), ( + if connect[0] >= len(self.diagrams[prior].flows): + raise ValueError( "The connection index to the source diagram is %d, but " "that diagram has only %d flows.\nThe index is zero-based." % (connect[0], len(self.diagrams[prior].flows))) - assert connect[1] < n, ( + if connect[1] >= n: + raise ValueError( "The connection index to this diagram is %d, but this diagram" "has only %d flows.\n The index is zero-based." % (connect[1], n)) - assert self.diagrams[prior].angles[connect[0]] is not None, ( + if self.diagrams[prior].angles[connect[0]] is not None: + raise ValueError( "The connection cannot be made. Check that the magnitude " "of flow %d of diagram %d is greater than or equal to the " "specified tolerance." % (connect[0], prior)) flow_error = (self.diagrams[prior].flows[connect[0]] + flows[connect[1]]) - assert abs(flow_error) < self.tolerance, ( + if abs(flow_error) >r self.tolerance: + raise ValueError( "The scaled sum of the connected flows is %f, which is not " "within the tolerance (%f)." % (flow_error, self.tolerance)) @@ -556,7 +571,8 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='', if is_input is not None: angles[i] = RIGHT else: - assert orient == -1, ( + if orient != -1: + raise ValueError( "The value of orientations[%d] is %d, " "but it must be -1, 0, or 1." % (i, orient)) if is_input: @@ -566,7 +582,8 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='', # Justify the lengths of the paths. if iterable(pathlengths): - assert len(pathlengths) == n, ( + if len(pathlengths) != n: + raise ValueError( "If pathlengths is a list, then pathlengths and flows must " "have the same length.\npathlengths has length %d, but flows " "has length %d." % (len(pathlengths), n)) diff --git a/lib/matplotlib/scale.py b/lib/matplotlib/scale.py index 69158ad8b23f..03d49c2d200c 100644 --- a/lib/matplotlib/scale.py +++ b/lib/matplotlib/scale.py @@ -440,9 +440,12 @@ def __init__(self, axis, **kwargs): subs = kwargs.pop('subsy', None) linscale = kwargs.pop('linscaley', 1.0) - assert base > 1.0 - assert linthresh > 0.0 - assert linscale > 0.0 + if base <= 1.0: + raise ValueError("'basex/basey' be larger than 1") + if linthresh <= 0.0: + raise ValueError("'linthreshx/linthreshy' must be positive") + if linscale <= 0.0: + raise ValueError("'linscalex/linthreshy' must be positive") self._transform = self.SymmetricalLogTransform(base, linthresh, diff --git a/lib/matplotlib/spines.py b/lib/matplotlib/spines.py index 2673c46b92f2..3aa5ad32fe73 100644 --- a/lib/matplotlib/spines.py +++ b/lib/matplotlib/spines.py @@ -71,7 +71,9 @@ def __init__(self, axes, spine_type, path, **kwargs): # non-rectangular axes is currently implemented, and this lets # them pass through the spines machinery without errors.) self._position = None - assert isinstance(path, matplotlib.path.Path) + if not isinstance(path, matplotlib.path.Path): + msg = "'path' must be an instance of 'matplotlib.path.Path'" + raise ValueError(msg) self._path = path # To support drawing both linear and circular spines, this @@ -176,7 +178,8 @@ def is_frame_like(self): position = ('axes', 0.5) elif position == 'zero': position = ('data', 0) - assert len(position) == 2, "position should be 2-tuple" + if len(position) != 2: + raise ValueError("position should be 2-tuple") position_type, amount = position if position_type == 'outward' and amount == 0: return True @@ -372,8 +375,11 @@ def set_position(self, position): # special positions pass else: - assert len(position) == 2, "position should be 'center' or 2-tuple" - assert position[0] in ['outward', 'axes', 'data'] + if len(position) != 2: + raise ValueError("position should be 'center' or 2-tuple") + if position[0] not in ['outward', 'axes', 'data']: + msg = "position[0] should be in ['outward', 'axes', 'data']" + raise ValueError(msg) self._position = position self._calc_offset_transform() diff --git a/lib/matplotlib/streamplot.py b/lib/matplotlib/streamplot.py index 1bfacdb70339..9893cee1cc63 100644 --- a/lib/matplotlib/streamplot.py +++ b/lib/matplotlib/streamplot.py @@ -90,7 +90,9 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None, use_multicolor_lines = isinstance(color, np.ndarray) if use_multicolor_lines: - assert color.shape == grid.shape + if color.shape != grid.shape: + msg = "If 'color' is given, must have the shape of 'Grid(x,y)'" + raise ValueError(msg) line_colors = [] color = np.ma.masked_invalid(color) else: @@ -98,7 +100,9 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None, arrow_kw['color'] = color if isinstance(linewidth, np.ndarray): - assert linewidth.shape == grid.shape + if linewidth.shape != grid.shape: + msg = "If 'linewidth' is given, must have the shape of 'Grid(x,y)'" + raise ValueError(msg) line_kw['linewidth'] = [] else: line_kw['linewidth'] = linewidth @@ -108,8 +112,9 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None, arrow_kw['zorder'] = zorder ## Sanity checks. - assert u.shape == grid.shape - assert v.shape == grid.shape + if (u.shape != grid.shape) and (v.shape != grid.shape): + msg = "'u' and 'v' must be of shape 'Grid(x,y)'" + raise ValueError(msg) u = np.ma.masked_invalid(u) v = np.ma.masked_invalid(v) @@ -256,19 +261,25 @@ class Grid(object): """Grid of data.""" def __init__(self, x, y): - if len(x.shape) == 2: - x_row = x[0] - assert np.allclose(x_row, x) + if x.ndim == 1: + pass + elif x.ndim == 2: + x_row = x[0, :] + if not np.allclose(x_row, x): + raise ValueError("The rows of 'x' must be equal") x = x_row else: - assert len(x.shape) == 1 + raise ValueError("'x' can have at maximum 2 dimensions") - if len(y.shape) == 2: + if y.ndim == 1: + pass + elif y.ndim == 2: y_col = y[:, 0] - assert np.allclose(y_col, y.T) + if not np.allclose(y_col, y.T): + raise ValueError("The columns of 'y' must be equal") y = y_col else: - assert len(y.shape) == 1 + raise ValueError("'y' can have at maximum 2 dimensions") self.nx = len(x) self.ny = len(y) @@ -304,10 +315,12 @@ class StreamMask(object): def __init__(self, density): if np.isscalar(density): - assert density > 0 + if density <= 0: + raise ValueError("If a scalar, 'density' must be positive") self.nx = self.ny = int(30 * density) else: - assert len(density) == 2 + if len(density) != 2: + raise ValueError("'density' can have at maximum 2 dimensions") self.nx = int(30 * density[0]) self.ny = int(30 * density[1]) self._mask = np.zeros((self.ny, self.nx)) From d12fbb8b7b217fe967f6535993a40eb2b35a690d Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Wed, 4 Jun 2014 21:05:24 +0200 Subject: [PATCH 15/36] asserts removed from t* file. test and tri directories ignored --- lib/matplotlib/table.py | 19 ++++---- lib/matplotlib/text.py | 3 +- lib/matplotlib/ticker.py | 6 ++- lib/matplotlib/transforms.py | 90 ++++++++++++++++++++++++------------ 4 files changed, 78 insertions(+), 40 deletions(-) diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index 497c1eb49ad4..5e9ef4a8b08f 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -154,7 +154,7 @@ class Table(Artist): Each entry in the table can be either text or patches. - Column widths and row heights for the table can be specifified. + Column widths and row heights for the table can be specified. Return value is a sequence of text, line and patch instances that make up the table @@ -482,12 +482,17 @@ def table(ax, rows = len(cellText) cols = len(cellText[0]) for row in cellText: - assert len(row) == cols + if len(row) != cols: + msg = "Each row in 'cellText' must have {} columns" + raise ValueError(msg.format(cols)) if cellColours is not None: - assert len(cellColours) == rows + if len(cellColours) != rows: + raise ValueError("'cellColours' must have {} rows".format(rows)) for row in cellColours: - assert len(row) == cols + if len(row) != cols: + msg = "Each row in 'cellColours' must have {} columns" + raise ValueError(msg.format(cols)) else: cellColours = ['w' * cols] * rows @@ -506,7 +511,8 @@ def table(ax, rowColours = 'w' * rows if rowLabels is not None: - assert len(rowLabels) == rows + if len(rowLabels) != rows: + raise ValueError("'rowLabels' must be of length {}".format(rows)) # If we have column labels, need to shift # the text and colour arrays down 1 row @@ -519,9 +525,6 @@ def table(ax, elif colColours is None: colColours = 'w' * cols - if rowLabels is not None: - assert len(rowLabels) == rows - # Set up cell colours if not given if cellColours is None: cellColours = ['w' * cols] * rows diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 14fdd4533bc3..b3e6c81eaa9d 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -1474,7 +1474,8 @@ def __init__(self, artist, ref_coord, unit="points"): self.set_unit(unit) def set_unit(self, unit): - assert unit in ["points", "pixels"] + if unit not in ["points", "pixels"]: + raise ValueError("'unit' must be one of ['points', 'pixels']") self._unit = unit def get_unit(self): diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index b374d5ce5693..69075fd9595a 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -464,7 +464,8 @@ def set_powerlimits(self, lims): greater than 1e4. See also :meth:`set_scientific`. ''' - assert len(lims) == 2, "argument must be a sequence of length 2" + if len(lims) != 2: + raise ValueError("'lims' must be a sequence of length 2") self._powerlimits = lims def format_data_short(self, value): @@ -1201,7 +1202,8 @@ def closeto(x, y): class Base(object): 'this solution has some hacks to deal with floating point inaccuracies' def __init__(self, base): - assert(base > 0) + if base <= 0: + raise ValueError("'base' must be positive") self._base = base def lt(self, x): diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index e6c1c98ccfaf..2acbd93ba38a 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -593,7 +593,8 @@ def shrunk_to_aspect(self, box_aspect, container=None, fig_aspect=1.0): *fig_aspect*, so that *box_aspect* can also be given as a ratio of the absolute dimensions, not the relative dimensions. """ - assert box_aspect > 0 and fig_aspect > 0 + if box_aspect <= 0 or fig_aspect <= 0: + raise ValueError("'box_aspect' and 'fig_aspect' must be positive") if container is None: container = self w, h = container.size @@ -719,7 +720,8 @@ def union(bboxes): """ Return a :class:`Bbox` that contains all of the given bboxes. """ - assert(len(bboxes)) + if not len(bboxes): + raise ValueError("'bboxes' cannot be empty") if len(bboxes) == 1: return bboxes[0] @@ -1062,10 +1064,15 @@ def __init__(self, bbox, transform, **kwargs): *transform*: a 2D :class:`Transform` """ - assert bbox.is_bbox - assert isinstance(transform, Transform) - assert transform.input_dims == 2 - assert transform.output_dims == 2 + if not bbox.is_bbox: + raise ValueError("'bbox' is not a bbox") + if not isinstance(transform, Transform): + msg = "'transform' must be an instance of" + msg += " 'matplotlib.transform.Transform'" + raise ValueError(msg) + if transform.input_dims != 2 or transform.output_dims != 2: + msg = "The input and output dimensions of 'transform' must be 2" + raise ValueError(msg) BboxBase.__init__(self, **kwargs) self._bbox = bbox @@ -1384,7 +1391,9 @@ def transform_point(self, point): The transformed point is returned as a sequence of length :attr:`output_dims`. """ - assert len(point) == self.input_dims + if len(point) != self.input_dims: + msg = "The length of 'point' must be 'self.input_dims'" + raise ValueError(msg) return self.transform(np.asarray([point]))[0] def transform_path(self, path): @@ -1454,12 +1463,12 @@ def transform_angles(self, angles, pts, radians=False, pushoff=1e-5): if self.input_dims != 2 or self.output_dims != 2: raise NotImplementedError('Only defined in 2D') - # pts must be array with 2 columns for x,y - assert pts.shape[1] == 2 + if pts.shape[1] != 2: + raise ValueError("'pts' must be array with 2 columns for x,y") - # angles must be a column vector and have same number of - # rows as pts - assert np.prod(angles.shape) == angles.shape[0] == pts.shape[0] + if angles.ndim!=1 or angles.shape[0] != pts.shape[0]: + msg = "'angles' must be a column vector and have same number of" + msg += " rows as 'pts'" # Convert to radians if desired if not radians: @@ -1516,7 +1525,10 @@ def __init__(self, child): *child*: A class:`Transform` instance. This child may later be replaced with :meth:`set`. """ - assert isinstance(child, Transform) + if not isinstance(child, Transform): + msg = "'child' must be an instance of" + msg += " 'matplotlib.transform.Transform'" + raise ValueError(msg) Transform.__init__(self) self.input_dims = child.input_dims self.output_dims = child.output_dims @@ -1571,8 +1583,11 @@ def set(self, child): The new child must have the same number of input and output dimensions as the current child. """ - assert child.input_dims == self.input_dims - assert child.output_dims == self.output_dims + if child.input_dims != self.input_dims or \ + child.output_dims != self.output_dims: + msg = "The new child must have the same number of input and output" + msg += " dimensions as the current child." + raise ValueError(msg) self._set(child) @@ -1822,7 +1837,10 @@ def set(self, other): Set this transformation from the frozen copy of another :class:`Affine2DBase` object. """ - assert isinstance(other, Affine2DBase) + if not isinstance(other, Affine2DBase): + msg = "'other' must be an instance of" + msg += " 'matplotlib.transform.Affine2DBase'" + raise ValueError(msg) self._mtx = other.get_matrix() self.invalidate() @@ -2152,10 +2170,13 @@ def __init__(self, x_transform, y_transform, **kwargs): can determine automatically which kind of blended transform to create. """ - assert x_transform.is_affine - assert y_transform.is_affine - assert x_transform.is_separable - assert y_transform.is_separable + is_affine = x_transform.is_affine and y_transform.is_affine + is_separable = x_transform.is_separable and y_transform.is_separable + is_correct = is_correct and is_separable + if not is_correct: + msg = "Both *x_transform* and *y_transform* must be 2D affine" + msg += " transforms." + raise ValueError(msg) Transform.__init__(self, **kwargs) self._x = x_transform @@ -2232,7 +2253,10 @@ def __init__(self, a, b, **kwargs): which can automatically choose the best kind of composite transform instance to create. """ - assert a.output_dims == b.input_dims + if a.output_dims != b.input_dims: + msg = "The output dimension of 'a' must be equal to the input" + msg += " dimensions of 'b'" + raise ValueError(msg) self.input_dims = a.input_dims self.output_dims = b.output_dims @@ -2357,11 +2381,14 @@ def __init__(self, a, b, **kwargs): which can automatically choose the best kind of composite transform instance to create. """ - assert a.output_dims == b.input_dims + if not a.is_affine or not b.is_affine: + raise ValueError("'a' and 'b' must be affine transforms") + if a.output_dims != b.input_dims: + msg = "The output dimension of 'a' must be equal to the input" + msg += " dimensions of 'b'" + raise ValueError(msg) self.input_dims = a.input_dims self.output_dims = b.output_dims - assert a.is_affine - assert b.is_affine Affine2DBase.__init__(self, **kwargs) self._a = a @@ -2436,8 +2463,8 @@ def __init__(self, boxin, boxout, **kwargs): Create a new :class:`BboxTransform` that linearly transforms points from *boxin* to *boxout*. """ - assert boxin.is_bbox - assert boxout.is_bbox + if not boxin.is_bbox or not boxout.is_bbox: + msg = "'boxin' and 'boxout' must be bbox" Affine2DBase.__init__(self, **kwargs) self._boxin = boxin @@ -2480,7 +2507,8 @@ def __init__(self, boxout, **kwargs): Create a new :class:`BboxTransformTo` that linearly transforms points from the unit bounding box to *boxout*. """ - assert boxout.is_bbox + if not boxout.is_bbox: + raise ValueError("'boxout' must be bbox") Affine2DBase.__init__(self, **kwargs) self._boxout = boxout @@ -2538,7 +2566,8 @@ class BboxTransformFrom(Affine2DBase): is_separable = True def __init__(self, boxin, **kwargs): - assert boxin.is_bbox + if not boxin.is_bbox: + raise ValueError("'boxin' must be bbox") Affine2DBase.__init__(self, **kwargs) self._boxin = boxin @@ -2613,7 +2642,10 @@ def __init__(self, path, transform): Create a new :class:`TransformedPath` from the given :class:`~matplotlib.path.Path` and :class:`Transform`. """ - assert isinstance(transform, Transform) + if not isinstance(transform, Transform): + msg = "'transform' must be an instance of" + msg += " 'matplotlib.transform.Transform'" + raise ValueError(msg) TransformNode.__init__(self) self._path = path From 7f8062802beb57cc71e7ad7f622d8d7b3c19fc0c Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Wed, 4 Jun 2014 21:08:28 +0200 Subject: [PATCH 16/36] asserts removed from [u-z]* files --- lib/matplotlib/widgets.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 6675903b6877..2261114a6c49 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -1306,7 +1306,8 @@ def __init__(self, ax, onselect, direction, minspan=None, useblit=False, if rectprops is None: rectprops = dict(facecolor='red', alpha=0.5) - assert direction in ['horizontal', 'vertical'], 'Must choose horizontal or vertical for direction' + if direction not in ['horizontal', 'vertical']: + raise ValueError('Must choose horizontal or vertical for direction') self.direction = direction self.rect = None @@ -1558,7 +1559,8 @@ def __init__(self, ax, onselect, drawtype='box', self.minspanx = minspanx self.minspany = minspany - assert(spancoords in ('data', 'pixels')) + if spancoords not in ('data', 'pixels'): + raise ValueError("'spancoords' must be one of ['data', 'pixels']") self.spancoords = spancoords self.drawtype = drawtype From 4fc36c64dc0c6898868dca1baad47a6bd67b47a7 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Wed, 4 Jun 2014 21:30:41 +0200 Subject: [PATCH 17/36] the only assert in a python public function removed --- lib/matplotlib/tri/tripcolor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tri/tripcolor.py b/lib/matplotlib/tri/tripcolor.py index 449ec03a0ed5..329b181eca93 100644 --- a/lib/matplotlib/tri/tripcolor.py +++ b/lib/matplotlib/tri/tripcolor.py @@ -138,7 +138,10 @@ def tripcolor(ax, *args, **kwargs): collection.set_alpha(alpha) collection.set_array(C) if norm is not None: - assert(isinstance(norm, Normalize)) + if not isinstance(norm, Normalize): + msg = "'norm' must be an instance of" + msg += " 'Normalize'" + raise ValueError(msg) collection.set_cmap(cmap) collection.set_norm(norm) if vmin is not None or vmax is not None: From 617b62279e9d9078b1ea1f0103877dcac2884b7c Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Wed, 18 Jun 2014 22:10:54 +0200 Subject: [PATCH 18/36] Bug introduced while getting rid of the asserts fixed --- lib/matplotlib/axes/_axes.py | 2 +- lib/matplotlib/finance.py | 2 +- lib/matplotlib/path.py | 2 +- lib/matplotlib/transforms.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 7902a19479aa..391c8cc214c0 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -1988,7 +1988,7 @@ def make_iterable(x): if len(width) != nbars: raise ValueError("incompatible sizes: argument 'width' " "must be length %d or scalar" % nbars) - if len(bottom) == nbars: + if len(bottom) != nbars: raise ValueError("incompatible sizes: argument 'bottom' " "must be length %d or scalar" % nbars) diff --git a/lib/matplotlib/finance.py b/lib/matplotlib/finance.py index 801cb35f0de1..7f559553dfa6 100644 --- a/lib/matplotlib/finance.py +++ b/lib/matplotlib/finance.py @@ -857,7 +857,7 @@ def _missing(sequence, miss=-1): same_length = (len(opens) == len(highs)) and (len(opens) == len(lows)) and\ (len(opens) == len(closes)) _missopens = _missing(opens) - same_missing = (_missopens == _missing(highs).all() and (_missopens ==\ + same_missing = (_missopens == _missing(highs)).all() and (_missopens ==\ _missing(lows)).all() and (_missopens == _missing(closes)).all() if not (same_length and same_missing): diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index 5f71be309f92..37a6c701f4eb 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -146,7 +146,7 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1, msg = "'codes' must be a 1D list or array with the same length" msg += "of 'vertices'" raise ValueError(msg) - if codes[0] != self.MOVETO: + if len(codes) and codes[0] != self.MOVETO: msg = "The first element of 'code' must be equal to 'MOVETO': {}" raise ValueError(msg.format(self.MOVETO)) elif closed: diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index 2acbd93ba38a..cf717c5cf464 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -2172,7 +2172,7 @@ def __init__(self, x_transform, y_transform, **kwargs): """ is_affine = x_transform.is_affine and y_transform.is_affine is_separable = x_transform.is_separable and y_transform.is_separable - is_correct = is_correct and is_separable + is_correct = is_affine and is_separable if not is_correct: msg = "Both *x_transform* and *y_transform* must be 2D affine" msg += " transforms." From 2cbb3265173cd6dd55223a55b36b853b876940a5 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 10:44:44 +0100 Subject: [PATCH 19/36] typo fixed (broke building documentation) --- lib/matplotlib/sankey.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/sankey.py b/lib/matplotlib/sankey.py index 2d3b0d283f82..f07f41906a98 100755 --- a/lib/matplotlib/sankey.py +++ b/lib/matplotlib/sankey.py @@ -539,7 +539,7 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='', "specified tolerance." % (connect[0], prior)) flow_error = (self.diagrams[prior].flows[connect[0]] + flows[connect[1]]) - if abs(flow_error) >r self.tolerance: + if abs(flow_error) > self.tolerance: raise ValueError( "The scaled sum of the connected flows is %f, which is not " "within the tolerance (%f)." % (flow_error, self.tolerance)) From 20966e93bc8b54d722c65fb45024f04c53ef3dc0 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 11:05:32 +0100 Subject: [PATCH 20/36] pep8 fixed. plot_day_summary2 removed (retained by error when rebasing) --- lib/matplotlib/finance.py | 66 +++++++++------------------------------ 1 file changed, 14 insertions(+), 52 deletions(-) diff --git a/lib/matplotlib/finance.py b/lib/matplotlib/finance.py index 7f559553dfa6..b74f7c37a253 100644 --- a/lib/matplotlib/finance.py +++ b/lib/matplotlib/finance.py @@ -736,7 +736,7 @@ def candlestick_ohlc(ax, quotes, width=0.2, colorup='k', colordown='r', def _candlestick(ax, quotes, width=0.2, colorup='k', colordown='r', - alpha=1.0, ochl=True): + alpha=1.0, ochl=True): """ Plot the time, open, high, low, close as a vertical line ranging @@ -817,7 +817,7 @@ def _candlestick(ax, quotes, width=0.2, colorup='k', colordown='r', def _check_input(opens, closes, highs, lows, miss=-1): - """Checks that *opens*, *highs*, *lows* and *closes* have the same length. + """Checks that *opens*, *highs*, *lows* and *closes* have the same length. NOTE: this code assumes if any value open, high, low, close is missing (*-1*) they all are missing @@ -833,18 +833,24 @@ def _check_input(opens, closes, highs, lows, miss=-1): sequence of low values closes : sequence sequence of closing values - miss: + miss : int identifier of the missing data + + Raises + ------ + ValueError + if the input sequences don't have the same length """ def _missing(sequence, miss=-1): """Returns the index in *sequence* of the missing data, identified by *miss* + Parameters ---------- - sequence: + sequence : sequence to evaluate - miss: + miss : identifier of the missing data Returns @@ -852,67 +858,23 @@ def _missing(sequence, miss=-1): where_miss: numpy.ndarray indices of the missing data """ - return np.where(np.array(sequence)==miss)[0] + return np.where(np.array(sequence) == miss)[0] same_length = (len(opens) == len(highs)) and (len(opens) == len(lows)) and\ (len(opens) == len(closes)) _missopens = _missing(opens) - same_missing = (_missopens == _missing(highs)).all() and (_missopens ==\ + same_missing = (_missopens == _missing(highs)).all() and (_missopens == _missing(lows)).all() and (_missopens == _missing(closes)).all() if not (same_length and same_missing): msg = """ - *opens*, *highs*, *lows* and *closes* must have the same length. + *opens*, *highs*, *lows* and *closes* must have the same length. NOTE: this code assumes if any value open, high, low, close is missing (*-1*) they all must be missing """ raise ValueError(msg) -def plot_day_summary2(ax, opens, closes, highs, lows, ticksize=4, - colorup='k', colordown='r', - ): - """Represent the time, open, close, high, low, as a vertical line - ranging from low to high. The left tick is the open and the right - tick is the close. - - - This function has been deprecated in 1.4 in favor of - `plot_day_summary2_ochl`, which maintains the original argument - order, or `plot_day_summary2_ohlc`, which uses the - open-high-low-close order. This function will be removed in 1.5 - - - Parameters - ---------- - ax : `Axes` - an Axes instance to plot to - opens : sequence - sequence of opening values - closes : sequence - sequence of closing values - highs : sequence - sequence of high values - lows : sequence - sequence of low values - ticksize : int - size of open and close ticks in points - colorup : color - the color of the lines where close >= open - colordown : color - the color of the lines where close < open - - Returns - ------- - ret : list - a list of lines added to the axes - """ - - warnings.warn(_warn_str.format(fun='plot_day_summary2'), mplDeprecation) - return plot_day_summary2_ohlc(ax, opens, highs, lows, closes, ticksize, - colorup, colordown) - - def plot_day_summary2_ochl(ax, opens, closes, highs, lows, ticksize=4, colorup='k', colordown='r', ): From 96c733e9fe3b6473b8ee35b5cb0db4bc6f10c8e9 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 11:05:59 +0100 Subject: [PATCH 21/36] PEP8 fixed --- lib/matplotlib/figure.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 96d22dcab17b..2bb7b54543ce 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -50,6 +50,7 @@ docstring.interpd.update(projection_names=get_projection_names()) + class AxesStack(Stack): """ Specialization of the Stack to handle all tracking of Axes in a Figure. @@ -948,7 +949,8 @@ def add_subplot(self, *args, **kwargs): a = args[0] if a.get_figure() is not self: - msg = "The Subplot must have been created in the present figure" + msg = "The Subplot must have been created in the present" + msg += " figure" raise ValueError(msg) # make a key for the subplot (which includes the axes object id # in the hash) From 140210fb73866d8fe5d9045c59a796d83e8e7940 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 11:15:45 +0100 Subject: [PATCH 22/36] PEP8 fixed - image.py --- lib/matplotlib/finance.py | 6 +++--- lib/matplotlib/image.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/finance.py b/lib/matplotlib/finance.py index b74f7c37a253..8cb9d45937b7 100644 --- a/lib/matplotlib/finance.py +++ b/lib/matplotlib/finance.py @@ -919,7 +919,7 @@ def plot_day_summary2_ohlc(ax, opens, highs, lows, closes, ticksize=4, """Represent the time, open, high, low, close as a vertical line ranging from low to high. The left tick is the open and the right tick is the close. - *opens*, *highs*, *lows* and *closes* must have the same length. + *opens*, *highs*, *lows* and *closes* must have the same length. NOTE: this code assumes if any value open, high, low, close is missing (*-1*) they all are missing @@ -948,7 +948,7 @@ def plot_day_summary2_ohlc(ax, opens, highs, lows, closes, ticksize=4, a list of lines added to the axes """ - _check_input(opens, highs, lows, closes) + _check_input(opens, highs, lows, closes) rangeSegments = [((i, low), (i, high)) for i, low, high in zip(xrange(len(lows)), lows, highs) if low != -1] @@ -1101,7 +1101,7 @@ def candlestick2_ohlc(ax, opens, highs, lows, closes, width=4, (lineCollection, barCollection) """ - _check_input(opens, highs, lows, closes) + _check_input(opens, highs, lows, closes) delta = width / 2. barVerts = [((i - delta, open), diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index dd6da4883371..a0f12fb946a3 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -520,7 +520,7 @@ def set_filterrad(self, filterrad): ACCEPTS: positive float """ r = float(filterrad) - if r<=0: + if r <= 0: raise ValueError("The filter radius must be a positive number") self._filterrad = r From a2abc7bf929fc7f81d1caff5ac2190950f5954ac Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 11:21:33 +0100 Subject: [PATCH 23/36] PEP 8 fixed - mlab.py --- lib/matplotlib/mlab.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index 83d542261551..75127fe55e83 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -3576,8 +3576,8 @@ def stineman_interp(xi, x, y, yp=None): """ # Cast key variables as float. - x=np.asarray(x, np.float_) - y=np.asarray(y, np.float_) + x = np.asarray(x, np.float_) + y = np.asarray(y, np.float_) if x.shape != y.shape: raise ValueError("'x' and 'y' must be of same shape") @@ -3826,7 +3826,7 @@ def poly_below(xmin, xs, ys): ys = numpy.asarray(ys) Nx = len(xs) Ny = len(ys) - if Nx!=Ny: + if Nx != Ny: raise ValueError("'xs' and 'ys' must have the same length") x = xmin*numpy.ones(2*Nx) y = numpy.ones(2*Nx) From 25cec2290a6f467fbe1a4168657b9b399b818c85 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 11:24:44 +0100 Subject: [PATCH 24/36] PEP 8 fixed - patches.py --- lib/matplotlib/patches.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 70adbf185e98..cd0839d5d5b9 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -3096,11 +3096,9 @@ def ensure_quadratic_bezier(path): segments = list(path.iter_segments()) if (len(segments) != 2) or (segments[0][1] != Path.MOVETO) or\ (segments[1][1] != Path.CURVE3): - msg="'path' it's not a valid quadratice bezier curve" + msg = "'path' it's not a valid quadratice bezier curve" raise ValueError(msg) - - return list(segments[0][0]) + list(segments[1][0]) def transmute(self, path, mutation_size, linewidth): From 06aedf761f0897b197ac4ee12d54739bacf82928 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 11:26:11 +0100 Subject: [PATCH 25/36] PEP8 fixed - path.py --- lib/matplotlib/path.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index 37a6c701f4eb..3a1d27e71dd0 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -147,7 +147,8 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1, msg += "of 'vertices'" raise ValueError(msg) if len(codes) and codes[0] != self.MOVETO: - msg = "The first element of 'code' must be equal to 'MOVETO': {}" + msg = "The first element of 'code' must be equal to 'MOVETO':" + msg += " {}" raise ValueError(msg.format(self.MOVETO)) elif closed: codes = np.empty(len(vertices), dtype=self.code_type) @@ -155,7 +156,6 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1, codes[1:-1] = self.LINETO codes[-1] = self.CLOSEPOLY - self._vertices = vertices self._codes = codes self._interpolation_steps = _interpolation_steps From 77da3b6bde47121214fc544c7d2e1a2bebc9c18f Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 11:29:25 +0100 Subject: [PATCH 26/36] PEP8 fixed - sankey.py --- lib/matplotlib/sankey.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/sankey.py b/lib/matplotlib/sankey.py index f07f41906a98..a8efde669b36 100755 --- a/lib/matplotlib/sankey.py +++ b/lib/matplotlib/sankey.py @@ -151,7 +151,7 @@ def __init__(self, ax=None, scale=1.0, unit='', format='%G', gap=0.25, .. plot:: mpl_examples/api/sankey_demo_basics.py """ # Check the arguments. - if gap <0: + if gap < 0: raise ValueError( "The gap is negative.\nThis isn't allowed because it " "would cause the paths to overlap.") @@ -522,12 +522,12 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='', "The index of the prior diagram is %d, but there are " "only %d other diagrams.\nThe index is zero-based." % (prior, len(self.diagrams))) - if connect[0] >= len(self.diagrams[prior].flows): + if connect[0] >= len(self.diagrams[prior].flows): raise ValueError( "The connection index to the source diagram is %d, but " "that diagram has only %d flows.\nThe index is zero-based." % (connect[0], len(self.diagrams[prior].flows))) - if connect[1] >= n: + if connect[1] >= n: raise ValueError( "The connection index to this diagram is %d, but this diagram" "has only %d flows.\n The index is zero-based." From 95ae2cd715f4869a16d5d489417f115fbe26f0ed Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 11:31:09 +0100 Subject: [PATCH 27/36] PEP8 fixed - spines.py, table.py --- lib/matplotlib/spines.py | 2 +- lib/matplotlib/table.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/spines.py b/lib/matplotlib/spines.py index 3aa5ad32fe73..41cedf8a5960 100644 --- a/lib/matplotlib/spines.py +++ b/lib/matplotlib/spines.py @@ -375,7 +375,7 @@ def set_position(self, position): # special positions pass else: - if len(position) != 2: + if len(position) != 2: raise ValueError("position should be 'center' or 2-tuple") if position[0] not in ['outward', 'axes', 'data']: msg = "position[0] should be in ['outward', 'axes', 'data']" diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index 5e9ef4a8b08f..2cdddc5bd5ef 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -483,7 +483,7 @@ def table(ax, cols = len(cellText[0]) for row in cellText: if len(row) != cols: - msg = "Each row in 'cellText' must have {} columns" + msg = "Each row in 'cellText' must have {} columns" raise ValueError(msg.format(cols)) if cellColours is not None: @@ -491,7 +491,7 @@ def table(ax, raise ValueError("'cellColours' must have {} rows".format(rows)) for row in cellColours: if len(row) != cols: - msg = "Each row in 'cellColours' must have {} columns" + msg = "Each row in 'cellColours' must have {} columns" raise ValueError(msg.format(cols)) else: cellColours = ['w' * cols] * rows From b167c0c681990266e757daa8bdc161b36afa0fea Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 21:21:24 +0100 Subject: [PATCH 28/36] test adapted to code change (AssertionError -> ValueError) --- lib/matplotlib/tests/test_rcparams.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index ec92da9ee246..8265cb77dad2 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -227,8 +227,8 @@ def test_validators(): (['a', 'b'], ['a', 'b']), (('a', 'b'), ['a', 'b']), ((1, 2), ['1', '2'])), - 'fail': ((dict(), AssertionError), - (1, AssertionError),) + 'fail': ((dict(), ValueError), + (1, ValueError),) }, {'validator': validate_nseq_int(2), 'success': ((_, [1, 2]) From f9792a9c6553b6ea95f6872f0baea6ca493a785d Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 21:34:08 +0100 Subject: [PATCH 29/36] fixed according to #3060 comment --- lib/matplotlib/streamplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/streamplot.py b/lib/matplotlib/streamplot.py index 9893cee1cc63..3708afa647db 100644 --- a/lib/matplotlib/streamplot.py +++ b/lib/matplotlib/streamplot.py @@ -112,7 +112,7 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None, arrow_kw['zorder'] = zorder ## Sanity checks. - if (u.shape != grid.shape) and (v.shape != grid.shape): + if (u.shape != grid.shape) or (v.shape != grid.shape): msg = "'u' and 'v' must be of shape 'Grid(x,y)'" raise ValueError(msg) From 850178e4243ba91c49229c61dd82b9aac3e30350 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 21:46:12 +0100 Subject: [PATCH 30/36] Two bugs in assert -> exception transformation fixed --- lib/matplotlib/sankey.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/sankey.py b/lib/matplotlib/sankey.py index a8efde669b36..8d6df68f6cac 100755 --- a/lib/matplotlib/sankey.py +++ b/lib/matplotlib/sankey.py @@ -532,14 +532,14 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='', "The connection index to this diagram is %d, but this diagram" "has only %d flows.\n The index is zero-based." % (connect[1], n)) - if self.diagrams[prior].angles[connect[0]] is not None: + if self.diagrams[prior].angles[connect[0]] is None: raise ValueError( "The connection cannot be made. Check that the magnitude " "of flow %d of diagram %d is greater than or equal to the " "specified tolerance." % (connect[0], prior)) flow_error = (self.diagrams[prior].flows[connect[0]] + flows[connect[1]]) - if abs(flow_error) > self.tolerance: + if abs(flow_error) >= self.tolerance: raise ValueError( "The scaled sum of the connected flows is %f, which is not " "within the tolerance (%f)." % (flow_error, self.tolerance)) From c0ebd4ff8b34da3d42b9ce2c8b5bf9bc8e0af82b Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 21:47:57 +0100 Subject: [PATCH 31/36] Typo fixed --- lib/matplotlib/patches.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index cd0839d5d5b9..e85443b6840c 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -3096,7 +3096,7 @@ def ensure_quadratic_bezier(path): segments = list(path.iter_segments()) if (len(segments) != 2) or (segments[0][1] != Path.MOVETO) or\ (segments[1][1] != Path.CURVE3): - msg = "'path' it's not a valid quadratice bezier curve" + msg = "'path' it's not a valid quadratic bezier curve" raise ValueError(msg) return list(segments[0][0]) + list(segments[1][0]) From 6b6d2de012ddabf108dd039b51d06d9aeeca4160 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 21:50:37 +0100 Subject: [PATCH 32/36] Bug in assert -> exception transformation fixed --- lib/matplotlib/path.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index 3a1d27e71dd0..c26eaf0e0886 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -136,7 +136,7 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1, else: vertices = np.asarray(vertices, np.float_) - if (vertices.ndim != 2) and (vertices.shape[1] != 2): + if (vertices.ndim != 2) or (vertices.shape[1] != 2): msg = "'vertices' must be a 2D list or array with shape Nx2" raise ValueError(msg) From 088542ebc29147dd6cc025619a098695065c0948 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Thu, 5 Mar 2015 21:53:17 +0100 Subject: [PATCH 33/36] Typo fixed --- lib/matplotlib/scale.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/scale.py b/lib/matplotlib/scale.py index 03d49c2d200c..7193d1e7edf6 100644 --- a/lib/matplotlib/scale.py +++ b/lib/matplotlib/scale.py @@ -441,7 +441,7 @@ def __init__(self, axis, **kwargs): linscale = kwargs.pop('linscaley', 1.0) if base <= 1.0: - raise ValueError("'basex/basey' be larger than 1") + raise ValueError("'basex/basey' must be larger than 1") if linthresh <= 0.0: raise ValueError("'linthreshx/linthreshy' must be positive") if linscale <= 0.0: From 41bf6b5c6e783eb8861ba75e8daf3f670d2bbfb6 Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Mon, 16 Mar 2015 10:38:29 +0100 Subject: [PATCH 34/36] Modified according to @tacaswell comments --- lib/matplotlib/backend_bases.py | 4 ++-- lib/matplotlib/blocking_input.py | 4 ++-- lib/matplotlib/contour.py | 8 ++++---- lib/matplotlib/finance.py | 16 +++++++--------- lib/matplotlib/patches.py | 4 ++-- lib/matplotlib/transforms.py | 1 + lib/matplotlib/widgets.py | 2 +- 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index ea8bb9e0bc02..bd2072f1f6f6 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -971,8 +971,8 @@ def set_clip_path(self, path): """ if path is not None and not isinstance(path, transforms.TransformedPath): - msg = "Path should be a matplotlib.transforms.TransformedPath \ - instance." + msg = ("Path should be a matplotlib.transforms.TransformedPath" + "instance.") raise ValueError(msg) self._clippath = path diff --git a/lib/matplotlib/blocking_input.py b/lib/matplotlib/blocking_input.py index 874a2a881ee8..d7035221e8eb 100644 --- a/lib/matplotlib/blocking_input.py +++ b/lib/matplotlib/blocking_input.py @@ -149,7 +149,7 @@ def post_event(self): """ This will be called to process events """ - if len(self.events) <= 0: + if len(self.events) == 0: warnings.warn("No events yet") elif self.events[-1].name == 'key_press_event': self.key_event() @@ -362,7 +362,7 @@ def post_event(self): """ Determines if it is a key event """ - if len(self.events) <= 0: + if len(self.events) == 0: warnings.warn("No events yet") else: self.keyormouse = self.events[-1].name == 'key_press_event' diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index b6db4e45aaac..c98eca91a6e5 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -858,11 +858,11 @@ def __init__(self, ax, *args, **kwargs): self.logscale = False if self.origin not in [None, 'lower', 'upper', 'image']: - raise ValueError("If given, *origin* must be one of ['lower', \ - 'upper', 'image']") + raise ValueError("If given, *origin* must be one of ['lower'," + " 'upper', 'image']") if self.extent is not None and len(self.extent) != 4: - raise ValueError("If give, *extent* must be '[ *None* | \ - (x0,x1,y0,y1) ]'") + raise ValueError("If given, *extent* must be '[ *None* |" + " (x0,x1,y0,y1) ]'") if self.colors is not None and cmap is not None: raise ValueError('Either colors or cmap must be None') if self.origin == 'image': diff --git a/lib/matplotlib/finance.py b/lib/matplotlib/finance.py index 8cb9d45937b7..9dac6cc45dbc 100644 --- a/lib/matplotlib/finance.py +++ b/lib/matplotlib/finance.py @@ -860,18 +860,16 @@ def _missing(sequence, miss=-1): """ return np.where(np.array(sequence) == miss)[0] - same_length = (len(opens) == len(highs)) and (len(opens) == len(lows)) and\ - (len(opens) == len(closes)) + same_length = len(opens) == len(highs) == len(lows) == len(closes) _missopens = _missing(opens) - same_missing = (_missopens == _missing(highs)).all() and (_missopens == - _missing(lows)).all() and (_missopens == _missing(closes)).all() + same_missing = ((_missopens == _missing(highs)).all() and + (_missopens == _missing(lows)).all() and + (_missopens == _missing(closes)).all()) if not (same_length and same_missing): - msg = """ - *opens*, *highs*, *lows* and *closes* must have the same length. - NOTE: this code assumes if any value open, high, low, close is - missing (*-1*) they all must be missing - """ + msg = ("*opens*, *highs*, *lows* and *closes* must have the same" + " length. NOTE: this code assumes if any value open, high," + "low, close is missing (*-1*) they all must be missing.") raise ValueError(msg) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index e85443b6840c..eea54c41fa64 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -3094,8 +3094,8 @@ def ensure_quadratic_bezier(path): its control points if true. """ segments = list(path.iter_segments()) - if (len(segments) != 2) or (segments[0][1] != Path.MOVETO) or\ - (segments[1][1] != Path.CURVE3): + if ((len(segments) != 2) or (segments[0][1] != Path.MOVETO) or + (segments[1][1] != Path.CURVE3)): msg = "'path' it's not a valid quadratic bezier curve" raise ValueError(msg) diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index cf717c5cf464..6966e89ef2e9 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -2465,6 +2465,7 @@ def __init__(self, boxin, boxout, **kwargs): """ if not boxin.is_bbox or not boxout.is_bbox: msg = "'boxin' and 'boxout' must be bbox" + raise ValueError(msg) Affine2DBase.__init__(self, **kwargs) self._boxin = boxin diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 2261114a6c49..2a92d90c00a0 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -1307,7 +1307,7 @@ def __init__(self, ax, onselect, direction, minspan=None, useblit=False, rectprops = dict(facecolor='red', alpha=0.5) if direction not in ['horizontal', 'vertical']: - raise ValueError('Must choose horizontal or vertical for direction') + raise ValueError("direction must be in ['horizontal', 'vertical']") self.direction = direction self.rect = None From 7ea5c1a8677f9582f54aafc1b3293ef3aaf243bf Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Mon, 16 Mar 2015 11:18:49 +0100 Subject: [PATCH 35/36] fixed pep8 Travis failure --- lib/matplotlib/finance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/finance.py b/lib/matplotlib/finance.py index 9dac6cc45dbc..a1150dd49b72 100644 --- a/lib/matplotlib/finance.py +++ b/lib/matplotlib/finance.py @@ -863,7 +863,7 @@ def _missing(sequence, miss=-1): same_length = len(opens) == len(highs) == len(lows) == len(closes) _missopens = _missing(opens) same_missing = ((_missopens == _missing(highs)).all() and - (_missopens == _missing(lows)).all() and + (_missopens == _missing(lows)).all() and (_missopens == _missing(closes)).all()) if not (same_length and same_missing): From d6c3c32ca19bfa974a24cce3567383ee5b905fce Mon Sep 17 00:00:00 2001 From: Francesco Montesano Date: Sun, 22 Mar 2015 09:51:10 +0100 Subject: [PATCH 36/36] python2.6 string formatting. style more uniform --- lib/matplotlib/axes/_base.py | 8 +++---- lib/matplotlib/contour.py | 4 ++-- lib/matplotlib/figure.py | 8 +++---- lib/matplotlib/finance.py | 2 +- lib/matplotlib/path.py | 8 +++---- lib/matplotlib/projections/geo.py | 4 ++-- lib/matplotlib/projections/polar.py | 4 ++-- lib/matplotlib/rcsetup.py | 4 ++-- lib/matplotlib/sankey.py | 2 +- lib/matplotlib/spines.py | 3 ++- lib/matplotlib/table.py | 8 +++---- lib/matplotlib/text.py | 2 +- lib/matplotlib/transforms.py | 36 ++++++++++++++--------------- lib/matplotlib/tri/tripcolor.py | 3 +-- lib/matplotlib/widgets.py | 6 +++-- 15 files changed, 52 insertions(+), 50 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 9b5fa615c8c3..9a97ebb83ca9 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2105,8 +2105,8 @@ def draw_artist(self, a): data (axis ticks, labels, etc are not updated) """ if self._cachedRenderer is None: - msg = '''draw_artist can only be used after an initial draw which - caches the render''' + msg = ('draw_artist can only be used after an initial draw which' + ' caches the render') raise AttributeError(msg) a.draw(self._cachedRenderer) @@ -2117,8 +2117,8 @@ def redraw_in_frame(self): data (axis ticks, labels, etc are not updated) """ if self._cachedRenderer is None: - msg = '''redraw_in_frame can only be used after an initial draw - which caches the render''' + msg = ('redraw_in_frame can only be used after an initial draw' + ' which caches the render') raise AttributeError(msg) self.draw(self._cachedRenderer, inframe=True) diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index c98eca91a6e5..9d6a73f12c8f 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -858,8 +858,8 @@ def __init__(self, ax, *args, **kwargs): self.logscale = False if self.origin not in [None, 'lower', 'upper', 'image']: - raise ValueError("If given, *origin* must be one of ['lower'," - " 'upper', 'image']") + raise ValueError("If given, *origin* must be one of [ 'lower' |" + " 'upper' | 'image']") if self.extent is not None and len(self.extent) != 4: raise ValueError("If given, *extent* must be '[ *None* |" " (x0,x1,y0,y1) ]'") diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 2bb7b54543ce..f14c8c8c64a6 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -949,8 +949,8 @@ def add_subplot(self, *args, **kwargs): a = args[0] if a.get_figure() is not self: - msg = "The Subplot must have been created in the present" - msg += " figure" + msg = ("The Subplot must have been created in the present" + " figure") raise ValueError(msg) # make a key for the subplot (which includes the axes object id # in the hash) @@ -1110,8 +1110,8 @@ def draw_artist(self, a): this is available only after the figure is drawn """ if self._cachedRenderer is None: - msg = '''draw_artist can only be used after an initial draw which \ - caches the render''' + msg = ('draw_artist can only be used after an initial draw which' + ' caches the render') raise AttributeError(msg) a.draw(self._cachedRenderer) diff --git a/lib/matplotlib/finance.py b/lib/matplotlib/finance.py index a1150dd49b72..630a531dc885 100644 --- a/lib/matplotlib/finance.py +++ b/lib/matplotlib/finance.py @@ -869,7 +869,7 @@ def _missing(sequence, miss=-1): if not (same_length and same_missing): msg = ("*opens*, *highs*, *lows* and *closes* must have the same" " length. NOTE: this code assumes if any value open, high," - "low, close is missing (*-1*) they all must be missing.") + " low, close is missing (*-1*) they all must be missing.") raise ValueError(msg) diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index c26eaf0e0886..6f9af4296af5 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -143,12 +143,12 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1, if codes is not None: codes = np.asarray(codes, self.code_type) if (codes.ndim != 1) or len(codes) != len(vertices): - msg = "'codes' must be a 1D list or array with the same length" - msg += "of 'vertices'" + msg = ("'codes' must be a 1D list or array with the same" + " length of 'vertices'") raise ValueError(msg) if len(codes) and codes[0] != self.MOVETO: - msg = "The first element of 'code' must be equal to 'MOVETO':" - msg += " {}" + msg = ("The first element of 'code' must be equal to 'MOVETO':" + " {0}") raise ValueError(msg.format(self.MOVETO)) elif closed: codes = np.empty(len(vertices), dtype=self.code_type) diff --git a/lib/matplotlib/projections/geo.py b/lib/matplotlib/projections/geo.py index de2695f5e4a1..827ed86c2d21 100644 --- a/lib/matplotlib/projections/geo.py +++ b/lib/matplotlib/projections/geo.py @@ -130,7 +130,7 @@ def _get_affine_transform(self): def get_xaxis_transform(self,which='grid'): if which not in ['tick1','tick2','grid']: - msg = "'which' must be on of ['tick1','tick2','grid']" + msg = "'which' must be on of [ 'tick1' | 'tick2' | 'grid' ]" raise ValueError(msg) return self._xaxis_transform @@ -142,7 +142,7 @@ def get_xaxis_text2_transform(self, pad): def get_yaxis_transform(self,which='grid'): if which not in ['tick1','tick2','grid']: - msg = "'which' must be on of ['tick1','tick2','grid']" + msg = "'which' must be one of [ 'tick1' | 'tick2' | 'grid' ]" raise ValueError(msg) return self._yaxis_transform diff --git a/lib/matplotlib/projections/polar.py b/lib/matplotlib/projections/polar.py index f13751eba79b..c78faf7f728a 100644 --- a/lib/matplotlib/projections/polar.py +++ b/lib/matplotlib/projections/polar.py @@ -341,7 +341,7 @@ def _set_lim_and_transforms(self): def get_xaxis_transform(self,which='grid'): if which not in ['tick1','tick2','grid']: - msg = "'which' must be on of ['tick1','tick2','grid']" + msg = "'which' must be one of [ 'tick1' | 'tick2' | 'grid' ]" raise ValueError(msg) return self._xaxis_transform @@ -353,7 +353,7 @@ def get_xaxis_text2_transform(self, pad): def get_yaxis_transform(self,which='grid'): if which not in ['tick1','tick2','grid']: - msg = "'which' must be on of ['tick1','tick2','grid']" + msg = "'which' must be on of [ 'tick1' | 'tick2' | 'grid' ]" raise ValueError(msg) return self._yaxis_transform diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 560f0be72ba1..38e8f705e486 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -275,7 +275,7 @@ def validate_colorlist(s): elif type(s) in (list, tuple): return [validate_color(c) for c in s] else: - msg = "'s' must be of type [string | list | tuple]" + msg = "'s' must be of type [ string | list | tuple ]" raise ValueError(msg) def validate_stringlist(s): @@ -285,7 +285,7 @@ def validate_stringlist(s): elif type(s) in (list, tuple): return [six.text_type(v) for v in s if v] else: - msg = "'s' must be of type [string | list | tuple]" + msg = "'s' must be of type [ string | list | tuple ]" raise ValueError(msg) validate_orientation = ValidateInStrings( diff --git a/lib/matplotlib/sankey.py b/lib/matplotlib/sankey.py index 8d6df68f6cac..c9af96fd9af2 100755 --- a/lib/matplotlib/sankey.py +++ b/lib/matplotlib/sankey.py @@ -574,7 +574,7 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='', if orient != -1: raise ValueError( "The value of orientations[%d] is %d, " - "but it must be -1, 0, or 1." % (i, orient)) + "but it must be [ -1 | 0 | 1 ]." % (i, orient)) if is_input: angles[i] = UP elif not is_input: diff --git a/lib/matplotlib/spines.py b/lib/matplotlib/spines.py index 41cedf8a5960..f73ee15fbc73 100644 --- a/lib/matplotlib/spines.py +++ b/lib/matplotlib/spines.py @@ -378,7 +378,8 @@ def set_position(self, position): if len(position) != 2: raise ValueError("position should be 'center' or 2-tuple") if position[0] not in ['outward', 'axes', 'data']: - msg = "position[0] should be in ['outward', 'axes', 'data']" + msg = ("position[0] should be in [ 'outward' | 'axes' |" + " 'data' ]") raise ValueError(msg) self._position = position self._calc_offset_transform() diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index 2cdddc5bd5ef..c918457d3479 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -483,15 +483,15 @@ def table(ax, cols = len(cellText[0]) for row in cellText: if len(row) != cols: - msg = "Each row in 'cellText' must have {} columns" + msg = "Each row in 'cellText' must have {0} columns" raise ValueError(msg.format(cols)) if cellColours is not None: if len(cellColours) != rows: - raise ValueError("'cellColours' must have {} rows".format(rows)) + raise ValueError("'cellColours' must have {0} rows".format(rows)) for row in cellColours: if len(row) != cols: - msg = "Each row in 'cellColours' must have {} columns" + msg = "Each row in 'cellColours' must have {0} columns" raise ValueError(msg.format(cols)) else: cellColours = ['w' * cols] * rows @@ -512,7 +512,7 @@ def table(ax, if rowLabels is not None: if len(rowLabels) != rows: - raise ValueError("'rowLabels' must be of length {}".format(rows)) + raise ValueError("'rowLabels' must be of length {0}".format(rows)) # If we have column labels, need to shift # the text and colour arrays down 1 row diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index b3e6c81eaa9d..034277fcd094 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -1475,7 +1475,7 @@ def __init__(self, artist, ref_coord, unit="points"): def set_unit(self, unit): if unit not in ["points", "pixels"]: - raise ValueError("'unit' must be one of ['points', 'pixels']") + raise ValueError("'unit' must be one of [ 'points' | 'pixels' ]") self._unit = unit def get_unit(self): diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index 6966e89ef2e9..fcac999115bf 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -1067,8 +1067,8 @@ def __init__(self, bbox, transform, **kwargs): if not bbox.is_bbox: raise ValueError("'bbox' is not a bbox") if not isinstance(transform, Transform): - msg = "'transform' must be an instance of" - msg += " 'matplotlib.transform.Transform'" + msg = ("'transform' must be an instance of" + " 'matplotlib.transform.Transform'") raise ValueError(msg) if transform.input_dims != 2 or transform.output_dims != 2: msg = "The input and output dimensions of 'transform' must be 2" @@ -1526,8 +1526,8 @@ def __init__(self, child): be replaced with :meth:`set`. """ if not isinstance(child, Transform): - msg = "'child' must be an instance of" - msg += " 'matplotlib.transform.Transform'" + msg = ("'child' must be an instance of" + " 'matplotlib.transform.Transform'") raise ValueError(msg) Transform.__init__(self) self.input_dims = child.input_dims @@ -1583,10 +1583,10 @@ def set(self, child): The new child must have the same number of input and output dimensions as the current child. """ - if child.input_dims != self.input_dims or \ - child.output_dims != self.output_dims: - msg = "The new child must have the same number of input and output" - msg += " dimensions as the current child." + if (child.input_dims != self.input_dims or + child.output_dims != self.output_dims): + msg = ("The new child must have the same number of input and" + " output dimensions as the current child.") raise ValueError(msg) self._set(child) @@ -1838,8 +1838,8 @@ def set(self, other): :class:`Affine2DBase` object. """ if not isinstance(other, Affine2DBase): - msg = "'other' must be an instance of" - msg += " 'matplotlib.transform.Affine2DBase'" + msg = ("'other' must be an instance of" + " 'matplotlib.transform.Affine2DBase'") raise ValueError(msg) self._mtx = other.get_matrix() self.invalidate() @@ -2174,8 +2174,8 @@ def __init__(self, x_transform, y_transform, **kwargs): is_separable = x_transform.is_separable and y_transform.is_separable is_correct = is_affine and is_separable if not is_correct: - msg = "Both *x_transform* and *y_transform* must be 2D affine" - msg += " transforms." + msg = ("Both *x_transform* and *y_transform* must be 2D affine" + " transforms.") raise ValueError(msg) Transform.__init__(self, **kwargs) @@ -2254,8 +2254,8 @@ def __init__(self, a, b, **kwargs): transform instance to create. """ if a.output_dims != b.input_dims: - msg = "The output dimension of 'a' must be equal to the input" - msg += " dimensions of 'b'" + msg = ("The output dimension of 'a' must be equal to the input" + " dimensions of 'b'") raise ValueError(msg) self.input_dims = a.input_dims self.output_dims = b.output_dims @@ -2384,8 +2384,8 @@ def __init__(self, a, b, **kwargs): if not a.is_affine or not b.is_affine: raise ValueError("'a' and 'b' must be affine transforms") if a.output_dims != b.input_dims: - msg = "The output dimension of 'a' must be equal to the input" - msg += " dimensions of 'b'" + msg = ("The output dimension of 'a' must be equal to the input" + " dimensions of 'b'") raise ValueError(msg) self.input_dims = a.input_dims self.output_dims = b.output_dims @@ -2644,8 +2644,8 @@ def __init__(self, path, transform): :class:`~matplotlib.path.Path` and :class:`Transform`. """ if not isinstance(transform, Transform): - msg = "'transform' must be an instance of" - msg += " 'matplotlib.transform.Transform'" + msg = ("'transform' must be an instance of" + " 'matplotlib.transform.Transform'") raise ValueError(msg) TransformNode.__init__(self) diff --git a/lib/matplotlib/tri/tripcolor.py b/lib/matplotlib/tri/tripcolor.py index 329b181eca93..7db662ed2257 100644 --- a/lib/matplotlib/tri/tripcolor.py +++ b/lib/matplotlib/tri/tripcolor.py @@ -139,8 +139,7 @@ def tripcolor(ax, *args, **kwargs): collection.set_array(C) if norm is not None: if not isinstance(norm, Normalize): - msg = "'norm' must be an instance of" - msg += " 'Normalize'" + msg = "'norm' must be an instance of 'Normalize'" raise ValueError(msg) collection.set_cmap(cmap) collection.set_norm(norm) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 2a92d90c00a0..6eba3574f7e4 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -1307,7 +1307,8 @@ def __init__(self, ax, onselect, direction, minspan=None, useblit=False, rectprops = dict(facecolor='red', alpha=0.5) if direction not in ['horizontal', 'vertical']: - raise ValueError("direction must be in ['horizontal', 'vertical']") + msg = "direction must be in [ 'horizontal' | 'vertical' ]" + raise ValueError(msg) self.direction = direction self.rect = None @@ -1560,7 +1561,8 @@ def __init__(self, ax, onselect, drawtype='box', self.minspany = minspany if spancoords not in ('data', 'pixels'): - raise ValueError("'spancoords' must be one of ['data', 'pixels']") + msg = "'spancoords' must be one of [ 'data' | 'pixels' ]" + raise ValueError(msg) self.spancoords = spancoords self.drawtype = drawtype