Skip to content

Commit ce4ab90

Browse files
author
Steve Canny
committed
tbl: refactor Table.add_row() to _RowColl.add()
1 parent d28e053 commit ce4ab90

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

docx/parts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def add_table(self, rows, cols):
6565
for i in range(cols):
6666
table.columns.add()
6767
for i in range(rows):
68-
table.add_row()
68+
table.rows.add()
6969
return table
7070

7171
def clear_content(self):

docx/table.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ def __init__(self, tbl):
1717
super(Table, self).__init__()
1818
self._tbl = tbl
1919

20-
def add_row(self):
21-
"""
22-
Return a |_Row| instance, newly added bottom-most to the table.
23-
"""
24-
tbl = self._tbl
25-
tr = tbl.add_tr()
26-
for gridCol in tbl.tblGrid.gridCol_lst:
27-
tr.add_tc()
28-
return _Row(tr)
29-
3020
def cell(self, row_idx, col_idx):
3121
"""
3222
Return |_Cell| instance correponding to table cell at *row_idx*,
@@ -222,3 +212,13 @@ def __iter__(self):
222212

223213
def __len__(self):
224214
return len(self._tbl.tr_lst)
215+
216+
def add(self):
217+
"""
218+
Return a |_Row| instance, newly added bottom-most to the table.
219+
"""
220+
tbl = self._tbl
221+
tr = tbl.add_tr()
222+
for gridCol in tbl.tblGrid.gridCol_lst:
223+
tr.add_tc()
224+
return _Row(tr)

tests/test_table.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,8 @@ def it_provides_access_to_a_cell_by_row_and_col_indices(self, table):
3636
tc = tr.tc_lst[col_idx]
3737
assert tc is cell._tc
3838

39-
def it_can_add_a_row(self, add_row_fixture):
40-
table, expected_xml = add_row_fixture
41-
row = table.add_row()
42-
assert table._tbl.xml == expected_xml
43-
assert isinstance(row, _Row)
44-
4539
# fixtures -------------------------------------------------------
4640

47-
@pytest.fixture
48-
def add_row_fixture(self):
49-
tbl = _tbl_bldr(rows=1, cols=2).element
50-
table = Table(tbl)
51-
expected_xml = _tbl_bldr(rows=2, cols=2).xml()
52-
return table, expected_xml
53-
5441
@pytest.fixture
5542
def table(self):
5643
tbl = _tbl_bldr(rows=2, cols=2).element
@@ -250,8 +237,21 @@ def it_raises_on_indexed_access_out_of_range(self, rows_fixture):
250237
too_high = row_count
251238
rows[too_high]
252239

240+
def it_can_add_a_row(self, add_row_fixture):
241+
rows, expected_xml = add_row_fixture
242+
row = rows.add()
243+
assert rows._tbl.xml == expected_xml
244+
assert isinstance(row, _Row)
245+
253246
# fixtures -------------------------------------------------------
254247

248+
@pytest.fixture
249+
def add_row_fixture(self):
250+
tbl = _tbl_bldr(rows=1, cols=2).element
251+
rows = _RowCollection(tbl)
252+
expected_xml = _tbl_bldr(rows=2, cols=2).xml()
253+
return rows, expected_xml
254+
255255
@pytest.fixture
256256
def rows_fixture(self):
257257
row_count = 2

0 commit comments

Comments
 (0)