Skip to content

Commit 6c122c0

Browse files
author
Steve Canny
committed
doc: refactor some names
‘p’ has generally come to be reserved for a <w:p> element and ‘paragraph’ used for a Paragraph instance. Similaryly ‘r’ indicates a <w:r> element and ‘run’ an instance of Run.
1 parent 4555d2a commit 6c122c0

File tree

2 files changed

+46
-40
lines changed

2 files changed

+46
-40
lines changed

docx/api.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ def add_paragraph(self, text='', style=None):
6565
Return a paragraph newly added to the end of the document, populated
6666
with *text* and having paragraph style *style*.
6767
"""
68-
p = self._document_part.add_paragraph()
68+
paragraph = self._document_part.add_paragraph()
6969
if text:
70-
p.add_run(text)
70+
paragraph.add_run(text)
7171
if style is not None:
72-
p.style = style
73-
return p
72+
paragraph.style = style
73+
return paragraph
7474

7575
def add_picture(self, image_path_or_stream, width=None, height=None):
7676
"""

tests/test_api.py

+42-36
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ def it_should_raise_if_not_a_Word_file(self, Package_, package_, docx_):
5252
Document._open(docx_)
5353

5454
def it_can_add_a_heading(self, add_heading_fixture):
55-
document, add_paragraph_, p_, text, level, style = add_heading_fixture
56-
p = document.add_heading(text, level)
55+
document, add_paragraph_, paragraph_, text, level, style = (
56+
add_heading_fixture
57+
)
58+
paragraph = document.add_heading(text, level)
5759
add_paragraph_.assert_called_once_with(text, style)
58-
assert p is p_
60+
assert paragraph is paragraph_
5961

6062
def it_should_raise_on_heading_level_out_of_range(self, document):
6163
with pytest.raises(ValueError):
@@ -64,28 +66,28 @@ def it_should_raise_on_heading_level_out_of_range(self, document):
6466
document.add_heading(level=10)
6567

6668
def it_can_add_an_empty_paragraph(self, add_empty_paragraph_fixture):
67-
document, document_part_, p_ = add_empty_paragraph_fixture
68-
p = document.add_paragraph()
69+
document, document_part_, paragraph_ = add_empty_paragraph_fixture
70+
paragraph = document.add_paragraph()
6971
document_part_.add_paragraph.assert_called_once_with()
70-
assert p is p_
72+
assert paragraph is paragraph_
7173

7274
def it_can_add_a_paragraph_of_text(self, add_text_paragraph_fixture):
73-
document, text, p_ = add_text_paragraph_fixture
74-
p = document.add_paragraph(text)
75-
p.add_run.assert_called_once_with(text)
75+
document, text = add_text_paragraph_fixture
76+
paragraph = document.add_paragraph(text)
77+
paragraph.add_run.assert_called_once_with(text)
7678

7779
def it_can_add_a_styled_paragraph(self, add_styled_paragraph_fixture):
78-
document, style, p_ = add_styled_paragraph_fixture
79-
p = document.add_paragraph(style=style)
80-
assert p.style == style
80+
document, style = add_styled_paragraph_fixture
81+
paragraph = document.add_paragraph(style=style)
82+
assert paragraph.style == style
8183

8284
def it_can_add_a_page_break(self, add_page_break_fixture):
83-
document, document_part_, p_, r_ = add_page_break_fixture
84-
p = document.add_page_break()
85+
document, document_part_, paragraph_, run_ = add_page_break_fixture
86+
paragraph = document.add_page_break()
8587
document_part_.add_paragraph.assert_called_once_with()
86-
p_.add_run.assert_called_once_with()
87-
r_.add_break.assert_called_once_with(WD_BREAK.PAGE)
88-
assert p is p_
88+
paragraph_.add_run.assert_called_once_with()
89+
run_.add_break.assert_called_once_with(WD_BREAK.PAGE)
90+
assert paragraph is paragraph_
8991

