Skip to content

Commit 57516ae

Browse files
author
Steve Canny
committed
tbl: add _ColumnCellCollection magic methods
1 parent 8ba64d4 commit 57516ae

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed

docx/table.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,35 @@ def __init__(self, tbl, gridCol):
9191
self._tbl = tbl
9292
self._gridCol = gridCol
9393

94+
def __getitem__(self, idx):
95+
"""
96+
Provide indexed access, (e.g. 'cells[0]')
97+
"""
98+
try:
99+
tr = self._tr_lst[idx]
100+
except IndexError:
101+
msg = "cell index [%d] is out of range" % idx
102+
raise IndexError(msg)
103+
tc = tr.tc_lst[self._col_idx]
104+
return _Cell(tc)
105+
106+
def __iter__(self):
107+
for tr in self._tr_lst:
108+
tc = tr.tc_lst[self._col_idx]
109+
yield _Cell(tc)
110+
111+
def __len__(self):
112+
return len(self._tr_lst)
113+
114+
@property
115+
def _col_idx(self):
116+
gridCol_lst = self._tbl.tblGrid.gridCol_lst
117+
return gridCol_lst.index(self._gridCol)
118+
119+
@property
120+
def _tr_lst(self):
121+
return self._tbl.tr_lst
122+
94123

95124
class _ColumnCollection(object):
96125
"""

features/tbl-item-access.feature

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Feature: Access table rows, columns, and cells
2323
Then I can iterate over the column collection
2424
And I can access a collection column by index
2525

26-
@wip
2726
Scenario: Access cell collection of table column
2827
Given a table column having two cells
2928
Then I can access the cell collection of the column
@@ -34,7 +33,6 @@ Feature: Access table rows, columns, and cells
3433
Then I can access the cell collection of the row
3534
And I can get the length of the row cell collection
3635

37-
@wip
3836
Scenario: Access cell in column cell collection
3937
Given a column cell collection having two cells
4038
Then I can iterate over the column cells

tests/test_table.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,46 @@ def column(self):
8484
return _Column(None, None)
8585

8686

87+
class Describe_ColumnCellCollection(object):
88+
89+
def it_knows_how_many_cells_it_contains(self, cells_fixture):
90+
cells, cell_count = cells_fixture
91+
assert len(cells) == cell_count
92+
93+
def it_can_iterate_over_its__Cell_instances(self, cells_fixture):
94+
cells, cell_count = cells_fixture
95+
actual_count = 0
96+
for cell in cells:
97+
assert isinstance(cell, _Cell)
98+
actual_count += 1
99+
assert actual_count == cell_count
100+
101+
def it_provides_indexed_access_to_cells(self, cells_fixture):
102+
cells, cell_count = cells_fixture
103+
for idx in range(-cell_count, cell_count):
104+
cell = cells[idx]
105+
assert isinstance(cell, _Cell)
106+
107+
def it_raises_on_indexed_access_out_of_range(self, cells_fixture):
108+
cells, cell_count = cells_fixture
109+
too_low = -1 - cell_count
110+
too_high = cell_count
111+
with pytest.raises(IndexError):
112+
cells[too_low]
113+
with pytest.raises(IndexError):
114+
cells[too_high]
115+
116+
# fixtures -------------------------------------------------------
117+
118+
@pytest.fixture
119+
def cells_fixture(self):
120+
cell_count = 2
121+
tbl = _tbl_bldr(rows=cell_count, cols=1).element
122+
gridCol = tbl.tblGrid.gridCol_lst[0]
123+
cells = _ColumnCellCollection(tbl, gridCol)
124+
return cells, cell_count
125+
126+
87127
class Describe_ColumnCollection(object):
88128

89129
def it_knows_how_many_columns_it_contains(self, columns_fixture):
@@ -141,6 +181,10 @@ def cells_access_fixture(self):
141181

142182
class Describe_RowCellCollection(object):
143183

184+
def it_knows_how_many_cells_it_contains(self, cell_count_fixture):
185+
cells, cell_count = cell_count_fixture
186+
assert len(cells) == cell_count
187+
144188
def it_can_iterate_over_its__Cell_instances(self, cell_count_fixture):
145189
cells, cell_count = cell_count_fixture
146190
actual_count = 0
@@ -149,10 +193,6 @@ def it_can_iterate_over_its__Cell_instances(self, cell_count_fixture):
149193
actual_count += 1
150194
assert actual_count == cell_count
151195

152-
def it_knows_how_many_cells_it_contains(self, cell_count_fixture):
153-
cells, cell_count = cell_count_fixture
154-
assert len(cells) == cell_count
155-
156196
def it_provides_indexed_access_to_cells(self, cell_count_fixture):
157197
cells, cell_count = cell_count_fixture
158198
for idx in range(-cell_count, cell_count):

0 commit comments

Comments
 (0)