Skip to content

Commit f79e7f2

Browse files
author
Steve Canny
committed
tbl: add _RowCollection magic methods
1 parent a03a35d commit f79e7f2

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

docx/table.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,20 @@ class _RowCollection(object):
6767
def __init__(self, tbl):
6868
super(_RowCollection, self).__init__()
6969
self._tbl = tbl
70+
71+
def __getitem__(self, idx):
72+
"""
73+
Provide indexed access, (e.g. 'rows[0]')
74+
"""
75+
try:
76+
tr = self._tbl.tr_lst[idx]
77+
except IndexError:
78+
msg = "row index [%d] out of range" % idx
79+
raise IndexError(msg)
80+
return _Row(tr)
81+
82+
def __iter__(self):
83+
return iter([_Row(tr) for tr in self._tbl.tr_lst])
84+
85+
def __len__(self):
86+
return len(self._tbl.tr_lst)

features/steps/table.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ def given_a_table_having_two_rows(context):
2525

2626
# then =====================================================
2727

28+
@then('I can access the rows by index')
29+
def then_can_access_rows_by_index(context):
30+
rows = context.table_.rows
31+
row_count = len(rows)
32+
for idx in range(row_count):
33+
row = rows[idx]
34+
assert isinstance(row, _Row)
35+
36+
2837
@then('the length of its row collection is 2')
2938
def then_len_of_row_collection_is_2(context):
3039
rows = context.table_.rows

features/tbl-item-access.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ Feature: Access table rows, columns, and cells
77
Given a table having two rows
88
Then the length of its row collection is 2
99
And each item in its row collection is a table row
10+
11+
Scenario: Access table row by collection index
12+
Given a table having two rows
13+
Then I can access the rows by index

tests/test_table.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,46 @@ def row_access_fixture(self):
5656
return table
5757

5858

59+
class Describe_RowCollection(object):
60+
61+
def it_contains__Row_instances(self, row_count_fixture):
62+
table, row_count = row_count_fixture
63+
actual_count = 0
64+
for row in table.rows:
65+
assert isinstance(row, _Row)
66+
actual_count += 1
67+
assert actual_count == row_count
68+
69+
def it_knows_how_many_rows_it_contains(self, row_count_fixture):
70+
table, row_count = row_count_fixture
71+
rows = table.rows
72+
assert len(rows) == row_count
73+
74+
def it_provides_indexed_access_to_rows(self, row_count_fixture):
75+
table, row_count = row_count_fixture
76+
for idx in range(-row_count, row_count):
77+
row = table.rows[idx]
78+
assert isinstance(row, _Row)
79+
80+
def it_raises_on_indexed_access_out_of_range(self, row_count_fixture):
81+
table, row_count = row_count_fixture
82+
with pytest.raises(IndexError):
83+
too_low = -1 - row_count
84+
table.rows[too_low]
85+
with pytest.raises(IndexError):
86+
too_high = row_count
87+
table.rows[too_high]
88+
89+
# fixtures -------------------------------------------------------
90+
91+
@pytest.fixture
92+
def row_count_fixture(self):
93+
row_count = 2
94+
tbl = _tbl_bldr(rows=row_count, cols=2).element
95+
table = Table(tbl)
96+
return table, row_count
97+
98+
5999
# fixtures -----------------------------------------------------------
60100

61101
def _tbl_bldr(rows, cols):

0 commit comments

Comments
 (0)