Skip to content

Commit

Permalink
Add __lt__ to Table to allow sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
symroe committed Feb 25, 2019
1 parent 8ea4ec3 commit c019e58
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions camelot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ def __init__(self, cols, rows):
def __repr__(self):
return '<{} shape={}>'.format(self.__class__.__name__, self.shape)

def __lt__(self, other):
if self.page == other.page:
if self.order < other.order:
return True
if self.page < other.page:
return True

@property
def data(self):
"""Returns two-dimensional list of strings in table.
Expand Down
26 changes: 26 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pandas as pd

import camelot
from camelot.core import Table, TableList

from .data import *

Expand Down Expand Up @@ -247,3 +248,28 @@ def test_arabic():
filename = os.path.join(testdir, "tabula/arabic.pdf")
tables = camelot.read_pdf(filename)
assert df.equals(tables[0].df)


def test_table_order():
def _mk_table(page, order):
t = Table([], [])
t.page = page
t.order = order
return t

table_list = TableList(
[_mk_table(2, 1), _mk_table(1, 1), _mk_table(3, 4), _mk_table(1, 2)]
)

assert [(t.page, t.order) for t in sorted(table_list)] == [
(1, 1),
(1, 2),
(2, 1),
(3, 4),
]
assert [(t.page, t.order) for t in sorted(table_list, reverse=True)] == [
(3, 4),
(2, 1),
(1, 2),
(1, 1),
]

0 comments on commit c019e58

Please sign in to comment.