Skip to content

Commit dc6e4d5

Browse files
Add support to check equality of AnyVector
Signed-off-by: Jean-Christophe Morin <[email protected]>
1 parent c9e05d6 commit dc6e4d5

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/py-opentimelineio/opentimelineio/core/_core_utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,41 @@ def insert(self, index, item):
302302
if conversion_func else item
303303
)
304304

305+
def __le__(self, other): # Taken from collections.abc.Set
306+
if not isinstance(other, collections.abc.Sequence):
307+
return NotImplemented
308+
if len(self) > len(other):
309+
return False
310+
for elem in self:
311+
if elem not in other:
312+
return False
313+
return True
314+
315+
def __lt__(self, other): # Taken from collections.abc.Set
316+
if not isinstance(other, collections.abc.Sequence):
317+
return NotImplemented
318+
return len(self) < len(other) and self.__le__(other)
319+
320+
def __gt__(self, other): # Taken from collections.abc.Set
321+
if not isinstance(other, collections.abc.Sequence):
322+
return NotImplemented
323+
return len(self) > len(other) and self.__ge__(other)
324+
325+
def __ge__(self, other): # Taken from collections.abc.Set
326+
if not isinstance(other, collections.abc.Sequence):
327+
return NotImplemented
328+
if len(self) < len(other):
329+
return False
330+
for elem in other:
331+
if elem not in self:
332+
return False
333+
return True
334+
335+
def __eq__(self, other): # Taken from collections.abc.Set
336+
if not isinstance(other, collections.abc.Sequence):
337+
return NotImplemented
338+
return len(self) == len(other) and self.__le__(other)
339+
305340
collections.abc.MutableSequence.register(sequenceClass)
306341
sequenceClass.__radd__ = __radd__
307342
sequenceClass.__add__ = __add__
@@ -311,6 +346,11 @@ def insert(self, index, item):
311346
sequenceClass.insert = insert
312347
sequenceClass.__str__ = __str__
313348
sequenceClass.__repr__ = __repr__
349+
sequenceClass.__le__ = __le__
350+
sequenceClass.__lt__ = __lt__
351+
sequenceClass.__gt__ = __gt__
352+
sequenceClass.__ge__ = __ge__
353+
sequenceClass.__eq__ = __eq__
314354

315355
seen = set()
316356
for klass in (collections.abc.MutableSequence, collections.abc.Sequence):

tests/test_serializable_object.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ def test_cons2(self):
7070
self.assertEqual(so.metadata['string'], 'myvalue')
7171
self.assertEqual(so.metadata['int'], -999999999999)
7272
self.assertIsInstance(so.metadata['list'], opentimelineio._otio.AnyVector)
73-
self.assertEqual(list(so.metadata['list']), [1, 2.5, 'asd']) # AnyVector. Is this right?
73+
self.assertEqual(so.metadata['list'], opentimelineio._otio.AnyVector([1, 2.5, 'asd']))
7474
self.assertIsInstance(so.metadata['dict'], opentimelineio._otio.AnyDictionary)
75-
# self.assertEqual(so.metadata['dict'], opentimelineio._otio.AnyDictionary({'map1': [345]}))
75+
self.assertIsInstance(so.metadata['dict']['map1'], opentimelineio._otio.AnyVector)
76+
self.assertEqual(so.metadata['dict'], opentimelineio._otio.AnyDictionary({'map1': [345]}))
7677
self.assertIsInstance(so.metadata['AnyVector'], opentimelineio._otio.AnyVector)
77-
# self.assertEqual(list(so.metadata['AnyVector']), opentimelineio._otio.AnyVector([1, 'inside any vector']))
78+
self.assertEqual(list(so.metadata['AnyVector']), opentimelineio._otio.AnyVector([1, 'inside any vector']))
7879
self.assertIsInstance(so.metadata['AnyDictionary'], opentimelineio._otio.AnyDictionary)
79-
self.assertEqual(dict(so.metadata['AnyDictionary']), {'key_1': 1234, 'key_2': {'asdasdasd': 5.6}})
80+
self.assertEqual(so.metadata['AnyDictionary'], opentimelineio._otio.AnyDictionary({'key_1': 1234, 'key_2': {'asdasdasd': 5.6}}))
8081
self.assertEqual(so.metadata['RationalTime'], opentimelineio.opentime.RationalTime(value=10.0, rate=5.0))
8182
self.assertEqual(so.metadata['TimeRange'], opentimelineio.opentime.TimeRange(
8283
opentimelineio.opentime.RationalTime(value=1.0),

0 commit comments

Comments
 (0)