Skip to content

Commit

Permalink
[timestamps] Move method validate to constructor & update test
Browse files Browse the repository at this point in the history
  • Loading branch information
moi15moi committed Nov 11, 2023
1 parent 3664dec commit dd921b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
27 changes: 10 additions & 17 deletions pyonfx/timestamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,16 @@ def __init__(
)
self.last_frame_time = last_frame_time

Timestamps.validate(timestamps)
# Validate the timestamps
if len(timestamps) <= 1:
raise ValueError("There must be at least 2 timestamps.")

if any(timestamps[i] > timestamps[i + 1] for i in range(len(timestamps) - 1)):
raise ValueError("Timestamps must be in non-decreasing order.")

if timestamps.count(timestamps[0]) == len(timestamps):
raise ValueError("Timestamps must not be all identical.")

self.timestamps = timestamps

if normalize:
Expand Down Expand Up @@ -516,22 +525,6 @@ def get_timestamps(packets) -> Tuple[Fraction, Fraction, List[int]]:
last_frame_time=last_frame_time,
)

@staticmethod
def validate(timestamps: List[int]) -> None:
"""Verify that the provided timestamps are valid, raising ValueError in case they are not.
Parameters:
timestamps (list of int): A list of [timestamps](https://en.wikipedia.org/wiki/Timestamp) encoded as integers.
"""
if len(timestamps) <= 1:
raise ValueError("There must be at least 2 timestamps.")

if any(timestamps[i] > timestamps[i + 1] for i in range(len(timestamps) - 1)):
raise ValueError("Timestamps must be in non-decreasing order.")

if timestamps.count(timestamps[0]) == len(timestamps):
raise ValueError("Timestamps must not be all identical.")

@staticmethod
def normalize(
timestamps: List[int], last_frame_time: Fraction
Expand Down
6 changes: 3 additions & 3 deletions tests/test_timestamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ def test_from_video_file_invalid_path():

def test_validate():
with pytest.raises(ValueError) as exc_info:
Timestamps.validate([0])
Timestamps(RoundingMethod.FLOOR, [0], last_frame_time=Fraction(0))
assert str(exc_info.value) == "There must be at least 2 timestamps."

with pytest.raises(ValueError) as exc_info:
Timestamps.validate([0, 42, 20])
Timestamps(RoundingMethod.FLOOR, [0, 42, 20], last_frame_time=Fraction(0))
assert str(exc_info.value) == "Timestamps must be in non-decreasing order."

with pytest.raises(ValueError) as exc_info:
Timestamps.validate([20, 20])
Timestamps(RoundingMethod.FLOOR, [20, 20], last_frame_time=Fraction(0))
assert str(exc_info.value) == "Timestamps must not be all identical."


Expand Down

0 comments on commit dd921b9

Please sign in to comment.