Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add __iter__() for TableList to support enumerate() #486

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions camelot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,10 @@ def __len__(self):
def __getitem__(self, idx):
return self._tables[idx]

def __iter__(self):
for t in self._tables:
yield t

@staticmethod
def _format_func(table, f):
return getattr(table, f"to_{f}")
Expand Down
26 changes: 26 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,29 @@ def test_handler_with_pathlib(testdir):
with open(filename, "rb") as f:
handler = PDFHandler(f)
assert handler._get_pages("1") == [1]


def test_table_list_iter():
def _make_table(page, order):
t = Table([], [])
t.page = page
t.order = order
return t

table_list = TableList(
[_make_table(2, 1), _make_table(1, 1), _make_table(3, 4), _make_table(1, 2)]
)
# https://docs.python.org/3.12/library/functions.html#iter
# https://docs.python.org/3.12/library/stdtypes.html#typeiter
iterator_a = iter(table_list)
assert iterator_a is not None
item_a = next(iterator_a)
assert item_a is not None

item_b = table_list.__getitem__(0)
assert item_b is not None

iterator_b = table_list.__iter__()
assert iterator_b is not None
item_c = next(iterator_b)
assert item_c is not None