@@ -180,7 +180,7 @@ def __init__(
180180 if self .graph_exists_in_db :
181181 self .clear_edges = self .clear_edges_override
182182 self .add_node = self .add_node_override
183- # self.add_nodes_from = self.add_nodes_from_override
183+ self .add_nodes_from = self .add_nodes_from_override
184184 self .remove_node = self .remove_node_override
185185 self .reverse = self .reverse_override
186186
@@ -263,9 +263,49 @@ def add_node_override(self, node_for_adding, **attr):
263263
264264 nx ._clear_cache (self )
265265
266- # TODO: Address in separate PR
267- # def add_nodes_from_override(self, nodes_for_adding, **attr):
268- # raise NotImplementedError("Not yet implemented")
266+ def add_nodes_from_override (self , nodes_for_adding , ** attr ):
267+ for n in nodes_for_adding :
268+ try :
269+ newnode = n not in self ._node
270+ newdict = attr
271+ except TypeError :
272+ n , ndict = n
273+ newnode = n not in self ._node
274+ newdict = attr .copy ()
275+ newdict .update (ndict )
276+ if newnode :
277+ if n is None :
278+ raise ValueError ("None cannot be a node" )
279+ self ._succ [n ] = self .adjlist_inner_dict_factory ()
280+ self ._pred [n ] = self .adjlist_inner_dict_factory ()
281+
282+ ######################
283+ # NOTE: monkey patch #
284+ ######################
285+
286+ # Old:
287+ # self._node[n] = self.node_attr_dict_factory()
288+ #
289+ # self._node[n].update(newdict)
290+
291+ # New:
292+ node_attr_dict = self .node_attr_dict_factory ()
293+ node_attr_dict .data = newdict
294+ self ._node [n ] = node_attr_dict
295+
296+ else :
297+
298+ self ._node [n ].update (newdict )
299+
300+ # Reason:
301+ # We can optimize the process of adding a node by creating avoiding
302+ # the creation of a new dictionary and updating it with the attributes.
303+ # Instead, we can create a new node_attr_dict object and set the attributes
304+ # directly. This only makes 1 network call to the database instead of 2.
305+
306+ ###########################
307+
308+ nx ._clear_cache (self )
269309
270310 def remove_node_override (self , n ):
271311 if isinstance (n , (str , int )):
0 commit comments