9092
def it_can_add_a_picture(self, add_picture_fixture):
9193
(document, image_path, width, height, inline_shapes_, expected_width,
@@ -176,19 +178,22 @@ def it_creates_styles_part_on_first_access_if_not_present(
176178
# fixtures -------------------------------------------------------
177179

178180
@pytest.fixture
179-
def add_empty_paragraph_fixture(self, document, document_part_, p_):
180-
return document, document_part_, p_
181+
def add_empty_paragraph_fixture(
182+
self, document, document_part_, paragraph_):
183+
return document, document_part_, paragraph_
181184

182185
@pytest.fixture(params=[0, 1, 2, 5, 9])
183-
def add_heading_fixture(self, request, document, add_paragraph_, p_):
186+
def add_heading_fixture(
187+
self, request, document, add_paragraph_, paragraph_):
184188
level = request.param
185189
text = 'Spam vs. Bacon'
186190
style = 'Title' if level == 0 else 'Heading%d' % level
187-
return document, add_paragraph_, p_, text, level, style
191+
return document, add_paragraph_, paragraph_, text, level, style
188192

189193
@pytest.fixture
190-
def add_page_break_fixture(self, document, document_part_, p_, r_):
191-
return document, document_part_, p_, r_
194+
def add_page_break_fixture(
195+
self, document, document_part_, paragraph_, run_):
196+
return document, document_part_, paragraph_, run_
192197

193198
@pytest.fixture(params=[
194199
(None, None, 200, 100),
@@ -213,9 +218,9 @@ def add_section_fixture(self, document, start_type_, section_):
213218
return document, start_type_, section_
214219

215220
@pytest.fixture
216-
def add_styled_paragraph_fixture(self, document, p_):
221+
def add_styled_paragraph_fixture(self, document):
217222
style = 'foobaresque'
218-
return document, style, p_
223+
return document, style
219224

220225
@pytest.fixture(params=[None, 'LightShading-Accent1', 'foobar'])
221226
def add_table_fixture(self, request, document, document_part_, table_):
@@ -227,9 +232,9 @@ def add_table_fixture(self, request, document, document_part_, table_):
227232
)
228233

229234
@pytest.fixture
230-
def add_text_paragraph_fixture(self, document, p_):
235+
def add_text_paragraph_fixture(self, document):
231236
text = 'foobar\rbarfoo'
232-
return document, text, p_
237+
return document, text
233238

234239
@pytest.fixture
235240
def init_fixture(self, docx_, open_):
@@ -261,9 +266,9 @@ def tables_fixture(self, document, tables_):
261266
# fixture components ---------------------------------------------
262267

263268
@pytest.fixture
264-
def add_paragraph_(self, request, p_):
269+
def add_paragraph_(self, request, paragraph_):
265270
return method_mock(
266-
request, Document, 'add_paragraph', return_value=p_
271+
request, Document, 'add_paragraph', return_value=paragraph_
267272
)
268273

269274
@pytest.fixture
@@ -282,11 +287,12 @@ def document(self, open_):
282287

283288
@pytest.fixture
284289
def document_part_(
285-
self, request, p_, paragraphs_, section_, table_, tables_):
290+
self, request, paragraph_, paragraphs_, section_, table_,
291+
tables_):
286292
document_part_ = instance_mock(
287293
request, DocumentPart, content_type=CT.WML_DOCUMENT_MAIN
288294
)
289-
document_part_.add_paragraph.return_value = p_
295+
document_part_.add_paragraph.return_value = paragraph_
290296
document_part_.add_section.return_value = section_
291297
document_part_.add_table.return_value = table_
292298
document_part_.paragraphs = paragraphs_
@@ -325,10 +331,10 @@ def open_(self, request, document_part_, package_):
325331
)
326332

327333
@pytest.fixture
328-
def p_(self, request, r_):
329-
p_ = instance_mock(request, Paragraph)
330-
p_.add_run.return_value = r_
331-
return p_
334+
def paragraph_(self, request, run_):
335+
paragraph_ = instance_mock(request, Paragraph)
336+
paragraph_.add_run.return_value = run_
337+
return paragraph_
332338

333339
@pytest.fixture
334340
def Package_(self, request, package_):
@@ -347,7 +353,7 @@ def paragraphs_(self, request):
347353
return instance_mock(request, list)
348354

349355
@pytest.fixture
350-
def r_(self, request):
356+
def run_(self, request):
351357
return instance_mock(request, Run)
352358

353359
@pytest.fixture

0 commit comments

Comments
 (0)