8000 cbook.asSequence incorporated into unpack_labled_data · matplotlib/matplotlib@efc53d5 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit efc53d5

Browse files
committed
cbook.asSequence incorporated into unpack_labled_data
1 parent e3fd1b1 commit efc53d5

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

lib/matplotlib/__init__.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@
119119
import functools
120120
# cbook must import matplotlib only within function
121121
# definitions, so it is safe to import from it here.
122-
from matplotlib.cbook import is_string_like, mplDeprecation, dedent, get_label
122+
from matplotlib.cbook import (is_string_like,
123+
mplDeprecation,
124+
dedent, get_label,
125+
asSequence)
123126
from matplotlib.compat import subprocess
124127
from matplotlib.rcsetup import (defaultParams,
125128
validate_backend,
@@ -1641,12 +1644,15 @@ def test(verbosity=1, coverage=False):
16411644

16421645

16431646
def _replacer(data, key):
1647+
"""Either returns data[key] or passes data back. Also
1648+
converts input data to a sequence as needed.
1649+
"""
16441650
# if key isn't a string don't bother
16451651
if not isinstance(key, six.string_types):
1646-
return key
1652+
return asSequence(key)
16471653
# try to use __getitem__
16481654
try:
1649-
return data[key]
1655+
return asSequence(data[key])
16501656
# key does not exist, silently fall back to key
16511657
except KeyError:
16521658
return key
@@ -1699,6 +1705,8 @@ def foo(ax, *args, **kwargs)
16991705
NOTE: callables should only be used when the names and order of *args
17001706
can only be determined at runtime. Please use list of names
17011707
when the order and names of *args is clear before runtime!
1708+
1709+
.. note:: decorator also converts MappingView input data to list.
17021710
"""
17031711
if replace_names is not None:
17041712
replace_names = set(replace_names)
@@ -1839,7 +1847,10 @@ def inner(ax, *args, **kwargs):
18391847
label = None
18401848

18411849
data = kwargs.pop('data', None)
1842-
if data is not None:
1850+
1851+
if data is None: # data validation
1852+
args = tuple(asSequence(a) for a in args)
1853+
else:
18431854
if arg_names_at_runtime:
18441855
# update the information about replace names and
18451856
# label position

lib/matplotlib/cbook.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import datetime
1818
import errno
19-
from functools import reduce
19+
import functools
2020
import glob
2121
import gzip
2222
import io
@@ -1758,7 +1758,7 @@ def delete_masked_points(*args):
17581758
except: # Fixme: put in tuple of possible exceptions?
17591759
pass
17601760
if len(masks):
1761-
mask = reduce(np.logical_and, masks)
1761+
mask = functools.reduce(np.logical_and, masks)
17621762
igood = mask.nonzero()[0]
17631763
if len(igood) < nrecs:
17641764
for i, x in enumerate(margs):
@@ -2445,6 +2445,14 @@ def safe_first_element(obj):
24452445
return next(iter(obj))
24462446

24472447

2448+
def asSequence(data):
2449+
"""Converts dictview object to list
2450+
"""
2451+
if six.PY3 and isinstance(data, collections.abc.MappingView):
2452+
return list(data)
2453+
return data
2454+
2455+
24482456
def normalize_kwargs(kw, alias_mapping=None, required=(), forbidden=(),
24492457
allowed=None):
24502458
"""Helper function to normalize kwarg inputs

lib/matplotlib/tests/test_cbook.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,18 @@ def dummy(self):
332332
pass
333333

334334

335+
def test_asSequence():
336+
d = {'a': 1, 'b': 2, 'c': 3}
337+
k = ['a', 'b', 'c']
338+
v = [1, 2, 3]
339+
i = [('a', 1), ('b', 2), ('c', 3)]
340+
assert k == sorted(cbook.asSequence(d.keys()))
341+
assert v == sorted(cbook.asSequence(d.values()))
342+
assert i == sorted(cbook.asSequence(d.items()))
343+
assert i == cbook.asSequence(i)
344+
assert k == cbook.asSequence(k)
345+
346+
335347
def _kwarg_norm_helper(inp, expected, kwargs_to_norm, warn_count=0):
336348

337349
with warnings.catch_warnings(record=True) as w:

0 commit comments

Comments
 (0)
0