Skip to content

Commit

Permalink
better exceptions for .first, .last
Browse files Browse the repository at this point in the history
  • Loading branch information
JoFrhwld committed Nov 3, 2023
1 parent cc6863f commit 4870488
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/aligned_textgrid/mixins/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ class PrecedenceMixins:
Attributes:
first (SequenceInterval): The first interval in the subset list
last (SequenceInterval): The last interval in the subset list
last (SequenceInterval): The last interval in the subset list
"""

@property
def first(self):
if hasattr(self, "subset_list"):
if hasattr(self, "subset_list") and len(self.subset_list) > 0:
return self.subset_list[0]
else:
raise Exception(f"{type(self).__name__} does not\
have the attribute .first")

if hasattr(self, "subset_list"):
raise IndexError(f"{type(self).__name__} with label "\
f"'{self.label}' subset list is empty.")
raise AttributeError(f"{type(self).__name__} is not indexable.")

@property
def last(self):
if hasattr(self, "subset_list"):
if hasattr(self, "subset_list") and len(self.subset_list) > 0:
return self.subset_list[-1]
else:
raise Exception(f"{type(self).__name__} does not\
have the attribute .first")
if hasattr(self, "subset_list"):
raise IndexError(f"{type(self).__name__} with label "\
f"'{self.label}' subset list is empty.")
raise AttributeError(f"{type(self).__name__} is not indexable.")

def set_fol(
self, next_int):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_sequences/test_sequences.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pytest
from aligned_textgrid.sequences.sequences import *
from aligned_textgrid.points.points import SequencePoint
from aligned_textgrid.sequences.tiers import *
import numpy as np
from praatio.utilities.constants import Interval
from praatio.utilities.constants import Point

class TestSequenceIntervalDefault:
"""_Test default behavior of SequenceInterval_
Expand Down Expand Up @@ -295,6 +297,23 @@ def test_first_last(self):
last_interval = upper1[-1]
assert upper1.last is last_interval

def test_first_last_errors(self):
upper1 = self.UpperClass(Interval(0,10,"upper"))
point1 = SequencePoint(Point(0, "point"))

with pytest.raises(IndexError):
upper1.first

with pytest.raises(IndexError):
upper1.last

with pytest.raises(AttributeError):
point1.first

with pytest.raises(AttributeError):
point1.last


def test_subset_pop(self):
upper1 = self.UpperClass(Interval(0,10,"upper"))
lower1 = self.LowerClass(Interval(0,5,"lower1"))
Expand Down

0 comments on commit 4870488

Please sign in to comment.