summaryrefslogtreecommitdiff
path: root/networkx/readwrite/json_graph
diff options
context:
space:
mode:
authorJarrod Millman <jarrod.millman@gmail.com>2020-07-09 23:12:10 -0700
committerJarrod Millman <jarrod.millman@gmail.com>2020-07-10 09:44:54 -0700
commitb22d6b36ce0545995c99d233546e8a1fe7e27fc5 (patch)
tree9078401c2f4a7b463a82378a734508e16ef34867 /networkx/readwrite/json_graph
parentf30e9392bef0dccbcfd1b73ccb934064f6200fa3 (diff)
downloadnetworkx-b22d6b36ce0545995c99d233546e8a1fe7e27fc5.tar.gz
Format w/ black
Diffstat (limited to 'networkx/readwrite/json_graph')
-rw-r--r--networkx/readwrite/json_graph/adjacency.py38
-rw-r--r--networkx/readwrite/json_graph/cytoscape.py18
-rw-r--r--networkx/readwrite/json_graph/jit.py17
-rw-r--r--networkx/readwrite/json_graph/node_link.py66
-rw-r--r--networkx/readwrite/json_graph/tests/test_adjacency.py29
-rw-r--r--networkx/readwrite/json_graph/tests/test_cytoscape.py37
-rw-r--r--networkx/readwrite/json_graph/tests/test_jit.py12
-rw-r--r--networkx/readwrite/json_graph/tests/test_node_link.py63
-rw-r--r--networkx/readwrite/json_graph/tests/test_tree.py11
-rw-r--r--networkx/readwrite/json_graph/tree.py22
10 files changed, 160 insertions, 153 deletions
diff --git a/networkx/readwrite/json_graph/adjacency.py b/networkx/readwrite/json_graph/adjacency.py
index f4a42d2a..ec6d21a4 100644
--- a/networkx/readwrite/json_graph/adjacency.py
+++ b/networkx/readwrite/json_graph/adjacency.py
@@ -1,9 +1,9 @@
from itertools import chain
import networkx as nx
-__all__ = ['adjacency_data', 'adjacency_graph']
+__all__ = ["adjacency_data", "adjacency_graph"]
-_attrs = dict(id='id', key='key')
+_attrs = dict(id="id", key="key")
def adjacency_data(G, attrs=_attrs):
@@ -57,19 +57,19 @@ def adjacency_data(G, attrs=_attrs):
adjacency_graph, node_link_data, tree_data
"""
multigraph = G.is_multigraph()
- id_ = attrs['id']
+ id_ = attrs["id"]
# Allow 'key' to be omitted from attrs if the graph is not a multigraph.
- key = None if not multigraph else attrs['key']
+ key = None if not multigraph else attrs["key"]
if id_ == key:
- raise nx.NetworkXError('Attribute names are not unique.')
+ raise nx.NetworkXError("Attribute names are not unique.")
data = {}
- data['directed'] = G.is_directed()
- data['multigraph'] = multigraph
- data['graph'] = list(G.graph.items())
- data['nodes'] = []
- data['adjacency'] = []
+ data["directed"] = G.is_directed()
+ data["multigraph"] = multigraph
+ data["graph"] = list(G.graph.items())
+ data["nodes"] = []
+ data["adjacency"] = []
for n, nbrdict in G.adjacency():
- data['nodes'].append(dict(chain(G.nodes[n].items(), [(id_, n)])))
+ data["nodes"].append(dict(chain(G.nodes[n].items(), [(id_, n)])))
adj = []
if multigraph:
for nbr, keys in nbrdict.items():
@@ -78,7 +78,7 @@ def adjacency_data(G, attrs=_attrs):
else:
for nbr, d in nbrdict.items():
adj.append(dict(chain(d.items(), [(id_, nbr)])))
- data['adjacency'].append(adj)
+ data["adjacency"].append(adj)
return data
@@ -122,26 +122,26 @@ def adjacency_graph(data, directed=False, multigraph=True, attrs=_attrs):
--------
adjacency_graph, node_link_data, tree_data
"""
- multigraph = data.get('multigraph', multigraph)
- directed = data.get('directed', directed)
+ multigraph = data.get("multigraph", multigraph)
+ directed = data.get("directed", directed)
if multigraph:
graph = nx.MultiGraph()
else:
graph = nx.Graph()
if directed:
graph = graph.to_directed()
- id_ = attrs['id']
+ id_ = attrs["id"]
# Allow 'key' to be omitted from attrs if the graph is not a multigraph.
- key = None if not multigraph else attrs['key']
- graph.graph = dict(data.get('graph', []))
+ key = None if not multigraph else attrs["key"]
+ graph.graph = dict(data.get("graph", []))
mapping = []
- for d in data['nodes']:
+ for d in data["nodes"]:
node_data = d.copy()
node = node_data.pop(id_)
mapping.append(node)
graph.add_node(node)
graph.nodes[node].update(node_data)
- for i, d in enumerate(data['adjacency']):
+ for i, d in enumerate(data["adjacency"]):
source = mapping[i]
for tdata in d:
target_data = tdata.copy()
diff --git a/networkx/readwrite/json_graph/cytoscape.py b/networkx/readwrite/json_graph/cytoscape.py
index 202d6a03..1a6f0c06 100644
--- a/networkx/readwrite/json_graph/cytoscape.py
+++ b/networkx/readwrite/json_graph/cytoscape.py
@@ -1,8 +1,8 @@
import networkx as nx
-__all__ = ['cytoscape_data', 'cytoscape_graph']
+__all__ = ["cytoscape_data", "cytoscape_graph"]
-_attrs = dict(name='name', ident='id')
+_attrs = dict(name="name", ident="id")
def cytoscape_data(G, attrs=None):
@@ -31,11 +31,11 @@ def cytoscape_data(G, attrs=None):
ident = attrs["ident"]
if len({name, ident}) < 2:
- raise nx.NetworkXError('Attribute names are not unique.')
+ raise nx.NetworkXError("Attribute names are not unique.")
jsondata = {"data": list(G.graph.items())}
- jsondata['directed'] = G.is_directed()
- jsondata['multigraph'] = G.is_multigraph()
+ jsondata["directed"] = G.is_directed()
+ jsondata["multigraph"] = G.is_multigraph()
jsondata["elements"] = {"nodes": [], "edges": []}
nodes = jsondata["elements"]["nodes"]
edges = jsondata["elements"]["edges"]
@@ -73,17 +73,17 @@ def cytoscape_graph(data, attrs=None):
ident = attrs["ident"]
if len({ident, name}) < 2:
- raise nx.NetworkXError('Attribute names are not unique.')
+ raise nx.NetworkXError("Attribute names are not unique.")
- multigraph = data.get('multigraph')
- directed = data.get('directed')
+ multigraph = data.get("multigraph")
+ directed = data.get("directed")
if multigraph:
graph = nx.MultiGraph()
else:
graph = nx.Graph()
if directed:
graph = graph.to_directed()
- graph.graph = dict(data.get('data'))
+ graph.graph = dict(data.get("data"))
for d in data["elements"]["nodes"]:
node_data = d["data"].copy()
node = d["data"]["value"]
diff --git a/networkx/readwrite/json_graph/jit.py b/networkx/readwrite/json_graph/jit.py
index f404003c..ccef18b6 100644
--- a/networkx/readwrite/json_graph/jit.py
+++ b/networkx/readwrite/json_graph/jit.py
@@ -30,7 +30,7 @@ import json
import networkx as nx
from networkx.utils.decorators import not_implemented_for
-__all__ = ['jit_graph', 'jit_data']
+__all__ = ["jit_graph", "jit_data"]
def jit_graph(data, create_using=None):
@@ -57,14 +57,14 @@ def jit_graph(data, create_using=None):
data = json.loads(data)
for node in data:
- G.add_node(node['id'], **node['data'])
- if node.get('adjacencies') is not None:
- for adj in node['adjacencies']:
- G.add_edge(node['id'], adj['nodeTo'], **adj['data'])
+ G.add_node(node["id"], **node["data"])
+ if node.get("adjacencies") is not None:
+ for adj in node["adjacencies"]:
+ G.add_edge(node["id"], adj["nodeTo"], **adj["data"])
return G
-@not_implemented_for('multigraph')
+@not_implemented_for("multigraph")
def jit_data(G, indent=None, default=None):
"""Returns data in JIT JSON format.
@@ -88,10 +88,7 @@ def jit_data(G, indent=None, default=None):
"""
json_graph = []
for node in G.nodes():
- json_node = {
- "id": node,
- "name": node
- }
+ json_node = {"id": node, "name": node}
# node data
json_node["data"] = G.nodes[node]
# adjacencies
diff --git a/networkx/readwrite/json_graph/node_link.py b/networkx/readwrite/json_graph/node_link.py
index 721f0034..42c4d07f 100644
--- a/networkx/readwrite/json_graph/node_link.py
+++ b/networkx/readwrite/json_graph/node_link.py
@@ -1,11 +1,11 @@
from itertools import chain, count
import networkx as nx
from networkx.utils import to_tuple
-__all__ = ['node_link_data', 'node_link_graph']
+__all__ = ["node_link_data", "node_link_graph"]
-_attrs = dict(source='source', target='target', name='id',
- key='key', link='links')
+
+_attrs = dict(source="source", target="target", name="id", key="key", link="links")
def node_link_data(G, attrs=None):
@@ -69,26 +69,30 @@ def node_link_data(G, attrs=None):
attrs = _attrs
else:
attrs.update({k: v for (k, v) in _attrs.items() if k not in attrs})
- name = attrs['name']
- source = attrs['source']
- target = attrs['target']
- links = attrs['link']
+ name = attrs["name"]
+ source = attrs["source"]
+ target = attrs["target"]
+ links = attrs["link"]
# Allow 'key' to be omitted from attrs if the graph is not a multigraph.
- key = None if not multigraph else attrs['key']
+ key = None if not multigraph else attrs["key"]
if len({source, target, key}) < 3:
- raise nx.NetworkXError('Attribute names are not unique.')
- data = {'directed': G.is_directed(), 'multigraph': multigraph, 'graph': G.graph,
- 'nodes': [dict(chain(G.nodes[n].items(), [(name, n)])) for n in G]}
+ raise nx.NetworkXError("Attribute names are not unique.")
+ data = {
+ "directed": G.is_directed(),
+ "multigraph": multigraph,
+ "graph": G.graph,
+ "nodes": [dict(chain(G.nodes[n].items(), [(name, n)])) for n in G],
+ }
if multigraph:
data[links] = [
- dict(chain(d.items(),
- [(source, u), (target, v), (key, k)]))
- for u, v, k, d in G.edges(keys=True, data=True)]
+ dict(chain(d.items(), [(source, u), (target, v), (key, k)]))
+ for u, v, k, d in G.edges(keys=True, data=True)
+ ]
else:
data[links] = [
- dict(chain(d.items(),
- [(source, u), (target, v)]))
- for u, v, d in G.edges(data=True)]
+ dict(chain(d.items(), [(source, u), (target, v)]))
+ for u, v, d in G.edges(data=True)
+ ]
return data
@@ -139,23 +143,23 @@ def node_link_graph(data, directed=False, multigraph=True, attrs=None):
attrs = _attrs
else:
attrs.update({k: v for k, v in _attrs.items() if k not in attrs})
- multigraph = data.get('multigraph', multigraph)
- directed = data.get('directed', directed)
+ multigraph = data.get("multigraph", multigraph)
+ directed = data.get("directed", directed)
if multigraph:
graph = nx.MultiGraph()
else:
graph = nx.Graph()
if directed:
graph = graph.to_directed()
- name = attrs['name']
- source = attrs['source']
- target = attrs['target']
- links = attrs['link']
+ name = attrs["name"]
+ source = attrs["source"]
+ target = attrs["target"]
+ links = attrs["link"]
# Allow 'key' to be omitted from attrs if the graph is not a multigraph.
- key = None if not multigraph else attrs['key']
- graph.graph = data.get('graph', {})
+ key = None if not multigraph else attrs["key"]
+ graph.graph = data.get("graph", {})
c = count()
- for d in data['nodes']:
+ for d in data["nodes"]:
node = to_tuple(d.get(name, next(c)))
nodedata = {str(k): v for k, v in d.items() if k != name}
graph.add_node(node, **nodedata)
@@ -163,12 +167,14 @@ def node_link_graph(data, directed=False, multigraph=True, attrs=None):
src = tuple(d[source]) if isinstance(d[source], list) else d[source]
tgt = tuple(d[target]) if isinstance(d[target], list) else d[target]
if not multigraph:
- edgedata = {str(k): v for k, v in d.items()
- if k != source and k != target}
+ edgedata = {str(k): v for k, v in d.items() if k != source and k != target}
graph.add_edge(src, tgt, **edgedata)
else:
ky = d.get(key, None)
- edgedata = {str(k): v for k, v in d.items()
- if k != source and k != target and k != key}
+ edgedata = {
+ str(k): v
+ for k, v in d.items()
+ if k != source and k != target and k != key
+ }
graph.add_edge(src, tgt, ky, **edgedata)
return graph
diff --git a/networkx/readwrite/json_graph/tests/test_adjacency.py b/networkx/readwrite/json_graph/tests/test_adjacency.py
index 08bbb5fe..57a2a6b1 100644
--- a/networkx/readwrite/json_graph/tests/test_adjacency.py
+++ b/networkx/readwrite/json_graph/tests/test_adjacency.py
@@ -5,7 +5,6 @@ from networkx.readwrite.json_graph import adjacency_data, adjacency_graph
class TestAdjacency:
-
def test_graph(self):
G = nx.path_graph(4)
H = adjacency_graph(adjacency_data(G))
@@ -13,22 +12,22 @@ class TestAdjacency:
def test_graph_attributes(self):
G = nx.path_graph(4)
- G.add_node(1, color='red')
+ G.add_node(1, color="red")
G.add_edge(1, 2, width=7)
- G.graph['foo'] = 'bar'
- G.graph[1] = 'one'
+ G.graph["foo"] = "bar"
+ G.graph[1] = "one"
H = adjacency_graph(adjacency_data(G))
- assert H.graph['foo'] == 'bar'
- assert H.nodes[1]['color'] == 'red'
- assert H[1][2]['width'] == 7
+ assert H.graph["foo"] == "bar"
+ assert H.nodes[1]["color"] == "red"
+ assert H[1][2]["width"] == 7
d = json.dumps(adjacency_data(G))
H = adjacency_graph(json.loads(d))
- assert H.graph['foo'] == 'bar'
- assert H.graph[1] == 'one'
- assert H.nodes[1]['color'] == 'red'
- assert H[1][2]['width'] == 7
+ assert H.graph["foo"] == "bar"
+ assert H.graph[1] == "one"
+ assert H.nodes[1]["color"] == "red"
+ assert H[1][2]["width"] == 7
def test_digraph(self):
G = nx.DiGraph()
@@ -46,14 +45,14 @@ class TestAdjacency:
def test_multigraph(self):
G = nx.MultiGraph()
- G.add_edge(1, 2, key='first')
- G.add_edge(1, 2, key='second', color='blue')
+ G.add_edge(1, 2, key="first")
+ G.add_edge(1, 2, key="second", color="blue")
H = adjacency_graph(adjacency_data(G))
nx.is_isomorphic(G, H)
- assert H[1][2]['second']['color'] == 'blue'
+ assert H[1][2]["second"]["color"] == "blue"
def test_exception(self):
with pytest.raises(nx.NetworkXError):
G = nx.MultiDiGraph()
- attrs = dict(id='node', key='node')
+ attrs = dict(id="node", key="node")
adjacency_data(G, attrs)
diff --git a/networkx/readwrite/json_graph/tests/test_cytoscape.py b/networkx/readwrite/json_graph/tests/test_cytoscape.py
index 16c917d9..ee4799fb 100644
--- a/networkx/readwrite/json_graph/tests/test_cytoscape.py
+++ b/networkx/readwrite/json_graph/tests/test_cytoscape.py
@@ -5,7 +5,6 @@ from networkx.readwrite.json_graph import cytoscape_data, cytoscape_graph
class TestCytoscape:
-
def test_graph(self):
G = nx.path_graph(4)
H = cytoscape_graph(cytoscape_data(G))
@@ -13,27 +12,27 @@ class TestCytoscape:
def test_graph_attributes(self):
G = nx.path_graph(4)
- G.add_node(1, color='red')
+ G.add_node(1, color="red")
G.add_edge(1, 2, width=7)
- G.graph['foo'] = 'bar'
- G.graph[1] = 'one'
+ G.graph["foo"] = "bar"
+ G.graph[1] = "one"
G.add_node(3, name="node", id="123")
H = cytoscape_graph(cytoscape_data(G))
- assert H.graph['foo'] == 'bar'
- assert H.nodes[1]['color'] == 'red'
- assert H[1][2]['width'] == 7
- assert H.nodes[3]['name'] == 'node'
- assert H.nodes[3]['id'] == '123'
+ assert H.graph["foo"] == "bar"
+ assert H.nodes[1]["color"] == "red"
+ assert H[1][2]["width"] == 7
+ assert H.nodes[3]["name"] == "node"
+ assert H.nodes[3]["id"] == "123"
d = json.dumps(cytoscape_data(G))
H = cytoscape_graph(json.loads(d))
- assert H.graph['foo'] == 'bar'
- assert H.graph[1] == 'one'
- assert H.nodes[1]['color'] == 'red'
- assert H[1][2]['width'] == 7
- assert H.nodes[3]['name'] == 'node'
- assert H.nodes[3]['id'] == '123'
+ assert H.graph["foo"] == "bar"
+ assert H.graph[1] == "one"
+ assert H.nodes[1]["color"] == "red"
+ assert H[1][2]["width"] == 7
+ assert H.nodes[3]["name"] == "node"
+ assert H.nodes[3]["id"] == "123"
def test_digraph(self):
G = nx.DiGraph()
@@ -51,14 +50,14 @@ class TestCytoscape:
def test_multigraph(self):
G = nx.MultiGraph()
- G.add_edge(1, 2, key='first')
- G.add_edge(1, 2, key='second', color='blue')
+ G.add_edge(1, 2, key="first")
+ G.add_edge(1, 2, key="second", color="blue")
H = cytoscape_graph(cytoscape_data(G))
assert nx.is_isomorphic(G, H)
- assert H[1][2]['second']['color'] == 'blue'
+ assert H[1][2]["second"]["color"] == "blue"
def test_exception(self):
with pytest.raises(nx.NetworkXError):
G = nx.MultiDiGraph()
- attrs = dict(name='node', ident='node')
+ attrs = dict(name="node", ident="node")
cytoscape_data(G, attrs)
diff --git a/networkx/readwrite/json_graph/tests/test_jit.py b/networkx/readwrite/json_graph/tests/test_jit.py
index a251242f..9a2ef682 100644
--- a/networkx/readwrite/json_graph/tests/test_jit.py
+++ b/networkx/readwrite/json_graph/tests/test_jit.py
@@ -7,12 +7,12 @@ from networkx.readwrite.json_graph import jit_data, jit_graph
class TestJIT:
def test_jit(self):
G = nx.Graph()
- G.add_node('Node1', node_data='foobar')
- G.add_node('Node3', node_data='bar')
- G.add_node('Node4')
- G.add_edge('Node1', 'Node2', weight=9, something='isSomething')
- G.add_edge('Node2', 'Node3', weight=4, something='isNotSomething')
- G.add_edge('Node1', 'Node2')
+ G.add_node("Node1", node_data="foobar")
+ G.add_node("Node3", node_data="bar")
+ G.add_node("Node4")
+ G.add_edge("Node1", "Node2", weight=9, something="isSomething")
+ G.add_edge("Node2", "Node3", weight=4, something="isNotSomething")
+ G.add_edge("Node1", "Node2")
d = jit_data(G)
K = jit_graph(json.loads(d))
assert nx.is_isomorphic(G, K)
diff --git a/networkx/readwrite/json_graph/tests/test_node_link.py b/networkx/readwrite/json_graph/tests/test_node_link.py
index 75fe142e..e5773d26 100644
--- a/networkx/readwrite/json_graph/tests/test_node_link.py
+++ b/networkx/readwrite/json_graph/tests/test_node_link.py
@@ -5,7 +5,6 @@ from networkx.readwrite.json_graph import node_link_data, node_link_graph
class TestNodeLink:
-
def test_graph(self):
G = nx.path_graph(4)
H = node_link_graph(node_link_data(G))
@@ -13,22 +12,22 @@ class TestNodeLink:
def test_graph_attributes(self):
G = nx.path_graph(4)
- G.add_node(1, color='red')
+ G.add_node(1, color="red")
G.add_edge(1, 2, width=7)
- G.graph[1] = 'one'
- G.graph['foo'] = 'bar'
+ G.graph[1] = "one"
+ G.graph["foo"] = "bar"
H = node_link_graph(node_link_data(G))
- assert H.graph['foo'] == 'bar'
- assert H.nodes[1]['color'] == 'red'
- assert H[1][2]['width'] == 7
+ assert H.graph["foo"] == "bar"
+ assert H.nodes[1]["color"] == "red"
+ assert H[1][2]["width"] == 7
d = json.dumps(node_link_data(G))
H = node_link_graph(json.loads(d))
- assert H.graph['foo'] == 'bar'
- assert H.graph['1'] == 'one'
- assert H.nodes[1]['color'] == 'red'
- assert H[1][2]['width'] == 7
+ assert H.graph["foo"] == "bar"
+ assert H.graph["1"] == "one"
+ assert H.nodes[1]["color"] == "red"
+ assert H[1][2]["width"] == 7
def test_digraph(self):
G = nx.DiGraph()
@@ -37,11 +36,11 @@ class TestNodeLink:
def test_multigraph(self):
G = nx.MultiGraph()
- G.add_edge(1, 2, key='first')
- G.add_edge(1, 2, key='second', color='blue')
+ G.add_edge(1, 2, key="first")
+ G.add_edge(1, 2, key="second", color="blue")
H = node_link_graph(node_link_data(G))
nx.is_isomorphic(G, H)
- assert H[1][2]['second']['color'] == 'blue'
+ assert H[1][2]["second"]["color"] == "blue"
def test_graph_with_tuple_nodes(self):
G = nx.Graph()
@@ -51,7 +50,7 @@ class TestNodeLink:
dd = json.loads(dumped_d)
H = node_link_graph(dd)
assert H.nodes[(0, 0)] == G.nodes[(0, 0)]
- assert H[(0, 0)][(1, 0)]['color'] == [255, 255, 0]
+ assert H[(0, 0)][(1, 0)]["color"] == [255, 255, 0]
def test_unicode_keys(self):
q = "qualité"
@@ -66,32 +65,40 @@ class TestNodeLink:
def test_exception(self):
with pytest.raises(nx.NetworkXError):
G = nx.MultiDiGraph()
- attrs = dict(name='node', source='node', target='node', key='node')
+ attrs = dict(name="node", source="node", target="node", key="node")
node_link_data(G, attrs)
def test_string_ids(self):
q = "qualité"
G = nx.DiGraph()
- G.add_node('A')
+ G.add_node("A")
G.add_node(q)
- G.add_edge('A', q)
+ G.add_edge("A", q)
data = node_link_data(G)
- assert data['links'][0]['source'] == 'A'
- assert data['links'][0]['target'] == q
+ assert data["links"][0]["source"] == "A"
+ assert data["links"][0]["target"] == q
H = node_link_graph(data)
assert nx.is_isomorphic(G, H)
def test_custom_attrs(self):
G = nx.path_graph(4)
- G.add_node(1, color='red')
+ G.add_node(1, color="red")
G.add_edge(1, 2, width=7)
- G.graph[1] = 'one'
- G.graph['foo'] = 'bar'
+ G.graph[1] = "one"
+ G.graph["foo"] = "bar"
- attrs = dict(source='c_source', target='c_target', name='c_id', key='c_key', link='c_links')
+ attrs = dict(
+ source="c_source",
+ target="c_target",
+ name="c_id",
+ key="c_key",
+ link="c_links",
+ )
- H = node_link_graph(node_link_data(G, attrs=attrs), multigraph=False, attrs=attrs)
+ H = node_link_graph(
+ node_link_data(G, attrs=attrs), multigraph=False, attrs=attrs
+ )
assert nx.is_isomorphic(G, H)
- assert H.graph['foo'] == 'bar'
- assert H.nodes[1]['color'] == 'red'
- assert H[1][2]['width'] == 7
+ assert H.graph["foo"] == "bar"
+ assert H.nodes[1]["color"] == "red"
+ assert H[1][2]["width"] == 7
diff --git a/networkx/readwrite/json_graph/tests/test_tree.py b/networkx/readwrite/json_graph/tests/test_tree.py
index 5decfaf3..8deda52b 100644
--- a/networkx/readwrite/json_graph/tests/test_tree.py
+++ b/networkx/readwrite/json_graph/tests/test_tree.py
@@ -5,10 +5,9 @@ from networkx.readwrite.json_graph import tree_data, tree_graph
class TestTree:
-
def test_graph(self):
G = nx.DiGraph()
- G.add_nodes_from([1, 2, 3], color='red')
+ G.add_nodes_from([1, 2, 3], color="red")
G.add_edge(1, 2, foo=7)
G.add_edge(1, 3, foo=10)
G.add_edge(3, 4, foo=10)
@@ -17,20 +16,20 @@ class TestTree:
def test_graph_attributes(self):
G = nx.DiGraph()
- G.add_nodes_from([1, 2, 3], color='red')
+ G.add_nodes_from([1, 2, 3], color="red")
G.add_edge(1, 2, foo=7)
G.add_edge(1, 3, foo=10)
G.add_edge(3, 4, foo=10)
H = tree_graph(tree_data(G, 1))
- assert H.nodes[1]['color'] == 'red'
+ assert H.nodes[1]["color"] == "red"
d = json.dumps(tree_data(G, 1))
H = tree_graph(json.loads(d))
- assert H.nodes[1]['color'] == 'red'
+ assert H.nodes[1]["color"] == "red"
def test_exception(self):
with pytest.raises(nx.NetworkXError):
G = nx.MultiDiGraph()
G.add_node(0)
- attrs = dict(id='node', children='node')
+ attrs = dict(id="node", children="node")
tree_data(G, 0, attrs)
diff --git a/networkx/readwrite/json_graph/tree.py b/networkx/readwrite/json_graph/tree.py
index 70ca186f..30ef1b53 100644
--- a/networkx/readwrite/json_graph/tree.py
+++ b/networkx/readwrite/json_graph/tree.py
@@ -1,9 +1,9 @@
from itertools import chain
import networkx as nx
-__all__ = ['tree_data', 'tree_graph']
+__all__ = ["tree_data", "tree_graph"]
-_attrs = dict(id='id', children='children')
+_attrs = dict(id="id", children="children")
def tree_data(G, root, attrs=_attrs):
@@ -66,10 +66,10 @@ def tree_data(G, root, attrs=_attrs):
if not G.is_directed():
raise TypeError("G is not directed.")
- id_ = attrs['id']
- children = attrs['children']
+ id_ = attrs["id"]
+ children = attrs["children"]
if id_ == children:
- raise nx.NetworkXError('Attribute names are not unique.')
+ raise nx.NetworkXError("Attribute names are not unique.")
def add_children(n, G):
nbrs = G[n]
@@ -123,8 +123,8 @@ def tree_graph(data, attrs=_attrs):
tree_graph, node_link_data, adjacency_data
"""
graph = nx.DiGraph()
- id_ = attrs['id']
- children = attrs['children']
+ id_ = attrs["id"]
+ children = attrs["children"]
def add_children(parent, children_):
for data in children_:
@@ -133,14 +133,14 @@ def tree_graph(data, attrs=_attrs):
grandchildren = data.get(children, [])
if grandchildren:
add_children(child, grandchildren)
- nodedata = {str(k): v for k, v in data.items()
- if k != id_ and k != children}
+ nodedata = {
+ str(k): v for k, v in data.items() if k != id_ and k != children
+ }
graph.add_node(child, **nodedata)
root = data[id_]
children_ = data.get(children, [])
- nodedata = {str(k): v for k, v in data.items()
- if k != id_ and k != children}
+ nodedata = {str(k): v for k, v in data.items() if k != id_ and k != children}
graph.add_node(root, **nodedata)
add_children(root, children_)
return graph