diff options
author | nsengaw4c <nsengiyumvawilberforce@gmail.com> | 2022-11-08 18:30:37 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-09 00:30:37 +0100 |
commit | dfe47b1601b92cf08107e1d1b7a1b2fe8a638757 (patch) | |
tree | f89e68a9b5bcbfad566cfc3919eceb868c7cd675 | |
parent | cc734a40bd78721557e004cb9d3b2384337cbdbf (diff) | |
download | networkx-dfe47b1601b92cf08107e1d1b7a1b2fe8a638757.tar.gz |
Add ThinGraph example to Multi*Graph doc_strings (#6160)
-rw-r--r-- | networkx/classes/multidigraph.py | 21 | ||||
-rw-r--r-- | networkx/classes/multigraph.py | 21 |
2 files changed, 42 insertions, 0 deletions
diff --git a/networkx/classes/multidigraph.py b/networkx/classes/multidigraph.py index 31c33f67..81d2942f 100644 --- a/networkx/classes/multidigraph.py +++ b/networkx/classes/multidigraph.py @@ -269,6 +269,27 @@ class MultiDiGraph(MultiGraph, DiGraph): to_undirected_class : callable, (default: Graph or MultiGraph) Class to create a new graph structure in the `to_undirected` method. If `None`, a NetworkX class (Graph or MultiGraph) is used. + + **Subclassing Example** + + Create a low memory graph class that effectively disallows edge + attributes by using a single attribute dict for all edges. + This reduces the memory used, but you lose edge attributes. + + >>> class ThinGraph(nx.Graph): + ... all_edge_dict = {"weight": 1} + ... + ... def single_edge_dict(self): + ... return self.all_edge_dict + ... + ... edge_attr_dict_factory = single_edge_dict + >>> G = ThinGraph() + >>> G.add_edge(2, 1) + >>> G[2][1] + {'weight': 1} + >>> G.add_edge(2, 2) + >>> G[2][1] is G[2][2] + True """ # node_dict_factory = dict # already assigned in Graph diff --git a/networkx/classes/multigraph.py b/networkx/classes/multigraph.py index 308c20cc..3d9e69b3 100644 --- a/networkx/classes/multigraph.py +++ b/networkx/classes/multigraph.py @@ -262,6 +262,27 @@ class MultiGraph(Graph): to_undirected_class : callable, (default: Graph or MultiGraph) Class to create a new graph structure in the `to_undirected` method. If `None`, a NetworkX class (Graph or MultiGraph) is used. + + **Subclassing Example** + + Create a low memory graph class that effectively disallows edge + attributes by using a single attribute dict for all edges. + This reduces the memory used, but you lose edge attributes. + + >>> class ThinGraph(nx.Graph): + ... all_edge_dict = {"weight": 1} + ... + ... def single_edge_dict(self): + ... return self.all_edge_dict + ... + ... edge_attr_dict_factory = single_edge_dict + >>> G = ThinGraph() + >>> G.add_edge(2, 1) + >>> G[2][1] + {'weight': 1} + >>> G.add_edge(2, 2) + >>> G[2][1] is G[2][2] + True """ # node_dict_factory = dict # already assigned in Graph |