summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiamondJoseph <53935796+DiamondJoseph@users.noreply.github.com>2023-05-02 16:24:45 +0100
committerGitHub <noreply@github.com>2023-05-02 11:24:45 -0400
commitb467fdd82b9039b84b183e966749bd5a9a7a4770 (patch)
tree663fc4939daf6704f1f9c21545071bedfa8e3f9b
parenteef342543125514adc8dea3cfbd80dcfb777d254 (diff)
downloadnetworkx-b467fdd82b9039b84b183e966749bd5a9a7a4770.tar.gz
Use unpacking operator on dicts to prevent constructing intermediate objects (#6040)
-rw-r--r--networkx/readwrite/json_graph/adjacency.py8
-rw-r--r--networkx/readwrite/json_graph/node_link.py9
-rw-r--r--networkx/readwrite/json_graph/tree.py6
3 files changed, 8 insertions, 15 deletions
diff --git a/networkx/readwrite/json_graph/adjacency.py b/networkx/readwrite/json_graph/adjacency.py
index 0cb866a4..066b78f1 100644
--- a/networkx/readwrite/json_graph/adjacency.py
+++ b/networkx/readwrite/json_graph/adjacency.py
@@ -1,5 +1,3 @@
-from itertools import chain
-
import networkx as nx
__all__ = ["adjacency_data", "adjacency_graph"]
@@ -70,15 +68,15 @@ def adjacency_data(G, attrs=_attrs):
data["nodes"] = []
data["adjacency"] = []
for n, nbrdict in G.adjacency():
- data["nodes"].append(dict(chain(G.nodes[n].items(), [(id_, n)])))
+ data["nodes"].append({**G.nodes[n], id_: n})
adj = []
if multigraph:
for nbr, keys in nbrdict.items():
for k, d in keys.items():
- adj.append(dict(chain(d.items(), [(id_, nbr), (key, k)])))
+ adj.append({**d, id_: nbr, key: k})
else:
for nbr, d in nbrdict.items():
- adj.append(dict(chain(d.items(), [(id_, nbr)])))
+ adj.append({**d, id_: nbr})
data["adjacency"].append(adj)
return data
diff --git a/networkx/readwrite/json_graph/node_link.py b/networkx/readwrite/json_graph/node_link.py
index 86d3a4c2..c5957f67 100644
--- a/networkx/readwrite/json_graph/node_link.py
+++ b/networkx/readwrite/json_graph/node_link.py
@@ -169,18 +169,15 @@ def node_link_data(
"directed": G.is_directed(),
"multigraph": multigraph,
"graph": G.graph,
- "nodes": [dict(chain(G.nodes[n].items(), [(name, n)])) for n in G],
+ "nodes": [{**G.nodes[n], name: n} for n in G],
}
if multigraph:
data[link] = [
- dict(chain(d.items(), [(source, u), (target, v), (key, k)]))
+ {**d, source: u, target: v, key: k}
for u, v, k, d in G.edges(keys=True, data=True)
]
else:
- data[link] = [
- dict(chain(d.items(), [(source, u), (target, v)]))
- for u, v, d in G.edges(data=True)
- ]
+ data[link] = [{**d, source: u, target: v} for u, v, d in G.edges(data=True)]
return data
diff --git a/networkx/readwrite/json_graph/tree.py b/networkx/readwrite/json_graph/tree.py
index 3e9a4c95..5ec000d5 100644
--- a/networkx/readwrite/json_graph/tree.py
+++ b/networkx/readwrite/json_graph/tree.py
@@ -73,16 +73,14 @@ def tree_data(G, root, ident="id", children="children"):
return []
children_ = []
for child in nbrs:
- d = dict(chain(G.nodes[child].items(), [(ident, child)]))
+ d = {**G.nodes[child], ident: child}
c = add_children(child, G)
if c:
d[children] = c
children_.append(d)
return children_
- data = dict(chain(G.nodes[root].items(), [(ident, root)]))
- data[children] = add_children(root, G)
- return data
+ return {**G.nodes[root], ident: root, children: add_children(root, G)}
def tree_graph(data, ident="id", children="children"):