Skip to content

Commit

Permalink
add iteration to Base
Browse files Browse the repository at this point in the history
  • Loading branch information
TomDonoghue committed Feb 25, 2021
1 parent a614b88 commit e598e94
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lisc/objects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def __getitem__(self, key):
return self.get_term(self.get_index(key) if isinstance(key, str) else key)


def __iter__(self):
"""Allow for iterating across the object by stepping through terms."""

for ind in range(self.n_terms):
yield self.get_term(ind)


@property
def has_terms(self):
"""Indicator for if the object has terms."""
Expand Down
5 changes: 5 additions & 0 deletions lisc/tests/objects/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def test_get_item(tbase_terms):
assert isinstance(out, Term)
assert out.label == 'label0'

def test_iter(tbase_terms):

for term in tbase_terms:
assert isinstance(term, Term)

def test_get_index(tbase_terms):

ind = tbase_terms.get_index('label0')
Expand Down

0 comments on commit e598e94

Please sign in to comment.