Skip to content
Snippets Groups Projects

Add more validation for buffers

Open James Kennington requested to merge feature-validate-buffers into main
2 files
+ 66
3
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 23
3
@@ -585,7 +585,7 @@ class TSFrame(Frame):
def __post_init__(self):
super().__post_init__()
assert len(self.buffers) > 0
self.__sanity_check(self.buffers)
self._validate_buffers(self.buffers)
self.is_gap = all([b.is_gap for b in self.buffers])
def __getitem__(self, item):
@@ -604,7 +604,17 @@ class TSFrame(Frame):
def __len__(self):
return len(self.buffers)
def __sanity_check(self, bufs: list[SeriesBuffer]) -> None:
def _validate_buffers(self, bufs: list[SeriesBuffer]) -> None:
"""Validate the buffers attribute.
Args:
bufs:
list[SeriesBuffer], the buffers to validate
"""
self._validate_buffers_not_mixed_type(bufs)
self._validate_buffers_continuous(bufs)
def _validate_buffers_continuous(self, bufs: list[SeriesBuffer]) -> None:
"""Sanity check that the buffers don't overlap nor have discontinuities.
Args:
@@ -616,9 +626,19 @@ class TSFrame(Frame):
slices = [buf.slice for buf in bufs]
off0 = slices[0].stop
for sl in slices[1:]:
assert off0 == sl.start
if not off0 == sl.start:
raise ValueError(f"Discontinuity between {off0} and {sl.start}")
off0 = sl.stop
def _validate_buffers_not_mixed_type(self, bufs: list[SeriesBuffer]) -> None:
"""Helper method for validating the buffers attribute. A set of buffers
is valid if it does not contain both zero-length and non-zero-length buffers.
"""
has_zero_length = any([len(buf) == 0 for buf in bufs])
has_non_zero_length = any([len(buf) > 0 for buf in bufs])
if (has_zero_length and has_non_zero_length):
raise ValueError("Cannot mix zero-length and non-zero-length buffers")
def set_buffers(self, bufs: list[SeriesBuffer]) -> None:
"""Set the buffers attribute to the bufs provided.
Loading