summaryrefslogtreecommitdiff
path: root/networkx/classes/graph.py
diff options
context:
space:
mode:
authorDan Schult <dschult@colgate.edu>2022-07-25 15:25:07 -0400
committerGitHub <noreply@github.com>2022-07-25 15:25:07 -0400
commitf99f1a69fea12c0d14ca6edd5efecf73a20003a7 (patch)
tree9bbc5216c14ae7491c73a6ed6d6ec49618575f3d /networkx/classes/graph.py
parente32fd8b0ea027aa81c486e674394827402dd94c2 (diff)
downloadnetworkx-f99f1a69fea12c0d14ca6edd5efecf73a20003a7.tar.gz
Add cache reset for when G._node is changed (#5894)
Diffstat (limited to 'networkx/classes/graph.py')
-rw-r--r--networkx/classes/graph.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/networkx/classes/graph.py b/networkx/classes/graph.py
index ebbc8b53..47d5f81d 100644
--- a/networkx/classes/graph.py
+++ b/networkx/classes/graph.py
@@ -41,6 +41,28 @@ class _CachedPropertyResetterAdj:
del od["adj"]
+class _CachedPropertyResetterNode:
+ """Data Descriptor class for _node that resets ``nodes`` cached_property when needed
+
+ This assumes that the ``cached_property`` ``G.node`` should be reset whenever
+ ``G._node`` is set to a new value.
+
+ This object sits on a class and ensures that any instance of that
+ class clears its cached property "nodes" whenever the underlying
+ instance attribute "_node" is set to a new object. It only affects
+ the set process of the obj._adj attribute. All get/del operations
+ act as they normally would.
+
+ For info on Data Descriptors see: https://docs.python.org/3/howto/descriptor.html
+ """
+
+ def __set__(self, obj, value):
+ od = obj.__dict__
+ od["_node"] = value
+ if "nodes" in od:
+ del od["nodes"]
+
+
class Graph:
"""
Base class for undirected graphs.
@@ -282,6 +304,7 @@ class Graph:
"""
_adj = _CachedPropertyResetterAdj()
+ _node = _CachedPropertyResetterNode()
node_dict_factory = dict
node_attr_dict_factory = dict