summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJarrod Millman <jarrod.millman@gmail.com>2017-09-10 08:42:01 -0700
committerGitHub <noreply@github.com>2017-09-10 08:42:01 -0700
commit0ab38ce6c46ce678bda23c1d26494683ef93e40f (patch)
tree02ff96b07885b5cdc7e85f57d165a3ca3a914e85
parent06c701e3a4c598ec88c799d325de541e47af042c (diff)
downloadnetworkx-0ab38ce6c46ce678bda23c1d26494683ef93e40f.tar.gz
Use isinstance in is_string_like (#2660)
See https://github.com/matplotlib/matplotlib/pull/8011
-rw-r--r--networkx/algorithms/operators/all.py1
-rw-r--r--networkx/drawing/nx_pylab.py20
-rw-r--r--networkx/readwrite/leda.py2
-rw-r--r--networkx/readwrite/p2g.py6
-rw-r--r--networkx/readwrite/pajek.py4
-rw-r--r--networkx/utils/misc.py13
6 files changed, 21 insertions, 25 deletions
diff --git a/networkx/algorithms/operators/all.py b/networkx/algorithms/operators/all.py
index f37cb40c..21339a35 100644
--- a/networkx/algorithms/operators/all.py
+++ b/networkx/algorithms/operators/all.py
@@ -11,7 +11,6 @@ try:
except ImportError: # Python3 has zip_longest
from itertools import zip_longest
import networkx as nx
-# from networkx.utils import is_string_like
__author__ = """\n""".join(['Robert King <kingrobertking@gmail.com>',
'Aric Hagberg <aric.hagberg@gmail.com>'])
diff --git a/networkx/drawing/nx_pylab.py b/networkx/drawing/nx_pylab.py
index 5832b9a7..1927d0c7 100644
--- a/networkx/drawing/nx_pylab.py
+++ b/networkx/drawing/nx_pylab.py
@@ -22,6 +22,7 @@ pygraphviz: http://pygraphviz.github.io/
"""
import networkx as nx
+from networkx.utils import is_string_like
from networkx.drawing.layout import shell_layout, \
circular_layout, kamada_kawai_layout, spectral_layout, \
spring_layout, random_layout
@@ -39,13 +40,6 @@ __all__ = ['draw',
'draw_spring',
'draw_shell']
-# 2.x/3.x compatibility
-try:
- basestring
-except NameError:
- basestring = str
- unicode = str
-
def draw(G, pos=None, ax=None, **kwds):
"""Draw the graph G with Matplotlib.
@@ -526,15 +520,15 @@ def draw_networkx_edges(G, pos,
else:
lw = width
- if not isinstance(edge_color, basestring) \
+ if not is_string_like(edge_color) \
and cb.iterable(edge_color) \
and len(edge_color) == len(edge_pos):
- if np.alltrue([isinstance(c, basestring) for c in edge_color]):
+ if np.alltrue([is_string_like(c) for c in edge_color]):
# (should check ALL elements)
# list of color letters such as ['k','r','k',...]
edge_colors = tuple([colorConverter.to_rgba(c, alpha)
for c in edge_color])
- elif np.alltrue([not isinstance(c, basestring) for c in edge_color]):
+ elif np.alltrue([not is_string_like(c) for c in edge_color]):
# If color specs are given as (rgb) or (rgba) tuples, we're OK
if np.alltrue([cb.iterable(c) and len(c) in (3, 4)
for c in edge_color]):
@@ -545,7 +539,7 @@ def draw_networkx_edges(G, pos,
else:
raise ValueError('edge_color must consist of either color names or numbers')
else:
- if isinstance(edge_color, basestring) or len(edge_color) == 1:
+ if is_string_like(edge_color) or len(edge_color) == 1:
edge_colors = (colorConverter.to_rgba(edge_color, alpha), )
else:
raise ValueError(
@@ -726,7 +720,7 @@ def draw_networkx_labels(G, pos,
text_items = {} # there is no text collection so we'll fake one
for n, label in labels.items():
(x, y) = pos[n]
- if not isinstance(label, basestring):
+ if not is_string_like(label):
label = str(label) # this will cause "1" and 1 to be labeled the same
t = ax.text(x, y,
label,
@@ -864,7 +858,7 @@ def draw_networkx_edge_labels(G, pos,
ec=(1.0, 1.0, 1.0),
fc=(1.0, 1.0, 1.0),
)
- if not isinstance(label, basestring):
+ if not is_string_like(label):
label = str(label) # this will cause "1" and 1 to be labeled the same
# set optional alignment
diff --git a/networkx/readwrite/leda.py b/networkx/readwrite/leda.py
index b96a590f..3ee4fc45 100644
--- a/networkx/readwrite/leda.py
+++ b/networkx/readwrite/leda.py
@@ -32,7 +32,7 @@ def read_leda(path, encoding='UTF-8'):
Parameters
----------
path : file or string
- File or filename to read. Filenames ending in .gz or .bz2 will be
+ File or filename to read. Filenames ending in .gz or .bz2 will be
uncompressed.
Returns
diff --git a/networkx/readwrite/p2g.py b/networkx/readwrite/p2g.py
index 910a6dd5..934c4564 100644
--- a/networkx/readwrite/p2g.py
+++ b/networkx/readwrite/p2g.py
@@ -1,5 +1,5 @@
"""
-This module provides the following: read and write of p2g format
+This module provides the following: read and write of p2g format
used in metabolic pathway studies.
See https://web.archive.org/web/20080626113807/http://www.cs.purdue.edu/homes/koyuturk/pathway/ for a description.
@@ -66,7 +66,7 @@ def write_p2g(G, path, encoding='utf-8'):
@open_file(0, mode='r')
def read_p2g(path, encoding='utf-8'):
- """Read graph in p2g format from path.
+ """Read graph in p2g format from path.
Returns
-------
@@ -83,7 +83,7 @@ def read_p2g(path, encoding='utf-8'):
def parse_p2g(lines):
- """Parse p2g format graph from string or iterable.
+ """Parse p2g format graph from string or iterable.
Returns
-------
diff --git a/networkx/readwrite/pajek.py b/networkx/readwrite/pajek.py
index f75aaa8b..1409d89d 100644
--- a/networkx/readwrite/pajek.py
+++ b/networkx/readwrite/pajek.py
@@ -205,10 +205,10 @@ def parse_pajek(lines):
G.nodes[label].update(extra_attr)
elif l.lower().startswith("*edges") or l.lower().startswith("*arcs"):
if l.lower().startswith("*edge"):
- # switch from multidigraph to multigraph
+ # switch from multidigraph to multigraph
G = nx.MultiGraph(G)
if l.lower().startswith("*arcs"):
- # switch to directed with multiple arcs for each existing edge
+ # switch to directed with multiple arcs for each existing edge
G = G.to_directed()
for l in lines:
try:
diff --git a/networkx/utils/misc.py b/networkx/utils/misc.py
index ba5ca78c..4e826d6d 100644
--- a/networkx/utils/misc.py
+++ b/networkx/utils/misc.py
@@ -47,6 +47,13 @@ except ImportError:
total = func(total, element)
yield total
+# 2.x/3.x compatibility
+try:
+ basestring
+except NameError:
+ basestring = str
+ unicode = str
+
# some cookbook stuff
# used in deciding whether something is a bunch of nodes, edges, etc.
# see G.add_nodes and others in Graph Class in networkx/base.py
@@ -54,11 +61,7 @@ except ImportError:
def is_string_like(obj): # from John Hunter, types-free version
"""Check if obj is string."""
- try:
- obj + ''
- except (TypeError, ValueError):
- return False
- return True
+ return isinstance(obj, basestring)
def iterable(obj):