Skip to content

Commit 0b722cc

Browse files
committed
new: add_nodes_from_override
1 parent 315c525 commit 0b722cc

File tree

3 files changed

+91
-14
lines changed

3 files changed

+91
-14
lines changed

nx_arangodb/classes/dict/node.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,16 @@ def __getitem__(self, key: str) -> NodeAttrDict:
334334

335335
@key_is_string
336336
def __setitem__(self, key: str, value: NodeAttrDict) -> None:
337-
"""G._node['node/1'] = {'foo': 'bar'}
338-
339-
Not to be confused with:
340-
- G.add_node('node/1', foo='bar')
341-
"""
337+
"""G._node['node/1'] = {'foo': 'bar'}"""
342338
assert isinstance(value, NodeAttrDict)
343339

344340
node_type, node_id = get_node_type_and_id(key, self.default_node_type)
345341

346342
result = doc_insert(self.db, node_type, node_id, value.data)
347343

348-
node_attr_dict = self._create_node_attr_dict(result["_id"], value.data)
344+
node_attr_dict = self._create_node_attr_dict(
345+
result["_id"], {**value.data, **result}
346+
)
349347

350348
self.data[node_id] = node_attr_dict
351349

nx_arangodb/classes/digraph.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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)):

nx_arangodb/classes/graph.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def __init__(
248248
self.clear = self.clear_override
249249
self.clear_edges = self.clear_edges_override
250250
self.add_node = self.add_node_override
251-
# self.add_nodes_from = self.add_nodes_from_override
251+
self.add_nodes_from = self.add_nodes_from_override
252252
self.number_of_edges = self.number_of_edges_override
253253
self.nbunch_iter = self.nbunch_iter_override
254254

@@ -710,9 +710,48 @@ def add_node_override(self, node_for_adding, **attr):
710710

711711
nx._clear_cache(self)
712712

713-
# TODO: Address in separate PR
714-
# def add_nodes_from_override(self, nodes_for_adding, **attr):
715-
# raise NotImplementedError("Not yet implemented")
713+
def add_nodes_from_override(self, nodes_for_adding, **attr):
714+
for n in nodes_for_adding:
715+
try:
716+
newnode = n not in self._node
717+
newdict = attr
718+
except TypeError:
719+
n, ndict = n
720+
newnode = n not in self._node
721+
newdict = attr.copy()
722+
newdict.update(ndict)
723+
if newnode:
724+
if n is None:
725+
raise ValueError("None cannot be a node")
726+
self._adj[n] = self.adjlist_inner_dict_factory()
727+
728+
######################
729+
# NOTE: monkey patch #
730+
######################
731+
732+
# Old:
733+
# self._node[n] = self.node_attr_dict_factory()
734+
#
735+
# self._node[n].update(newdict)
736+
737+
# New:
738+
node_attr_dict = self.node_attr_dict_factory()
739+
node_attr_dict.data = newdict
740+
self._node[n] = node_attr_dict
741+
742+
else:
743+
744+
self._node[n].update(newdict)
745+
746+
# Reason:
747+
# We can optimize the process of adding a node by creating avoiding
748+
# the creation of a new dictionary and updating it with the attributes.
749+
# Instead, we can create a new node_attr_dict object and set the attributes
750+
# directly. This only makes 1 network call to the database instead of 2.
751+
752+
###########################
753+
754+
nx._clear_cache(self)
716755

717756
def number_of_edges_override(self, u=None, v=None):
718757
if u is not None:

0 commit comments

Comments
 (0)