Skip to content

Commit

Permalink
Merge pull request cytoscape#214 from sven5s/remove_duplicated_code
Browse files Browse the repository at this point in the history
combine add_node with add_nodes and add_egde with add_edges
  • Loading branch information
ianhi authored Jan 4, 2021
2 parents 8c86466 + f06dc97 commit 43cde47
Showing 1 changed file with 8 additions and 47 deletions.
55 changes: 8 additions & 47 deletions ipycytoscape/cytoscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ def add_node(self, node):
----------
node : ipycytoscape.Node
"""
if node.data["id"] not in self._adj:
self._adj[node.data["id"]] = dict()
self.nodes.append(node)
self.add_nodes([node])

def add_nodes(self, nodes):
"""
Expand Down Expand Up @@ -314,50 +312,7 @@ def add_edge(self, edge, directed=False, multiple_edges=False):
directed : bool
multiple_edges : bool
"""
source, target = edge.data["source"], edge.data["target"]

if directed and "directed" not in edge.classes:
edge.classes += " directed "
if multiple_edges and "multiple_edges" not in edge.classes:
edge.classes += " multiple_edges "

# If multiple edges are allowed, it's okay to add more edges between the source and target
if multiple_edges:
new_edge = True
# Check to see if the edge source -> target exists in the graph (don't add it again)
elif source in self._adj and target in self._adj[source]:
new_edge = False
# Check to see if the edge target-> source exists in an undirected graph (don't add it again)
elif not directed and target in self._adj and source in self._adj[target]:
new_edge = False
# If the edge doesn't exist already
else:
new_edge = True

if new_edge: # if the edge is not present in the graph
self.edges.append(edge)
if source not in self._adj:
node_instance = Node()
# setting the id, according to current spec should be only int/str
node_instance.data = {"id": source}
self.add_node(node_instance)
if target not in self._adj:
node_instance = Node()
# setting the id, according to current spec should be only int/str
node_instance.data = {"id": target}
self.add_node(node_instance)

if multiple_edges and target in self._adj[source]:
self._adj[source][target] += 1
else:
self._adj[source][target] = 1
if not (directed or "directed" in edge.classes):
if multiple_edges and source in self._adj[target]:
self._adj[target][source] += 1
else:
self._adj[target][source] = 1
else: # Don't add a new edge
pass
self.add_edges([edge], directed=directed, multiple_edges=multiple_edges)

def add_edges(self, edges, directed=False, multiple_edges=False):
"""
Expand All @@ -374,6 +329,12 @@ def add_edges(self, edges, directed=False, multiple_edges=False):
edge_list = list()
for edge in edges:
source, target = edge.data["source"], edge.data["target"]

if directed and "directed" not in edge.classes:
edge.classes += " directed "
if multiple_edges and "multiple_edges" not in edge.classes:
edge.classes += " multiple_edges "

# If multiple edges are allowed, it's okay to add more edges between the source and target
if multiple_edges:
new_edge = True
Expand Down

0 comments on commit 43cde47

Please sign in to comment.