Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5233fd

Browse files
DKWoodsSteve Canny
authored andcommitted
tabs: add TabStops.__getitem__()
1 parent 1bcdf4a commit e5233fd

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

docx/text/tabstops.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def __init__(self, element):
2525
super(TabStops, self).__init__(element, None)
2626
self._pPr = element
2727

28+
def __getitem__(self, idx):
29+
"""
30+
Enables list-style access by index.
31+
"""
32+
tabs = self._pPr.tabs
33+
if tabs is None:
34+
raise IndexError('TabStops object is empty')
35+
tab = tabs.tab_lst[idx]
36+
return TabStop(tab)
37+
2838
def __iter__(self):
2939
"""
3040
Generate a TabStop object for each of the w:tab elements, in XML

features/tab-access-tabs.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Feature: Access TabStop objects
1414
| 3 |
1515

1616

17-
@wip
1817
Scenario: Access an existing TabStop object
1918
Given a tab_stops having 3 tab stops
2019
Then I can iterate the TabStops object

tests/text/test_tabstops.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,31 @@ def it_can_iterate_over_its_tab_stops(self, iter_fixture):
3434
assert count == expected_count
3535
assert TabStop_.call_args_list == expected_calls
3636

37+
def it_can_get_a_tab_stop_by_index(self, index_fixture):
38+
tab_stops, idx, TabStop_, tab, tab_stop_ = index_fixture
39+
tab_stop = tab_stops[idx]
40+
TabStop_.assert_called_once_with(tab)
41+
assert tab_stop is tab_stop_
42+
43+
def it_raises_on_indexed_access_when_empty(self):
44+
tab_stops = TabStops(element('w:pPr'))
45+
with pytest.raises(IndexError):
46+
tab_stops[0]
47+
3748
# fixture --------------------------------------------------------
3849

50+
@pytest.fixture(params=[
51+
('w:pPr/w:tabs/w:tab{w:pos=0}', 0),
52+
('w:pPr/w:tabs/(w:tab{w:pos=1},w:tab{w:pos=2},w:tab{w:pos=3})', 1),
53+
('w:pPr/w:tabs/(w:tab{w:pos=4},w:tab{w:pos=5},w:tab{w:pos=6})', 2),
54+
])
55+
def index_fixture(self, request, TabStop_, tab_stop_):
56+
pPr_cxml, idx = request.param
57+
pPr = element(pPr_cxml)
58+
tab = pPr.xpath('./w:tabs/w:tab')[idx]
59+
tab_stops = TabStops(pPr)
60+
return tab_stops, idx, TabStop_, tab, tab_stop_
61+
3962
@pytest.fixture(params=[
4063
('w:pPr', 0),
4164
('w:pPr/w:tabs/w:tab{w:pos=2880}', 1),

0 commit comments

Comments
 (0)