Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding eq method to collection #41

Open
wants to merge 2 commits into
base: pipeline
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pipeline/src/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def has_property(self, name):
if property.name == name:
return True
return False

def __eq__(self, other: Node) -> bool:

for property in self.properties:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also need to check equality self._type_ and self.id (if it is defined, i.e., for LinkedMetadata, but not for EmbeddedMetadata, and if it is not None)

property_other = getattr(other, property.name, None)
property_self = getattr(self, property.name, None)
if property_other != property_self:
return False

return True

def to_jsonld(self, include_empty_properties=True, embed_linked_nodes=True, with_context=True):
"""
Expand Down
12 changes: 12 additions & 0 deletions pipeline/src/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ def __len__(self):

def __iter__(self):
return iter(self.nodes.values())

def __eq__(self, other):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a preliminary check that the len() of both collections are the same? If they're different, we can return False quickly without needing to check all the nodes.


# The current implementation assumes that nodes in both graphs are connected with the same link number.
for node_id in self.nodes:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also need to check that there are no extra nodes in other that are not present in self.

if node_id in other.nodes.keys():
if self.nodes[node_id] != other.nodes[node_id]:
return False
else:
return False

return True

def add(self, *nodes):
"""
Expand Down