Skip to content

Commit 3319e60

Browse files
author
Philip de Nier
authored
Merge pull request #42 from bbc/philipn-norm-range
Version 1.7.2: Normalise timerange never and eternity
2 parents 70d5e19 + 921eae7 commit 3319e60

4 files changed

Lines changed: 35 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# mediatimestamp Changelog
22

3+
## 1.7.3
4+
- Normalise time ranges where start > end to equal TimeRange.never().
5+
- Normalise inclusivity for unbounded time ranges to equal TimeRange.eternity().
6+
37
## 1.7.2
48
- Require Python version 3.6 rather than 3.
59

mediatimestamp/immutable.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,17 @@ def __init__(self, start, end, inclusivity=INCLUSIVE):
720720
:param start: A Timestamp or None
721721
:param end: A Timestamp or None
722722
:param inclusivity: a combination of flags INCLUDE_START and INCLUDE_END"""
723+
# Normalise the 'never' cases
724+
if start is not None and end is not None:
725+
if start > end or (start == end and inclusivity != TimeRange.INCLUSIVE):
726+
start = Timestamp()
727+
end = Timestamp()
728+
inclusivity = TimeRange.EXCLUSIVE
729+
730+
# Normalise the 'eternity' cases
731+
if start is None and end is None:
732+
inclusivity = TimeRange.INCLUSIVE
733+
723734
super(TimeRange, self).__init__(start, end, inclusivity)
724735

725736
def __setattr__(self, name, value):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
# Basic metadata
2020
name = 'mediatimestamp'
21-
version = '1.7.2'
21+
version = '1.7.3'
2222
description = 'A timestamp library for high precision nanosecond timestamps'
2323
url = 'https://github.com/bbc/rd-apmm-python-lib-mediatimestamp'
2424
author = 'James P. Weaver'

tests/test_immutable.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,3 +1599,22 @@ def test_union_raises(self):
15991599
with self.subTest(first=first, second=second):
16001600
with self.assertRaises(ValueError):
16011601
first.union_with_timerange(second)
1602+
1603+
def test_never_normalise(self):
1604+
"""Check 'never' (empty) normalisation"""
1605+
test_data = [
1606+
TimeRange.from_str("[100:0_0:0]"),
1607+
TimeRange.from_str("[10:0_10:0)"),
1608+
TimeRange.from_str("(10:0_10:0]"),
1609+
]
1610+
1611+
for tr in test_data:
1612+
with self.subTest(tr=tr):
1613+
self.assertEqual(tr.start, TimeRange.never().start)
1614+
self.assertEqual(tr.end, TimeRange.never().end)
1615+
self.assertEqual(tr.inclusivity, TimeRange.never().inclusivity)
1616+
1617+
def test_eternity_normalise(self):
1618+
"""Check 'eternity' normalisation"""
1619+
tr = TimeRange(None, None, TimeRange.EXCLUSIVE)
1620+
self.assertEqual(tr.inclusivity, TimeRange.INCLUSIVE)

0 commit comments

Comments
 (0)