Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 734c09f

Browse files
authored
Merge pull request #8011 from anntzer/is_string_like
MNT/API: Deprecate is_string_like, is_sequence_of_strings
2 parents b58ccb9 + 7b05193 commit 734c09f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+167
-198
lines changed
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
`cbook.is_numlike` and `cbook.is_string_like` only perform an instance check
2-
````````````````````````````````````````````````````````````````````````````
1+
`cbook.is_numlike` only performs an instance check, `cbook.is_string_like` is deprecated
2+
````````````````````````````````````````````````````````````````````````````````````````
33

4-
`cbook.is_numlike` and `cbook.is_string_like` now only check that
5-
their argument is an instance of ``(numbers.Number, np.Number)`` and
6-
``(six.string_types, np.str_, np.unicode_)`` respectively. In particular, this
7-
means that arrays are now never num-like or string-like regardless of their
8-
dtype.
4+
`cbook.is_numlike` now only checks that its argument is an instance of
5+
``(numbers.Number, np.Number)``. In particular, this means that arrays are now
6+
not num-like.
7+
8+
`cbook.is_string_like` and `cbook.is_sequence_of_strings` have been
9+
deprecated. Use ``isinstance(obj, six.string_types)`` and ``iterable(obj) and
10+
all(isinstance(o, six.string_types) for o in obj)`` instead.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
`Collection` offsets are no longer implicitly flattened
2+
-------------------------------------------------------
3+
4+
`Collection` (and thus `scatter` -- both 2D and 3D) no longer implicitly
5+
flattens its offsets. As a consequence, `scatter`'s x and y arguments can no
6+
longer be 2+-dimensional arrays.

examples/misc/rc_traits.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import os
1111
import re
1212
import traits.api as traits
13-
from matplotlib.cbook import is_string_like
1413
from matplotlib.artist import Artist
1514

1615
doprint = True
@@ -75,7 +74,7 @@ def tuple_to_rgba(ob, name, val):
7574
def hex_to_rgba(ob, name, val):
7675
rgx = re.compile('^#[0-9A-Fa-f]{6}$')
7776

78-
if not is_string_like(val):
77+
if not isinstance(val, six.string_types):
7978
raise TypeError
8079
if rgx.match(val) is None:
8180
raise ValueError

examples/mplot3d/2dcollections3d.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
# Fixing random state for reproducibility
2626
np.random.seed(19680801)
2727

28-
x = np.random.sample(20*len(colors))
29-
y = np.random.sample(20*len(colors))
28+
x = np.random.sample(20 * len(colors))
29+
y = np.random.sample(20 * len(colors))
3030
c_list = []
3131
for c in colors:
32-
c_list.append([c]*20)
32+
c_list.extend([c] * 20)
3333
# By using zdir='y', the y value of these points is fixed to the zs value 0
3434
# and the (x,y) points are plotted on the x and z axes.
3535
ax.scatter(x, y, zs=0, zdir='y', c=c_list, label='points in (x,z)')

lib/matplotlib/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,10 @@
121121
# cbook must import matplotlib only within function
122122
# definitions, so it is safe to import from it here.
123123
from . import cbook
124-
from matplotlib.cbook import (is_string_like,
125-
mplDeprecation,
126-
dedent, get_label,
127-
sanitize_sequence)
124+
from matplotlib.cbook import (
125+
mplDeprecation, dedent, get_label, sanitize_sequence)
128126
from matplotlib.compat import subprocess
129-
from matplotlib.rcsetup import (defaultParams,
130-
validate_backend,
131-
cycler)
127+
from matplotlib.rcsetup import defaultParams, validate_backend, cycler
132128

133129
import numpy
134130
from six.moves.urllib.request import urlopen
@@ -1225,7 +1221,7 @@ def rc(group, **kwargs):
12251221
'aa': 'antialiased',
12261222
}
12271223

1228-
if is_string_like(group):
1224+
if isinstance(group, six.string_types):
12291225
group = (group,)
12301226
for g in group:
12311227
for k, v in six.iteritems(kwargs):

