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

Add equality operators to AnyVector in Python #1575

Closed
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
40 changes: 40 additions & 0 deletions src/py-opentimelineio/opentimelineio/core/_core_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,41 @@ def insert(self, index, item):
if conversion_func else item
)

def __le__(self, other): # Taken from collections.abc.Set
if not isinstance(other, collections.abc.Sequence):
return NotImplemented
if len(self) > len(other):
return False
for elem in self:
if elem not in other:
return False
return True

def __lt__(self, other): # Taken from collections.abc.Set
if not isinstance(other, collections.abc.Sequence):
return NotImplemented
return len(self) < len(other) and self.__le__(other)

def __gt__(self, other): # Taken from collections.abc.Set
if not isinstance(other, collections.abc.Sequence):
return NotImplemented
return len(self) > len(other) and self.__ge__(other)

def __ge__(self, other): # Taken from collections.abc.Set
if not isinstance(other, collections.abc.Sequence):
return NotImplemented
if len(self) < len(other):
return False
for elem in other:
if elem not in self:
return False
return True

def __eq__(self, other): # Taken from collections.abc.Set
if not isinstance(other, collections.abc.Sequence):
return NotImplemented
return len(self) == len(other) and self.__le__(other)

collections.abc.MutableSequence.register(sequenceClass)
sequenceClass.__radd__ = __radd__
sequenceClass.__add__ = __add__
Expand All @@ -314,6 +349,11 @@ def insert(self, index, item):
sequenceClass.insert = insert
sequenceClass.__str__ = __str__
sequenceClass.__repr__ = __repr__
sequenceClass.__le__ = __le__
sequenceClass.__lt__ = __lt__
sequenceClass.__gt__ = __gt__
sequenceClass.__ge__ = __ge__
sequenceClass.__eq__ = __eq__

seen = set()
for klass in (collections.abc.MutableSequence, collections.abc.Sequence):
Expand Down
18 changes: 8 additions & 10 deletions tests/test_core_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ def test_main(self):
v.append(2)
self.assertEqual(len(v), 2)

self.assertEqual([value for value in v], [1, 2])
self.assertEqual(v, [1, 2])

v.insert(0, 5)
self.assertEqual([value for value in v], [5, 1, 2])
self.assertEqual(v, [5, 1, 2])
self.assertEqual(v[0], 5)
self.assertEqual(v[-3], 5)

Expand All @@ -124,13 +124,11 @@ def test_main(self):

del v[0]
self.assertEqual(len(v), 2)
# Doesn't work...
# assert v == [1, 100]
self.assertEqual([value for value in v], [1, 100])
self.assertEqual(v, [1, 100])

del v[1000] # This will surprisingly delete the last item...
self.assertEqual(len(v), 1)
self.assertEqual([value for value in v], [1])
self.assertEqual(v, [1])

# Will delete the last item even if the index doesn't match.
# It's a surprising behavior.
Expand All @@ -144,7 +142,7 @@ def test_main(self):
items.append(value)

self.assertEqual(items, [1, '234', {}])
self.assertFalse(v == [1, '234', {}]) # __eq__ is not implemented
self.assertTrue(v == [1, '234', {}])

self.assertTrue(1 in v) # Test __contains__
self.assertTrue('234' in v)
Expand Down Expand Up @@ -181,13 +179,13 @@ def test_main(self):
self.assertEqual(v3[1:7:2], [1, 3, 5])

del v3[2:7]
self.assertEqual(list(v3), [0, 1, 7, 8, 9])
self.assertEqual(v3, [0, 1, 7, 8, 9])

v4 = opentimelineio.core._core_utils.AnyVector()
v4.extend(range(10))

del v4[::2]
self.assertEqual(list(v4), [1, 3, 5, 7, 9])
self.assertEqual(v4, [1, 3, 5, 7, 9])

v5 = opentimelineio.core._core_utils.AnyVector()
tmplist = [1, 2]
Expand Down Expand Up @@ -225,7 +223,7 @@ def test_raises_if_ref_destroyed(self):
def test_copy(self):
list1 = [1, 2, [3, 4], 5]
copied = copy.copy(list1)
self.assertEqual(list(list1), list(copied))
self.assertEqual(list1, copied)

v = opentimelineio.core._core_utils.AnyVector()
v.extend([1, 2, [3, 4], 5])
Expand Down