🐛 fix(base.py): fix add_edge method to prevent adding duplicate edges

🐛 fix(base.py): fix __eq__ method in Edge class to correctly compare objects
The add_edge method in the Vertex class has been modified to prevent adding duplicate edges. Now, before adding an edge, it checks if the edge already exists in the edges list to avoid duplicates.

The __eq__ method in the Edge class has been fixed to correctly compare objects. It now compares the string representation of the objects to determine equality, ensuring that the comparison is accurate.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-07-05 09:37:25 -03:00
commit 3059e8c670
2 changed files with 14 additions and 3 deletions

View file

@ -48,6 +48,16 @@ class Edge:
def __repr__(self) -> str:
return (
f"Edge(source={self.source.id}, target={self.target.id}, valid={self.valid}"
f"Edge(source={self.source.id}, target={self.target.id}, target_param={self.target_param}"
f", matched_type={self.matched_type})"
)
def __hash__(self) -> int:
return hash(self.__repr__())
def __eq__(self, __value: object) -> bool:
return (
self.__repr__() == __value.__repr__()
if isinstance(__value, Edge)
else False
)

View file

@ -95,7 +95,7 @@ class Vertex:
if param_key not in params:
params[param_key] = []
params[param_key].append(edge.source)
else:
elif edge.target.id == self.id:
params[param_key] = edge.source
for key, value in template_dict.items():
@ -204,7 +204,8 @@ class Vertex:
return self._built_object
def add_edge(self, edge: "Edge") -> None:
self.edges.append(edge)
if edge not in self.edges:
self.edges.append(edge)
def __repr__(self) -> str:
return f"Node(id={self.id}, data={self.data})"