55"""Stores the GraphContainer class"""
66
77import dataclasses
8+ import warnings
89from dataclasses import dataclass
910from typing import TYPE_CHECKING , Generator
1011
@@ -56,47 +57,68 @@ def empty(cls, graph_model: type[BaseGraphModel] = RustworkxGraphModel) -> "Grap
5657 complete_graph = graph_model (active_only = False ),
5758 )
5859
60+ def add_node_array (self , node_array : NodeArray ) -> None :
61+ """Add a node to all graphs"""
62+ for field in dataclasses .fields (self ):
63+ graph = getattr (self , field .name )
64+ graph .add_node_array (node_array = node_array , raise_on_fail = False )
65+
66+ def add_node (self , node : NodeArray ) -> None :
67+ """Add a node to all graphs"""
68+ warnings .warn (
69+ "add_node is deprecated and will be removed in a future release, use add_node_array instead" ,
70+ category = DeprecationWarning ,
71+ stacklevel = 2 ,
72+ )
73+ self .add_node_array (node_array = node )
74+
75+ def add_branch_array (self , branch_array : BranchArray ) -> None :
76+ """Add a branch to all graphs"""
77+ for field in self .graph_attributes :
78+ graph = getattr (self , field .name )
79+ graph .add_branch_array (branch_array = branch_array )
80+
5981 def add_branch (self , branch : BranchArray ) -> None :
82+ """Add a branch to all graphs"""
83+ warnings .warn (
84+ "add_branch is deprecated and will be removed in a future release, use add_branch_array instead" ,
85+ category = DeprecationWarning ,
86+ stacklevel = 2 ,
87+ )
88+ self .add_branch_array (branch_array = branch )
89+
90+ def add_branch3_array (self , branch3_array : Branch3Array ) -> None :
6091 """Add a branch to all graphs"""
6192 for field in self .graph_attributes :
6293 graph = getattr (self , field .name )
63- graph .add_branch_array (branch_array = branch )
64- setattr (self , field .name , graph )
94+ graph .add_branch3_array (branch3_array = branch3_array )
6595
6696 def add_branch3 (self , branch : Branch3Array ) -> None :
6797 """Add a branch to all graphs"""
68- for field in self .graph_attributes :
98+ warnings .warn (
99+ "add_branch3 is deprecated and will be removed in a future release, use add_branch3_array instead" ,
100+ category = DeprecationWarning ,
101+ stacklevel = 2 ,
102+ )
103+ self .add_branch3_array (branch3_array = branch )
104+
105+ def delete_node (self , node : NodeArray ) -> None :
106+ """Remove a node from all graphs"""
107+ for field in dataclasses .fields (self ):
69108 graph = getattr (self , field .name )
70- graph .add_branch3_array (branch3_array = branch )
71- setattr (self , field .name , graph )
109+ graph .delete_node_array (node_array = node )
72110
73111 def delete_branch (self , branch : BranchArray ) -> None :
74112 """Remove a branch from all graphs"""
75113 for field in self .graph_attributes :
76114 graph = getattr (self , field .name )
77115 graph .delete_branch_array (branch_array = branch )
78- setattr (self , field .name , graph )
79116
80117 def delete_branch3 (self , branch : Branch3Array ) -> None :
81118 """Remove a branch from all graphs"""
82119 for field in self .graph_attributes :
83120 graph = getattr (self , field .name )
84121 graph .delete_branch3_array (branch3_array = branch )
85- setattr (self , field .name , graph )
86-
87- def add_node (self , node : NodeArray ) -> None :
88- """Add a node to all graphs"""
89- for field in dataclasses .fields (self ):
90- graph = getattr (self , field .name )
91- graph .add_node_array (node_array = node , raise_on_fail = False )
92- setattr (self , field .name , graph )
93-
94- def delete_node (self , node : NodeArray ) -> None :
95- """Remove a node from all graphs"""
96- for field in dataclasses .fields (self ):
97- graph = getattr (self , field .name )
98- graph .delete_node_array (node_array = node )
99- setattr (self , field .name , graph )
100122
101123 def make_active (self , branch : BranchArray ) -> None :
102124 """Add branch to all active_only graphs"""
@@ -107,7 +129,6 @@ def make_active(self, branch: BranchArray) -> None:
107129 graph = getattr (self , field .name )
108130 if graph .active_only :
109131 graph .add_branch (from_ext_node_id = from_node , to_ext_node_id = to_node )
110- setattr (self , field .name , graph )
111132
112133 def make_inactive (self , branch : BranchArray ) -> None :
113134 """Remove a branch from all active_only graphs"""
@@ -118,7 +139,6 @@ def make_inactive(self, branch: BranchArray) -> None:
118139 graph = getattr (self , field .name )
119140 if graph .active_only :
120141 graph .delete_branch (from_ext_node_id = from_node , to_ext_node_id = to_node )
121- setattr (self , field .name , graph )
122142
123143 @classmethod
124144 def from_arrays (cls , arrays : "Grid" ) -> "GraphContainer" :
@@ -142,9 +162,9 @@ def _validate_branches(arrays: "Grid") -> None:
142162 raise RecordDoesNotExist (f"Found invalid .to_node values in { array .__class__ .__name__ } " )
143163
144164 def _append (self , array : FancyArray ) -> None :
165+ if isinstance (array , NodeArray ):
166+ self .add_node_array (array )
145167 if isinstance (array , BranchArray ):
146- self .add_branch (array )
168+ self .add_branch_array (array )
147169 if isinstance (array , Branch3Array ):
148- self .add_branch3 (array )
149- if isinstance (array , NodeArray ):
150- self .add_node (array )
170+ self .add_branch3_array (array )
0 commit comments