Skip to content

Commit b7ab74c

Browse files
author
Philip de Nier
committed
Support timerange arg in TimeRange.__contains__
sem-ver: feature
1 parent 2daaf3e commit b7ab74c

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

mediatimestamp/immutable/timerange.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,13 @@ def finite(self) -> bool:
371371
return (self.start is not None and self.end is not None)
372372

373373
def __contains__(self, ts: object) -> bool:
374-
"""Returns true if the timestamp is within this range."""
375-
return (isinstance(ts, (Timestamp, SupportsMediaTimestamp, int, float)) and
376-
self.contains_subrange(mediatimestamp(ts)))
374+
"""Returns true if the timestamp or timerange is wholly within this range."""
375+
return ((
376+
isinstance(ts, (Timestamp, SupportsMediaTimestamp, int, float)) and
377+
self.contains_subrange(mediatimestamp(ts))
378+
) or (
379+
isinstance(ts, (TimeRange, SupportsMediaTimeRange)) and
380+
self.contains_subrange(ts)))
377381

378382
def __eq__(self, other: object) -> bool:
379383
if not isinstance(other, SupportsMediaTimeRange):

tests/test_timerange.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,17 @@ def test_contains(self):
369369
self.assertNotIn(None, TimeRange.from_str("[0:0]"))
370370
self.assertNotIn(None, TimeRange.never())
371371
self.assertNotIn(None, TimeRange.eternity())
372+
372373
self.assertNotIn(1.0, TimeRange.from_str("[0:0]"))
373374
self.assertIn(1.0, TimeRange.from_str("[1:0]"))
374375
self.assertIn(1.0, TimeRange.from_str("[0:0_10:0)"))
376+
377+
self.assertNotIn(Timestamp.from_str("0:0"), TimeRange.from_str("[1:0_10:0)"))
378+
self.assertIn(Timestamp.from_str("1:0"), TimeRange.from_str("[1:0_10:0)"))
379+
380+
self.assertNotIn(TimeRange.from_str("[0:0_10:0)"), TimeRange.from_str("[0:0_5:0)"))
381+
self.assertIn(TimeRange.from_str("0:0_5:0"), TimeRange.from_str("[0:0_10:0)"))
382+
375383
# The subrange tests will cover the rest
376384

377385
def _check_intersection(self, a, b, c, d):

0 commit comments

Comments
 (0)