Skip to content

Add __eq__ method to SigMFFile #98

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

Merged
merged 1 commit into from
Jan 10, 2025
Merged
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
11 changes: 11 additions & 0 deletions sigmf/sigmffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ def __init__(self, metadata=None, data_file=None, global_info=None, skip_checksu
def __len__(self):
return self._memmap.shape[0]

def __eq__(self, other):
"""
Define equality between two `SigMFFile`s.

Rely on the checksum value in the metadata to decide whether `data_file` is the same since the path of the
dataset is immaterial to equivalency.
"""
if isinstance(other, SigMFFile):
return self._metadata == other._metadata
return False

def __next__(self):
"""get next batch of samples"""
if self.iter_position < len(self):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_sigmffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def test_checksum(self):
with self.assertRaises(error.SigMFFileError):
_ = SigMFFile(bad_checksum_metadata, self.temp_path_data)

def test_equality(self):
"""Ensure __eq__ working as expected"""
other = SigMFFile(copy.deepcopy(TEST_METADATA))
self.assertEqual(self.sigmf_object, other)
# different after changing any part of metadata
other.add_annotation(start_index=0, metadata={"a": 0})
self.assertNotEqual(self.sigmf_object, other)


class TestAnnotationHandling(unittest.TestCase):
def test_get_annotations_with_index(self):
Expand Down
Loading