Skip to content

Commit 6fb4e88

Browse files
committed
💥 replace overloading on remove functions.
1 parent f4c2bb7 commit 6fb4e88

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

pynode_next/graph.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,41 @@ def add(self, element):
6969
core.ax(lambda x: x.dispatch(e._data()))
7070
return e
7171

72+
def remove(self, element):
73+
"""Removes the specified element (either a Node or an Edge object) from the graph."""
74+
elem_type = type(element)
75+
if elem_type == Node:
76+
for edge in element._incident_edges:
77+
self.remove(edge)
78+
79+
del self._nodes[element._id]
80+
core.ax(lambda x: x.node(str(element._id)).remove())
81+
82+
return element
83+
elif elem_type == Edge:
84+
element._source._incident_edges.remove(element)
85+
element._target._incident_edges.remove(element)
86+
87+
self._edges.remove(element)
88+
del self._has_edge_cache[element]
89+
90+
core.ax(
91+
lambda x: x.dispatch(
92+
{"attrs": {"edges": {str(element._internal_id): {"remove": True}}}}
93+
)
94+
)
95+
96+
return element
97+
7298
def add_node(self, id, value=None):
7399
"""Adds a node to the graph using an id."""
74100
if value is None:
75101
value = id
76102
return self.add(Node(id, value))
77103

78104
def remove_node(self, node):
79-
"""Removes the specified node from the graph"""
80-
n = self.node(node)
81-
82-
for edge in n._incident_edges:
83-
self.remove_edge(edge)
84-
85-
del self._nodes[n._id]
86-
core.ax(lambda x: x.node(str(n._id)).remove())
87-
return n
105+
"""Removes the specified node id from the graph"""
106+
return self.remove(self.node(node))
88107

89108
def node(self, node):
90109
"""Returns the node specified"""
@@ -106,32 +125,15 @@ def add_edge(self, source, target, weight="", directed: bool = False):
106125
"""Adds an edge by defining it's relation to other nodes."""
107126
return self.add(Edge(source, target, weight, directed))
108127

109-
@overloaded
110128
def remove_edge(self, nodeA, nodeB, directed: bool = False):
129+
"""Removes the specified edge(s) between two nodes from the graph"""
111130
edges_between = self.edges_between(nodeA, nodeB, directed)
112131
edge_list = []
113132
for edge in edges_between:
114133
edge_list.append(edge_list)
115-
self.remove_edge(edge)
134+
self.remove(edge)
116135
return edge_list
117136

118-
@overloads(remove_edge)
119-
def remove_edge(self, edge: Edge):
120-
"""Removes an edge object from the graph"""
121-
edge._source._incident_edges.remove(edge)
122-
edge._target._incident_edges.remove(edge)
123-
124-
self._edges.remove(edge)
125-
del self._has_edge_cache[edge]
126-
127-
core.ax(
128-
lambda x: x.dispatch(
129-
{"attrs": {"edges": {str(edge._internal_id): {"remove": True}}}}
130-
)
131-
)
132-
133-
return edge
134-
135137
def has_node(self, node):
136138
"""Checks if a node exists in the graph."""
137139
return self.node(node) is not None

0 commit comments

Comments
 (0)