Conditions | 20 |
Total Lines | 167 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like AbstractGenerator._write_doc_aini2016() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
187 | def _write_doc_aini2016(self, doc, record): |
||
188 | print('"%s"' % record['Title']) |
||
189 | exreg4num = re.compile(r'\((\w+)\)') |
||
190 | |||
191 | font = doc.styles['Normal'].font |
||
192 | font.size = docx.shared.Pt(10) |
||
193 | font.name = 'Times New Roman' |
||
194 | |||
195 | # Program Number |
||
196 | # p = doc.add_paragraph() |
||
197 | # p.paragraph_format.line_spacing = docx.shared.Pt(12) |
||
198 | # p.paragraph_format.space_after = docx.shared.Pt(5) |
||
199 | # r = p.add_run(record['Program No.'].strip()) |
||
200 | |||
201 | # Title |
||
202 | p = doc.add_paragraph() |
||
203 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER |
||
204 | p.paragraph_format.space_before = docx.shared.Pt(25) |
||
205 | p.paragraph_format.space_after = docx.shared.Pt(14) |
||
206 | r = p.add_run(record['Title'].strip()) |
||
207 | r.font.size = docx.shared.Pt(12) |
||
208 | r.bold = True |
||
209 | r.italic = True |
||
210 | |||
211 | # Authors |
||
212 | p = doc.add_paragraph() |
||
213 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER |
||
214 | p.paragraph_format.line_spacing = docx.shared.Pt(12) |
||
215 | p.paragraph_format.space_after = docx.shared.Pt(12) |
||
216 | authors = self._toArray(record['Name'], '\n') |
||
217 | first = True |
||
218 | for author in authors: |
||
219 | m = self.exreg4author.match(author) |
||
220 | if first == False: |
||
221 | p.add_run(', ').bold = True |
||
222 | name = m.group(1).strip().replace(' ', '\u00A0') |
||
223 | num = self._removeParentheses(m.group(2).strip()) |
||
224 | p.add_run(name).bold = True |
||
225 | if num != '': |
||
226 | r = p.add_run('\u00A0' + num) |
||
227 | r.bold = True |
||
228 | r.font.superscript = True |
||
229 | first = False |
||
230 | p.add_run('\n') |
||
231 | |||
232 | # Affiliation |
||
233 | affiliations = self._toArray(record['Affiliation'], '\n') |
||
234 | first = True |
||
235 | for affiliation in affiliations: |
||
236 | m = self.exreg4affiliation.match(affiliation) |
||
237 | if first == False: |
||
238 | p.add_run(', ') |
||
239 | num = self._removeParentheses(m.group(1).strip()) |
||
240 | name = m.group(2).strip() |
||
241 | if num != '': |
||
242 | r = p.add_run(num + '\u00A0') |
||
243 | r.font.superscript = True |
||
244 | p.add_run(name) |
||
245 | first = False |
||
246 | p.add_run('\n' + record['e-mail']) |
||
247 | |||
248 | # DOI |
||
249 | p = doc.add_paragraph('DOI:' + record['DOI'].strip()) |
||
250 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER |
||
251 | p.paragraph_format.space_after = docx.shared.Pt(12) |
||
252 | |||
253 | # Abstract Body |
||
254 | items = self._toArray(record['Abstract'], '\n') |
||
255 | first = True |
||
256 | for item in items: |
||
257 | p = doc.add_paragraph(item) |
||
258 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
259 | p.paragraph_format.line_spacing = docx.shared.Pt(11) |
||
260 | p.paragraph_format.space_after = docx.shared.Pt(2) |
||
261 | if first == False: |
||
262 | p.paragraph_format.first_line_indent = docx.shared.Pt(12) |
||
263 | first = False |
||
264 | p.paragraph_format.space_after = docx.shared.Pt(12) |
||
265 | |||
266 | # Figure |
||
267 | if self._empty(record['Figure file Name']) == False: |
||
268 | |||
269 | # Figure File Name |
||
270 | img_fpath = os.path.join(self.image_dir, record['Figure file Name']) |
||
271 | size = self._getPreferredImageSize(img_fpath) |
||
272 | doc.add_picture(img_fpath, width=size[0]) # , height=size[1]) |
||
273 | p = doc.paragraphs[-1] |
||
274 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER |
||
275 | |||
276 | # Figure Comment |
||
277 | items = self._toArray(record['Figure comment'], '\n') |
||
278 | first = True |
||
279 | for item in items: |
||
280 | p = doc.add_paragraph() |
||
281 | p.paragraph_format.line_spacing = docx.shared.Pt(10) |
||
282 | p.paragraph_format.space_after = docx.shared.Pt(0) |
||
283 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
284 | if first: |
||
285 | p.add_run('Figure: ').bold = True |
||
286 | first = False |
||
287 | p.add_run(item) |
||
288 | |||
289 | p.paragraph_format.space_after = docx.shared.Pt(14) |
||
290 | |||
291 | # References |
||
292 | items = self._toArray(record['References'], '\n') |
||
293 | first = True |
||
294 | for item in items: |
||
295 | if first: |
||
296 | p = doc.add_paragraph() |
||
297 | p.paragraph_format.line_spacing = docx.shared.Pt(11) |
||
298 | p.paragraph_format.space_after = docx.shared.Pt(0) |
||
299 | p.add_run('References:').bold = True |
||
300 | first = False |
||
301 | p = doc.add_paragraph() |
||
302 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
303 | p.paragraph_format.line_spacing = docx.shared.Pt(10) |
||
304 | p.paragraph_format.space_after = docx.shared.Pt(0) |
||
305 | p.add_run(item) |
||
306 | p.paragraph_format.space_after = docx.shared.Pt(10) |
||
307 | |||
308 | # Acknowledgement |
||
309 | items = self._toArray(record['Acknowledgement'], '\n') |
||
310 | first = True |
||
311 | for item in items: |
||
312 | p = doc.add_paragraph() |
||
313 | p.paragraph_format.line_spacing = docx.shared.Pt(10) |
||
314 | p.paragraph_format.space_after = docx.shared.Pt(0) |
||
315 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
316 | if first: |
||
317 | p.add_run('Ackknowledgement: ').bold = True |
||
318 | first = False |
||
319 | p.add_run(item) |
||
320 | p.paragraph_format.space_after = docx.shared.Pt(10) |
||
321 | |||
322 | # Funding |
||
323 | items = self._toArray(record['Funding'], '\n') |
||
324 | first = True |
||
325 | for item in items: |
||
326 | p = doc.add_paragraph() |
||
327 | p.paragraph_format.line_spacing = docx.shared.Pt(10) |
||
328 | p.paragraph_format.space_after = docx.shared.Pt(0) |
||
329 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
330 | if first: |
||
331 | p.add_run('Funding: ').bold = True |
||
332 | first = False |
||
333 | p.add_run(item) |
||
334 | p.paragraph_format.space_after = docx.shared.Pt(10) |
||
335 | |||
336 | # Citation |
||
337 | p = doc.add_paragraph() |
||
338 | p.paragraph_format.line_spacing = docx.shared.Pt(10) |
||
339 | p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.JUSTIFY |
||
340 | p.add_run('Citation: ').bold = True |
||
341 | author_tmp = '' |
||
342 | first = True |
||
343 | for author in authors: |
||
344 | m = self.exreg4author.match(author) |
||
345 | if first == False: |
||
346 | author_tmp += ', ' |
||
347 | author_tmp += m.group(1).strip() |
||
348 | first = False |
||
349 | p.add_run(author_tmp + ' (2016). ' + record['Title'].strip().replace('\n', ' ') + '. ') |
||
350 | p.add_run('Advances in Neuroinformatics IV. ').italic = True |
||
351 | p.add_run( |
||
352 | 'AINI 2016 and INCF Nodes Workshop Abstract: ' + record['Program No. Long'].strip() + '. DOI:' + record[ |
||
353 | 'DOI'].strip()) |
||
354 | |||
361 |