Skip to content

Commit

Permalink
first_last for tiers
Browse files Browse the repository at this point in the history
  • Loading branch information
JoFrhwld committed Nov 3, 2023
1 parent 4870488 commit 54f28e3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/aligned_textgrid/mixins/tiermixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,28 @@ class TierMixins:
Attributes:
[]: indexable and iterable
first (SequenceInterval): The first entry in the tier.
last (SequenceInterval): The last entry in the tier.
"""

@property
def first(self):
if hasattr(self, "sequence_list") and len(self.sequence_list) > 0:
return self.sequence_list[0]
if hasattr(self, "sequence_list"):
raise IndexError(f"{type(self).__name__} tier with name"\
f" {self.name} has empty sequence_list")
raise AttributeError(f"{type(self).__name__} is not indexible.")

@property
def last(self):
if hasattr(self, "sequence_list") and len(self.sequence_list) > 0:
return self.sequence_list[-1]
if hasattr(self, "sequence_list"):
raise IndexError(f"{type(self).__name__} tier with name"\
f" {self.name} has empty sequence_list")
raise AttributeError(f"{type(self).__name__} is not indexible.")

def __contains__(self, item):
return item in self.sequence_list

Expand Down
22 changes: 22 additions & 0 deletions tests/test_tiers/test_PointTier.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ def test_return(self):
out_tier = self.seq_point_tier.return_tier()
assert isinstance(out_tier, PointTier)

class TestPointPrecedence:
def test_first_last(self):
point_a = SequencePoint(Point(1, "a"))
point_b = SequencePoint(Point(2, "b"))
point_c = SequencePoint(Point(3, "c"))

tier = SequencePointTier(tier = [point_a, point_b, point_c])

first_point = tier[0]
assert tier.first is first_point

last_point = tier[-1]
assert tier.last is last_point

def test_first_last_error(self):
tier = SequencePointTier()

with pytest.raises(IndexError):
tier.first



class TestPointGroup:
point_a = Point(1, "a")
point_b = Point(2, "b")
Expand Down
21 changes: 21 additions & 0 deletions tests/test_tiers/test_SequenceTier.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,27 @@ def test_sequence_index(self):
for idx, entry in enumerate(tier):
assert entry.tier_index == idx

def test_first_last(self):
tier = SequenceTier(
tier = [
self.interval1,
self.interval2,
self.interval3
]
)

first_interval = tier[0]
assert tier.first is first_interval

last_interval = tier[-1]
assert tier.last is last_interval

def test_first_last_errors(self):
tier = SequenceTier()

with pytest.raises(IndexError):
tier.first

def test_get_tieridx(self):
tier = SequenceTier(
tier = [
Expand Down

0 comments on commit 54f28e3

Please sign in to comment.