lib/matplotlib/animation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import contextlib
3939
import tempfile
4040
import warnings
41-
from matplotlib.cbook import iterable, is_string_like, deprecated
41+
from matplotlib.cbook import iterable, deprecated
4242
from matplotlib.compat import subprocess
4343
from matplotlib import verbose
4444
from matplotlib import rcParams, rcParamsDefault, rc_context
@@ -1023,7 +1023,7 @@ class to use, such as 'ffmpeg' or 'mencoder'. If `None`,
10231023
# to use
10241024
if writer is None:
10251025
writer = rcParams['animation.writer']
1026-
elif (not is_string_like(writer) and
1026+
elif (not isinstance(writer, six.string_types) and
10271027
any(arg is not None
10281028
for arg in (fps, codec, bitrate, extra_args, metadata))):
10291029
raise RuntimeError('Passing in values for arguments '
@@ -1068,7 +1068,7 @@ class to use, such as 'ffmpeg' or 'mencoder'. If `None`,
10681068

10691069
# If we have the name of a writer, instantiate an instance of the
10701070
# registered class.
1071-
if is_string_like(writer):
1071+
if isinstance(writer, six.string_types):
10721072
if writer in writers.avail:
10731073
writer = writers[writer](fps, codec, bitrate,
10741074
extra_args=extra_args,

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
from matplotlib import _preprocess_data
1717

1818
import matplotlib.cbook as cbook
19-
from matplotlib.cbook import (mplDeprecation, STEP_LOOKUP_MAP,
20-
iterable, is_string_like,
21-
safe_first_element)
19+
from matplotlib.cbook import (
20+
mplDeprecation, STEP_LOOKUP_MAP, iterable, safe_first_element)
2221
import matplotlib.collections as mcoll
2322
import matplotlib.colors as mcolors
2423
import matplotlib.contour as mcontour
@@ -2662,7 +2661,7 @@ def get_next_color():
26622661
if autopct is not None:
26632662
xt = x + pctdistance * radius * math.cos(thetam)
26642663
yt = y + pctdistance * radius * math.sin(thetam)
2665-
if is_string_like(autopct):
2664+
if isinstance(autopct, six.string_types):
26662665
s = autopct % (100. * frac)
26672666
elif callable(autopct):
26682667
s = autopct(100. * frac)
@@ -5472,8 +5471,8 @@ def pcolor(self, *args, **kwargs):
54725471
# makes artifacts that are often disturbing.
54735472
if 'antialiased' in kwargs:
54745473
kwargs['antialiaseds'] = kwargs.pop('antialiased')
5475-
if 'antialiaseds' not in kwargs and (is_string_like(ec) and
5476-
ec.lower() == "none"):
5474+
if 'antialiaseds' not in kwargs and (
5475+
isinstance(ec, six.string_types) and ec.lower() == "none"):
54775476
kwargs['antialiaseds'] = False
54785477

54795478
kwargs.setdefault('snap', False)
@@ -6377,7 +6376,7 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
63776376

63786377
if label is None:
63796378
labels = [None]
6380-
elif is_string_like(label):
6379+
elif isinstance(label, six.string_types):
63816380
labels = [label]
63826381
else:
63836382
labels = [six.text_type(lab) for lab in label]

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def _makefill(self, x, y, kw, kwargs):
352352

353353
def _plot_args(self, tup, kwargs):
354354
ret = []
355-
if len(tup) > 1 and is_string_like(tup[-1]):
355+
if len(tup) > 1 and isinstance(tup[-1], six.string_types):
356356
linestyle, marker, color = _process_plot_format(tup[-1])
357357
tup = tup[:-1]
358358
elif len(tup) == 3:
@@ -398,7 +398,7 @@ def _plot_args(self, tup, kwargs):
398398
def _grab_next_args(self, *args, **kwargs):
399399
while args:
400400
this, args = args[:2], args[2:]
401-
if args and is_string_like(args[0]):
401+
if args and isinstance(args[0], six.string_types):
402402
this += args[0],
403403
args = args[1:]
404404
for seg in self._plot_args(this, kwargs):
@@ -1290,7 +1290,8 @@ def set_aspect(self, aspect, adjustable=None, anchor=None):
12901290
etc.
12911291
===== =====================
12921292
"""
1293-
if cbook.is_string_like(aspect) and aspect in ('equal', 'auto'):
1293+
if (isinstance(aspect, six.string_types)
1294+
and aspect in ('equal', 'auto')):
12941295
self._aspect = aspect
12951296
else:
12961297
self._aspect = float(aspect) # raise ValueError if necessary
@@ -1565,7 +1566,7 @@ def axis(self, *v, **kwargs):
15651566

15661567
emit = kwargs.get('emit', True)
15671568

1568-
if len(v) == 1 and is_string_like(v[0]):
1569+
if len(v) == 1 and isinstance(v[0], six.string_types):
15691570
s = v[0].lower()
15701571
if s == 'on':
15711572
self.set_axis_on()

lib/matplotlib/backend_bases.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def get_registered_canvas_class(format):
129129
if format not in _default_backends:
130130
return None
131131
backend_class = _default_backends[format]
132-
if cbook.is_string_like(backend_class):
132+
if isinstance(backend_class, six.string_types):
133133
backend_class = importlib.import_module(backend_class).FigureCanvas
134134
_default_backends[format] = backend_class
135135
return backend_class
@@ -2090,11 +2090,11 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20902090

20912091
if format is None:
20922092
# get format from filename, or from backend's default filetype
2093-
if cbook.is_string_like(filename):
2093+
if isinstance(filename, six.string_types):
20942094
format = os.path.splitext(filename)[1][1:]
20952095
if format is None or format == '':
20962096
format = self.get_default_filetype()
2097-
if cbook.is_string_like(filename):
2097+
if isinstance(filename, six.string_types):
20982098
filename = filename.rstrip('.') + '.' + format
20992099
format = format.lower()
21002100

lib/matplotlib/backends/backend_agg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from matplotlib import verbose, rcParams, __version__
3232
from matplotlib.backend_bases import (RendererBase, FigureManagerBase,
3333
FigureCanvasBase)
34-
from matplotlib.cbook import is_string_like, maxdict, restrict_dict
34+
from matplotlib.cbook import maxdict, restrict_dict
3535
from matplotlib.figure import Figure
3636
from matplotlib.font_manager import findfont, get_font
3737
from matplotlib.ft2font import (LOAD_FORCE_AUTOHINT, LOAD_NO_HINTING,
@@ -530,7 +530,7 @@ def print_raw(self, filename_or_obj, *args, **kwargs):
530530
renderer = self.get_renderer()
531531
original_dpi = renderer.dpi
532532
renderer.dpi = self.figure.dpi
533-
if is_string_like(filename_or_obj):
533+
if isinstance(filename_or_obj, six.string_types):
534534
fileobj = open(filename_or_obj, 'wb')
535535
close = True
536536
else:
@@ -549,7 +549,7 @@ def print_png(self, filename_or_obj, *args, **kwargs):
549549
renderer = self.get_renderer()
550550
original_dpi = renderer.dpi
551551
renderer.dpi = self.figure.dpi
552-
if is_string_like(filename_or_obj):
552+
if isinstance(filename_or_obj, six.string_types):
553553
filename_or_obj = open(filename_or_obj, 'wb')
554554
close = True
555555
else:

lib/matplotlib/backends/backend_cairo.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
3535
try:
3636
import cairo
3737
except ImportError:
38-
raise ImportError("Cairo backend requires that cairocffi or pycairo is installed.")
38+
raise ImportError("Cairo backend requires that cairocffi or pycairo "
39+
"is installed.")
3940
else:
4041
HAS_CAIRO_CFFI = False
4142
else:
@@ -49,9 +50,8 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
4950
backend_version = cairo.version
5051
del _version_required
5152

52-
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
53-
FigureManagerBase, FigureCanvasBase
54-
from matplotlib.cbook import is_string_like
53+
from matplotlib.backend_bases import (
54+
RendererBase, GraphicsContextBase, FigureManagerBase, FigureCanvasBase)
5555
from matplotlib.figure import Figure
5656
from matplotlib.mathtext import MathTextParser
5757
from matplotlib.path import Path
@@ -555,7 +555,7 @@ def _save (self, fo, format, **kwargs):
555555
raise RuntimeError ('cairo has not been compiled with SVG '
556556
'support enabled')
557557
if format == 'svgz':
558-
if is_string_like(fo):
558+
if isinstance(fo, six.string_types):
559559
fo = gzip.GzipFile(fo, 'wb')
560560
else:
561561
fo = gzip.GzipFile(None, 'wb', fileobj=fo)

lib/matplotlib/backends/backend_gdk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def fn_name(): return sys._getframe(1).f_code.co_name
2626
from matplotlib._pylab_helpers import Gcf
2727
from matplotlib.backend_bases import RendererBase, GraphicsContextBase, \
2828
FigureManagerBase, FigureCanvasBase
29-
from matplotlib.cbook import is_string_like, restrict_dict, warn_deprecated
29+
from matplotlib.cbook import restrict_dict, warn_deprecated
3030
from matplotlib.figure import Figure
3131
from matplotlib.mathtext import MathTextParser
3232
from matplotlib.transforms import Affine2D

lib/matplotlib/backends/backend_gtk.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def fn_name(): return sys._getframe(1).f_code.co_name
3434
from matplotlib.backend_bases import ShowBase
3535

3636
from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK
37-
from matplotlib.cbook import is_string_like, is_writable_file_like
37+
from matplotlib.cbook import is_writable_file_like
3838
from matplotlib.figure import Figure
3939
from matplotlib.widgets import SubplotTool
4040
from matplotlib.cbook import warn_deprecated
@@ -490,7 +490,7 @@ def _print_image(self, filename, format, *args, **kwargs):
490490

491491
options['quality'] = str(options['quality'])
492492

493-
if is_string_like(filename):
493+
if isinstance(filename, six.string_types):
494494
try:
495495
pixbuf.save(filename, format, options=options)
496496
except gobject.GError as exc:
@@ -908,7 +908,7 @@ class DialogLineprops(object):
908908
linestyled = {s: i for i, s in enumerate(linestyles)}
909909

910910
markers = [m for m in markers.MarkerStyle.markers
911-
if cbook.is_string_like(m)]
911+
if isinstance(m, six.string_types)]
912912
markerd = {s: i for i, s in enumerate(markers)}
913913

914914
def __init__(self, lines):
@@ -1068,7 +1068,7 @@ def error_msg_gtk(msg, parent=None):
10681068
if parent.flags() & gtk.TOPLEVEL == 0:
10691069
parent = None
10701070

1071-
if not is_string_like(msg):
1071+
if not isinstance(msg, six.string_types):
10721072
msg = ','.join(map(str,msg))
10731073

10741074
dialog = gtk.MessageDialog(

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def fn_name(): return sys._getframe(1).f_code.co_name
3535
from matplotlib.backend_managers import ToolManager
3636
from matplotlib import backend_tools
3737

38-
from matplotlib.cbook import is_string_like, is_writable_file_like
38+
from matplotlib.cbook import is_writable_file_like
3939
from matplotlib.figure import Figure
4040
from matplotlib.widgets import SubplotTool
4141

@@ -954,7 +954,7 @@ def error_msg_gtk(msg, parent=None):
954954
if not parent.is_toplevel():
955955
parent = None
956956

957-
if not is_string_like(msg):
957+
if not isinstance(msg, six.string_types):
958958
msg = ','.join(map(str,msg))
959959

960960
dialog = Gtk.MessageDialog(

lib/matplotlib/backends/backend_pdf.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from matplotlib.backend_bases import (RendererBase, GraphicsContextBase,
3535
FigureManagerBase, FigureCanvasBase)
3636
from matplotlib.backends.backend_mixed import MixedModeRenderer
37-
from matplotlib.cbook import (Bunch, is_string_like, get_realpath_and_stat,
37+
from matplotlib.cbook import (Bunch, get_realpath_and_stat,
3838
is_writable_file_like, maxdict)
3939
from matplotlib.figure import Figure
4040
from matplotlib.font_manager import findfont, is_opentype_cff_font, get_font
@@ -434,7 +434,7 @@ def __init__(self, filename, metadata=None):
434434
self.passed_in_file_object = False
435435
self.original_file_like = None
436436
self.tell_base = 0
437-
if is_string_like(filename):
437+
if isinstance(filename, six.string_types):
438438
fh = open(filename, 'wb')
439439
elif is_writable_file_like(filename):
440440
try:
@@ -1564,6 +1564,9 @@ def writeXref(self):
15641564
def writeInfoDict(self):
15651565
"""Write out the info dictionary, checking it for good form"""
15661566

1567+
def is_string_like(x):
1568+
return isinstance(x, six.string_types)
1569+
15671570
def is_date(x):
15681571
return isinstance(x, datetime)
15691572

lib/matplotlib/backends/backend_pgf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from matplotlib.text import Text
2626
from matplotlib.path import Path
2727
from matplotlib import _png, rcParams
28-
from matplotlib.cbook import is_string_like, is_writable_file_like
28+
from matplotlib.cbook import is_writable_file_like
2929
from matplotlib.compat import subprocess
3030
from matplotlib.compat.subprocess import check_output
3131

@@ -859,7 +859,7 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
859859
return
860860

861861
# figure out where the pgf is to be written to
862-
if is_string_like(fname_or_fh):
862+
if isinstance(fname_or_fh, six.string_types):
863863
with codecs.open(fname_or_fh, "w", encoding="utf-8") as fh:
864864
self._print_pgf_to_fh(fh, *args, **kwargs)
865865
elif is_writable_file_like(fname_or_fh):
@@ -923,7 +923,7 @@ def print_pdf(self, fname_or_fh, *args, **kwargs):
923923
return
924924

925925
# figure out where the pdf is to be written to
926-
if is_string_like(fname_or_fh):
926+
if isinstance(fname_or_fh, six.string_types):
927927
with open(fname_or_fh, "wb") as fh:
928928
self._print_pdf_to_fh(fh, *args, **kwargs)
929929
elif is_writable_file_like(fname_or_fh):
@@ -959,7 +959,7 @@ def print_png(self, fname_or_fh, *args, **kwargs):
959959
self._print_pgf_to_fh(None, *args, **kwargs)
960960
return
961961

962-
if is_string_like(fname_or_fh):
962+
if isinstance(fname_or_fh, six.string_types):
963963
with open(fname_or_fh, "wb") as fh:
964964
self._print_png_to_fh(fh, *args, **kwargs)
965965
elif is_writable_file_like(fname_or_fh):

0 commit comments

Comments
 (0)