diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 6f69f8f46165..e5898efe7f6b 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -119,7 +119,10 @@ import functools # cbook must import matplotlib only within function # definitions, so it is safe to import from it here. -from matplotlib.cbook import is_string_like, mplDeprecation, dedent, get_label +from matplotlib.cbook import (is_string_like, + mplDeprecation, + dedent, get_label, + sanitize_sequence) from matplotlib.compat import subprocess from matplotlib.rcsetup import (defaultParams, validate_backend, @@ -1541,7 +1544,7 @@ def _jupyter_nbextension_paths(): 'matplotlib.tests.test_units', 'matplotlib.tests.test_widgets', 'matplotlib.tests.test_cycles', - 'matplotlib.tests.test_labeled_data_unpacking', + 'matplotlib.tests.test_preprocess_data', 'matplotlib.sphinxext.tests.test_tinypages', 'mpl_toolkits.tests.test_mplot3d', 'mpl_toolkits.tests.test_axes_grid1', @@ -1649,12 +1652,15 @@ def test(verbosity=1, coverage=False): def _replacer(data, key): + """Either returns data[key] or passes data back. Also + converts input data to a sequence as needed. + """ # if key isn't a string don't bother if not isinstance(key, six.string_types): - return key + return (key) # try to use __getitem__ try: - return data[key] + return sanitize_sequence(data[key]) # key does not exist, silently fall back to key except KeyError: return key @@ -1673,7 +1679,7 @@ def _replacer(data, key): """ -def unpack_labeled_data(replace_names=None, replace_all_args=False, +def _preprocess_data(replace_names=None, replace_all_args=False, label_namer=None, positional_parameter_names=None): """ A decorator to add a 'data' kwarg to any a function. The signature @@ -1707,6 +1713,8 @@ def foo(ax, *args, **kwargs) NOTE: callables should only be used when the names and order of *args can only be determined at runtime. Please use list of names when the order and names of *args is clear before runtime! + + .. note:: decorator also converts MappingView input data to list. """ if replace_names is not None: replace_names = set(replace_names) @@ -1847,7 +1855,10 @@ def inner(ax, *args, **kwargs): label = None data = kwargs.pop('data', None) - if data is not None: + + if data is None: # data validation + args = tuple(sanitize_sequence(a) for a in args) + else: if arg_names_at_runtime: # update the information about replace names and # label position diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 405e7c45b854..16d4e89978bd 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -12,7 +12,7 @@ from numpy import ma import matplotlib -from matplotlib import unpack_labeled_data +from matplotlib import _preprocess_data import matplotlib.cbook as cbook from matplotlib.cbook import (mplDeprecation, STEP_LOOKUP_MAP, @@ -906,7 +906,7 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs): self.autoscale_view(scaley=False) return p - @unpack_labeled_data(replace_names=['y', 'xmin', 'xmax'], label_namer="y") + @_preprocess_data(replace_names=['y', 'xmin', 'xmax'], label_namer="y") @docstring.dedent def hlines(self, y, xmin, xmax, colors='k', linestyles='solid', label='', **kwargs): @@ -985,7 +985,7 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid', return coll - @unpack_labeled_data(replace_names=["x", "ymin", "ymax", "colors"], + @_preprocess_data(replace_names=["x", "ymin", "ymax", "colors"], label_namer="x") @docstring.dedent_interpd def vlines(self, x, ymin, ymax, colors='k', linestyles='solid', @@ -1067,7 +1067,7 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid', return coll - @unpack_labeled_data(replace_names=["positions", "lineoffsets", + @_preprocess_data(replace_names=["positions", "lineoffsets", "linelengths", "linewidths", "colors", "linestyles"], label_namer=None) @@ -1254,7 +1254,7 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1, # ### Basic plotting # The label_naming happens in `matplotlib.axes._base._plot_args` - @unpack_labeled_data(replace_names=["x", "y"], + @_preprocess_data(replace_names=["x", "y"], positional_parameter_names=_plot_args_replacer, label_namer=None) @docstring.dedent_interpd @@ -1403,7 +1403,7 @@ def plot(self, *args, **kwargs): self.autoscale_view(scalex=scalex, scaley=scaley) return lines - @unpack_labeled_data(replace_names=["x", "y"], label_namer="y") + @_preprocess_data(replace_names=["x", "y"], label_namer="y") @docstring.dedent_interpd def plot_date(self, x, y, fmt='o', tz=None, xdate=True, ydate=False, **kwargs): @@ -1477,7 +1477,7 @@ def plot_date(self, x, y, fmt='o', tz=None, xdate=True, ydate=False, return ret - # @unpack_labeled_data() # let 'plot' do the unpacking.. + # @_preprocess_data() # let 'plot' do the unpacking.. @docstring.dedent_interpd def loglog(self, *args, **kwargs): """ @@ -1539,7 +1539,7 @@ def loglog(self, *args, **kwargs): return l - # @unpack_labeled_data() # let 'plot' do the unpacking.. + # @_preprocess_data() # let 'plot' do the unpacking.. @docstring.dedent_interpd def semilogx(self, *args, **kwargs): """ @@ -1592,7 +1592,7 @@ def semilogx(self, *args, **kwargs): self._hold = b # restore the hold return l - # @unpack_labeled_data() # let 'plot' do the unpacking.. + # @_preprocess_data() # let 'plot' do the unpacking.. @docstring.dedent_interpd def semilogy(self, *args, **kwargs): """ @@ -1645,7 +1645,7 @@ def semilogy(self, *args, **kwargs): return l - @unpack_labeled_data(replace_names=["x"], label_namer="x") + @_preprocess_data(replace_names=["x"], label_namer="x") @docstring.dedent_interpd def acorr(self, x, **kwargs): """ @@ -1707,7 +1707,7 @@ def acorr(self, x, **kwargs): """ return self.xcorr(x, x, **kwargs) - @unpack_labeled_data(replace_names=["x", "y"], label_namer="y") + @_preprocess_data(replace_names=["x", "y"], label_namer="y") @docstring.dedent_interpd def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none, usevlines=True, maxlags=10, **kwargs): @@ -1797,7 +1797,7 @@ def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none, #### Specialized plotting - @unpack_labeled_data(replace_names=["x", "y"], label_namer="y") + @_preprocess_data(replace_names=["x", "y"], label_namer="y") def step(self, x, y, *args, **kwargs): """ Make a step plot. @@ -1835,7 +1835,7 @@ def step(self, x, y, *args, **kwargs): return self.plot(x, y, *args, **kwargs) - @unpack_labeled_data(replace_names=["left", "height", "width", "bottom", + @_preprocess_data(replace_names=["left", "height", "width", "bottom", "color", "edgecolor", "linewidth", "tick_label", "xerr", "yerr", "ecolor"], @@ -2270,7 +2270,7 @@ def barh(self, bottom, width, height=0.8, left=None, **kwargs): bottom=bottom, orientation='horizontal', **kwargs) return patches - @unpack_labeled_data(label_namer=None) + @_preprocess_data(label_namer=None) @docstring.dedent_interpd def broken_barh(self, xranges, yrange, **kwargs): """ @@ -2331,7 +2331,7 @@ def broken_barh(self, xranges, yrange, **kwargs): return col - @unpack_labeled_data(replace_all_args=True, label_namer=None) + @_preprocess_data(replace_all_args=True, label_namer=None) def stem(self, *args, **kwargs): """ Create a stem plot. @@ -2456,7 +2456,7 @@ def stem(self, *args, **kwargs): return stem_container - @unpack_labeled_data(replace_names=['x', 'explode', 'labels', 'colors'], + @_preprocess_data(replace_names=['x', 'explode', 'labels', 'colors'], label_namer=None) def pie(self, x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, @@ -2683,7 +2683,7 @@ def get_next_color(): else: return slices, texts, autotexts - @unpack_labeled_data(replace_names=["x", "y", "xerr", "yerr"], + @_preprocess_data(replace_names=["x", "y", "xerr", "yerr"], label_namer="y") @docstring.dedent_interpd def errorbar(self, x, y, yerr=None, xerr=None, @@ -3077,7 +3077,7 @@ def extract_err(err, data): return errorbar_container # (l0, caplines, barcols) - @unpack_labeled_data(label_namer=None) + @_preprocess_data(label_namer=None) def boxplot(self, x, notch=None, sym=None, vert=None, whis=None, positions=None, widths=None, patch_artist=None, bootstrap=None, usermedians=None, conf_intervals=None, @@ -3799,7 +3799,7 @@ def dopatch(xs, ys, **kwargs): return dict(whiskers=whiskers, caps=caps, boxes=boxes, medians=medians, fliers=fliers, means=means) - @unpack_labeled_data(replace_names=["x", "y", "s", "linewidths", + @_preprocess_data(replace_names=["x", "y", "s", "linewidths", "edgecolors", "c", 'facecolor', 'facecolors', 'color'], label_namer="y") @@ -4049,7 +4049,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None, return collection - @unpack_labeled_data(replace_names=["x", "y"], label_namer="y") + @_preprocess_data(replace_names=["x", "y"], label_namer="y") @docstring.dedent_interpd def hexbin(self, x, y, C=None, gridsize=100, bins=None, xscale='linear', yscale='linear', extent=None, @@ -4553,7 +4553,7 @@ def quiverkey(self, *args, **kw): quiverkey.__doc__ = mquiver.QuiverKey.quiverkey_doc # args can by a combination if X, Y, U, V, C and all should be replaced - @unpack_labeled_data(replace_all_args=True, label_namer=None) + @_preprocess_data(replace_all_args=True, label_namer=None) def quiver(self, *args, **kw): if not self._hold: self.cla() @@ -4565,12 +4565,12 @@ def quiver(self, *args, **kw): quiver.__doc__ = mquiver.Quiver.quiver_doc # args can by either Y or y1,y2,... and all should be replaced - @unpack_labeled_data(replace_all_args=True, label_namer=None) + @_preprocess_data(replace_all_args=True, label_namer=None) def stackplot(self, x, *args, **kwargs): return mstack.stackplot(self, x, *args, **kwargs) stackplot.__doc__ = mstack.stackplot.__doc__ - @unpack_labeled_data(replace_names=["x", "y", "u", "v", "start_points"], + @_preprocess_data(replace_names=["x", "y", "u", "v", "start_points"], label_namer=None) def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None, cmap=None, norm=None, arrowsize=1, arrowstyle='-|>', @@ -4593,7 +4593,7 @@ def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None, streamplot.__doc__ = mstream.streamplot.__doc__ # args can be some combination of X, Y, U, V, C and all should be replaced - @unpack_labeled_data(replace_all_args=True, label_namer=None) + @_preprocess_data(replace_all_args=True, label_namer=None) @docstring.dedent_interpd def barbs(self, *args, **kw): """ @@ -4610,7 +4610,7 @@ def barbs(self, *args, **kw): self.autoscale_view() return b - @unpack_labeled_data(replace_names=["x", "y"], label_namer=None, + @_preprocess_data(replace_names=["x", "y"], label_namer=None, positional_parameter_names=["x", "y", "c"]) @docstring.dedent_interpd def fill(self, *args, **kwargs): @@ -4665,7 +4665,7 @@ def fill(self, *args, **kwargs): self.autoscale_view() return patches - @unpack_labeled_data(replace_names=["x", "y1", "y2", "where"], + @_preprocess_data(replace_names=["x", "y1", "y2", "where"], label_namer=None) @docstring.dedent_interpd def fill_between(self, x, y1, y2=0, where=None, interpolate=False, @@ -4820,7 +4820,7 @@ def get_interp_point(ind): self.autoscale_view() return collection - @unpack_labeled_data(replace_names=["y", "x1", "x2", "where"], + @_preprocess_data(replace_names=["y", "x1", "x2", "where"], label_namer=None) @docstring.dedent_interpd def fill_betweenx(self, y, x1, x2=0, where=None, @@ -4946,7 +4946,7 @@ def fill_betweenx(self, y, x1, x2=0, where=None, return collection #### plotting z(x,y): imshow, pcolor and relatives, contour - @unpack_labeled_data(label_namer=None) + @_preprocess_data(label_namer=None) @docstring.dedent_interpd def imshow(self, X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, @@ -5156,7 +5156,7 @@ def _pcolorargs(funcname, *args, **kw): C = cbook.safe_masked_invalid(C) return X, Y, C - @unpack_labeled_data(label_namer=None) + @_preprocess_data(label_namer=None) @docstring.dedent_interpd def pcolor(self, *args, **kwargs): """ @@ -5433,7 +5433,7 @@ def pcolor(self, *args, **kwargs): self.autoscale_view() return collection - @unpack_labeled_data(label_namer=None) + @_preprocess_data(label_namer=None) @docstring.dedent_interpd def pcolormesh(self, *args, **kwargs): """ @@ -5583,7 +5583,7 @@ def pcolormesh(self, *args, **kwargs): self.autoscale_view() return collection - @unpack_labeled_data(label_namer=None) + @_preprocess_data(label_namer=None) @docstring.dedent_interpd def pcolorfast(self, *args, **kwargs): """ @@ -5772,7 +5772,7 @@ def pcolorfast(self, *args, **kwargs): self.autoscale_view(tight=True) return ret - @unpack_labeled_data() + @_preprocess_data() def contour(self, *args, **kwargs): if not self._hold: self.cla() @@ -5782,7 +5782,7 @@ def contour(self, *args, **kwargs): return contours contour.__doc__ = mcontour.QuadContourSet.contour_doc - @unpack_labeled_data() + @_preprocess_data() def contourf(self, *args, **kwargs): if not self._hold: self.cla() @@ -5825,7 +5825,7 @@ def table(self, **kwargs): #### Data analysis - @unpack_labeled_data(replace_names=["x", 'weights'], label_namer="x") + @_preprocess_data(replace_names=["x", 'weights'], label_namer="x") @docstring.dedent_interpd def hist(self, x, bins=None, range=None, normed=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', @@ -6391,7 +6391,7 @@ def _normalize_input(inp, ename='input'): else: return n, bins, cbook.silent_list('Lists of Patches', patches) - @unpack_labeled_data(replace_names=["x", "y", "weights"], label_namer=None) + @_preprocess_data(replace_names=["x", "y", "weights"], label_namer=None) @docstring.dedent_interpd def hist2d(self, x, y, bins=10, range=None, normed=False, weights=None, cmin=None, cmax=None, **kwargs): @@ -6485,7 +6485,7 @@ def hist2d(self, x, y, bins=10, range=None, normed=False, weights=None, return h, xedges, yedges, pc - @unpack_labeled_data(replace_names=["x"], label_namer=None) + @_preprocess_data(replace_names=["x"], label_namer=None) @docstring.dedent_interpd def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, window=None, noverlap=None, pad_to=None, @@ -6610,7 +6610,7 @@ def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, else: return pxx, freqs, line - @unpack_labeled_data(replace_names=["x", "y"], label_namer="y") + @_preprocess_data(replace_names=["x", "y"], label_namer="y") @docstring.dedent_interpd def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None, window=None, noverlap=None, pad_to=None, @@ -6722,7 +6722,7 @@ def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None, else: return pxy, freqs, line - @unpack_labeled_data(replace_names=["x"], label_namer=None) + @_preprocess_data(replace_names=["x"], label_namer=None) @docstring.dedent_interpd def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None, pad_to=None, sides=None, scale=None, @@ -6822,7 +6822,7 @@ def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None, return spec, freqs, lines[0] - @unpack_labeled_data(replace_names=["x"], label_namer=None) + @_preprocess_data(replace_names=["x"], label_namer=None) @docstring.dedent_interpd def angle_spectrum(self, x, Fs=None, Fc=None, window=None, pad_to=None, sides=None, **kwargs): @@ -6900,7 +6900,7 @@ def angle_spectrum(self, x, Fs=None, Fc=None, window=None, return spec, freqs, lines[0] - @unpack_labeled_data(replace_names=["x"], label_namer=None) + @_preprocess_data(replace_names=["x"], label_namer=None) @docstring.dedent_interpd def phase_spectrum(self, x, Fs=None, Fc=None, window=None, pad_to=None, sides=None, **kwargs): @@ -6978,7 +6978,7 @@ def phase_spectrum(self, x, Fs=None, Fc=None, window=None, return spec, freqs, lines[0] - @unpack_labeled_data(replace_names=["x", "y"], label_namer=None) + @_preprocess_data(replace_names=["x", "y"], label_namer=None) @docstring.dedent_interpd def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, window=mlab.window_hanning, noverlap=0, pad_to=None, @@ -7046,7 +7046,7 @@ def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, return cxy, freqs - @unpack_labeled_data(replace_names=["x"], label_namer=None) + @_preprocess_data(replace_names=["x"], label_namer=None) @docstring.dedent_interpd def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, window=None, noverlap=None, @@ -7364,7 +7364,7 @@ def matshow(self, Z, **kwargs): integer=True)) return im - @unpack_labeled_data(replace_names=["dataset"], label_namer=None) + @_preprocess_data(replace_names=["dataset"], label_namer=None) def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=False, showextrema=True, showmedians=False, points=100, bw_method=None): diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 0216ccb9c501..5556a65d4c09 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -16,7 +16,7 @@ import datetime import errno -from functools import reduce +import functools import glob import gzip import io @@ -1758,7 +1758,7 @@ def delete_masked_points(*args): except: # Fixme: put in tuple of possible exceptions? pass if len(masks): - mask = reduce(np.logical_and, masks) + mask = functools.reduce(np.logical_and, masks) igood = mask.nonzero()[0] if len(igood) < nrecs: for i, x in enumerate(margs): @@ -2445,6 +2445,14 @@ def safe_first_element(obj): return next(iter(obj)) +def sanitize_sequence(data): + """Converts dictview object to list + """ + if six.PY3 and isinstance(data, collections.abc.MappingView): + return list(data) + return data + + def normalize_kwargs(kw, alias_mapping=None, required=(), forbidden=(), allowed=None): """Helper function to normalize kwarg inputs diff --git a/lib/matplotlib/tests/test_cbook.py b/lib/matplotlib/tests/test_cbook.py index 093220ab659e..5077baaa4769 100644 --- a/lib/matplotlib/tests/test_cbook.py +++ b/lib/matplotlib/tests/test_cbook.py @@ -332,6 +332,18 @@ def dummy(self): pass +def test_sanitize_sequence(): + d = {'a': 1, 'b': 2, 'c': 3} + k = ['a', 'b', 'c'] + v = [1, 2, 3] + i = [('a', 1), ('b', 2), ('c', 3)] + assert k == sorted(cbook.sanitize_sequence(d.keys())) + assert v == sorted(cbook.sanitize_sequence(d.values())) + assert i == sorted(cbook.sanitize_sequence(d.items())) + assert i == cbook.sanitize_sequence(i) + assert k == cbook.sanitize_sequence(k) + + def _kwarg_norm_helper(inp, expected, kwargs_to_norm, warn_count=0): with warnings.catch_warnings(record=True) as w: diff --git a/lib/matplotlib/tests/test_labeled_data_unpacking.py b/lib/matplotlib/tests/test_preprocess_data.py similarity index 89% rename from lib/matplotlib/tests/test_labeled_data_unpacking.py rename to lib/matplotlib/tests/test_preprocess_data.py index d07d481b1f57..6231e7c7c342 100644 --- a/lib/matplotlib/tests/test_labeled_data_unpacking.py +++ b/lib/matplotlib/tests/test_preprocess_data.py @@ -14,7 +14,7 @@ from ..testing import assert_produces_warning -from .. import unpack_labeled_data +from .. import _preprocess_data # Notes on testing the plotting functions itself @@ -24,13 +24,13 @@ # these two get used in multiple tests, so define them here -@unpack_labeled_data(replace_names=["x", "y"], label_namer="y") +@_preprocess_data(replace_names=["x", "y"], label_namer="y") def plot_func(ax, x, y, ls="x", label=None, w="xyz"): return ("x: %s, y: %s, ls: %s, w: %s, label: %s" % ( list(x), list(y), ls, w, label)) -@unpack_labeled_data(replace_names=["x", "y"], label_namer="y", +@_preprocess_data(replace_names=["x", "y"], label_namer="y", positional_parameter_names=["x", "y", "ls", "label", "w"]) def plot_func_varags(ax, *args, **kwargs): all_args = [None, None, "x", None, "xyz"] @@ -59,48 +59,48 @@ def func_kwargs(ax, x, y, **kwargs): pass def func_no_ax_args(*args, **kwargs): pass # this is ok - unpack_labeled_data(replace_names=["x", "y"])(func) - unpack_labeled_data(replace_names=["x", "y"])(func_kwargs) + _preprocess_data(replace_names=["x", "y"])(func) + _preprocess_data(replace_names=["x", "y"])(func_kwargs) # this has "enough" information to do all the replaces - unpack_labeled_data(replace_names=["x", "y"])(func_args) + _preprocess_data(replace_names=["x", "y"])(func_args) # no positional_parameter_names but needed due to replaces def f(): # z is unknown - unpack_labeled_data(replace_names=["x", "y", "z"])(func_args) + _preprocess_data(replace_names=["x", "y", "z"])(func_args) assert_raises(AssertionError, f) def f(): - unpack_labeled_data(replace_names=["x", "y"])(func_no_ax_args) + _preprocess_data(replace_names=["x", "y"])(func_no_ax_args) assert_raises(AssertionError, f) # no replacements at all -> all ok... - unpack_labeled_data(replace_names=[], label_namer=None)(func) - unpack_labeled_data(replace_names=[], label_namer=None)(func_args) - unpack_labeled_data(replace_names=[], label_namer=None)(func_kwargs) - unpack_labeled_data(replace_names=[], label_namer=None)(func_no_ax_args) + _preprocess_data(replace_names=[], label_namer=None)(func) + _preprocess_data(replace_names=[], label_namer=None)(func_args) + _preprocess_data(replace_names=[], label_namer=None)(func_kwargs) + _preprocess_data(replace_names=[], label_namer=None)(func_no_ax_args) # label namer is unknown def f(): - unpack_labeled_data(label_namer="z")(func) + _preprocess_data(label_namer="z")(func) assert_raises(AssertionError, f) def f(): - unpack_labeled_data(label_namer="z")(func_args) + _preprocess_data(label_namer="z")(func_args) assert_raises(AssertionError, f) # but "ok-ish", if func has kwargs -> will show up at runtime :-( - unpack_labeled_data(label_namer="z")(func_kwargs) - unpack_labeled_data(label_namer="z")(func_no_ax_args) + _preprocess_data(label_namer="z")(func_kwargs) + _preprocess_data(label_namer="z")(func_no_ax_args) def test_label_problems_at_runtime(): """Tests for behaviour which would actually be nice to get rid of.""" - @unpack_labeled_data(label_namer="z") + @_preprocess_data(label_namer="z") def func(*args, **kwargs): pass @@ -117,7 +117,7 @@ def f(): def real_func(x, y): pass - @unpack_labeled_data(label_namer="x") + @_preprocess_data(label_namer="x") def func(*args, **kwargs): real_func(**kwargs) @@ -209,7 +209,7 @@ def test_function_call_replace_all(): """Test without a "replace_names" argument, all vars should be replaced""" data = {"a": [1, 2], "b": [8, 9], "x": "xyz"} - @unpack_labeled_data(label_namer="y") + @_preprocess_data(label_namer="y") def func_replace_all(ax, x, y, ls="x", label=None, w="NOT"): return "x: %s, y: %s, ls: %s, w: %s, label: %s" % ( list(x), list(y), ls, w, label) @@ -230,7 +230,7 @@ def func_replace_all(ax, x, y, ls="x", label=None, w="NOT"): func_replace_all(None, x="a", y="b", w="x", label="text", data=data), "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text") - @unpack_labeled_data(label_namer="y") + @_preprocess_data(label_namer="y") def func_varags_replace_all(ax, *args, **kwargs): all_args = [None, None, "x", None, "xyz"] for i, v in enumerate(args): @@ -270,7 +270,7 @@ def func_varags_replace_all(ax, *args, **kwargs): def test_no_label_replacements(): """Test with "label_namer=None" -> no label replacement at all""" - @unpack_labeled_data(replace_names=["x", "y"], label_namer=None) + @_preprocess_data(replace_names=["x", "y"], label_namer=None) def func_no_label(ax, x, y, ls="x", label=None, w="xyz"): return "x: %s, y: %s, ls: %s, w: %s, label: %s" % ( list(x), list(y), ls, w, label) @@ -287,7 +287,7 @@ def func_no_label(ax, x, y, ls="x", label=None, w="xyz"): def test_more_args_than_pos_parameter(): - @unpack_labeled_data(replace_names=["x", "y"], label_namer="y") + @_preprocess_data(replace_names=["x", "y"], label_namer="y") def func(ax, x, y, z=1): pass @@ -314,7 +314,7 @@ def funcy(ax, *args, **kwargs): return "x: %s, y: %s, ls: %s, w: %s, label: %s" % ( list(x), list(y), ls, w, label) - func = unpack_labeled_data(replace_all_args=True, replace_names=["w"], + func = _preprocess_data(replace_all_args=True, replace_names=["w"], label_namer="y")(funcy) assert_equal(func(None, "a", "b", w="x", label="", data=data), @@ -322,7 +322,7 @@ def funcy(ax, *args, **kwargs): assert_equal(func(None, "a", "b", w="x", label="text", data=data), "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text") - func2 = unpack_labeled_data(replace_all_args=True, replace_names=["w"], + func2 = _preprocess_data(replace_all_args=True, replace_names=["w"], label_namer="y", positional_parameter_names=["x", "y", "ls", "label", "w"])( @@ -337,7 +337,7 @@ def funcy(ax, *args, **kwargs): def test_docstring_addition(): - @unpack_labeled_data() + @_preprocess_data() def funcy(ax, *args, **kwargs): """Funcy does nothing""" pass @@ -348,7 +348,7 @@ def funcy(ax, *args, **kwargs): assert_not_regex(funcy.__doc__, r".*All arguments with the following names: .*") - @unpack_labeled_data(replace_all_args=True, replace_names=[]) + @_preprocess_data(replace_all_args=True, replace_names=[]) def funcy(ax, x, y, z, bar=None): """Funcy does nothing""" pass @@ -359,7 +359,7 @@ def funcy(ax, x, y, z, bar=None): assert_not_regex(funcy.__doc__, r".*All arguments with the following names: .*") - @unpack_labeled_data(replace_all_args=True, replace_names=["bar"]) + @_preprocess_data(replace_all_args=True, replace_names=["bar"]) def funcy(ax, x, y, z, bar=None): """Funcy does nothing""" pass @@ -370,7 +370,7 @@ def funcy(ax, x, y, z, bar=None): assert_not_regex(funcy.__doc__, r".*All positional and all keyword arguments\.") - @unpack_labeled_data(replace_names=["x", "bar"]) + @_preprocess_data(replace_names=["x", "bar"]) def funcy(ax, x, y, z, bar=None): """Funcy does nothing""" pass @@ -389,7 +389,7 @@ def test_positional_parameter_names_as_function(): # Also test the _plot_arg_replacer for plot... from matplotlib.axes._axes import _plot_args_replacer - @unpack_labeled_data(replace_names=["x", "y"], + @_preprocess_data(replace_names=["x", "y"], positional_parameter_names=_plot_args_replacer) def funcy(ax, *args, **kwargs): return "{args} | {kwargs}".format(args=args, kwargs=kwargs)