Conditions | 11 |
Total Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 table2csv() 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 | #!/usr/bin/env python3 |
||
60 | def table2csv(elem, doc): |
||
61 | """ |
||
62 | find Table element and return a csv table in code-block with class "table" |
||
63 | """ |
||
64 | if isinstance(elem, panflute.Table): |
||
65 | # get options as a dictionary |
||
66 | options = {} |
||
67 | # options: caption: panflute ast to markdown |
||
68 | if elem.caption: |
||
69 | options['caption'] = panflute.convert_text( |
||
70 | panflute.Para(*elem.caption), |
||
71 | input_format='panflute', |
||
72 | output_format='markdown' |
||
73 | ) |
||
74 | # options: alignment |
||
75 | parsed_alignment = [("L" if i == "AlignLeft" |
||
76 | else "C" if i == "AlignCenter" |
||
77 | else "R" if i == "AlignRight" |
||
78 | else "D") for i in elem.alignment] |
||
79 | options['alignment'] = "".join(parsed_alignment) |
||
80 | # options: width |
||
81 | options['width'] = elem.width |
||
82 | # options: table-width from width |
||
83 | options['table-width'] = sum(options['width']) |
||
84 | # options: header: False if empty header row, else True |
||
85 | options['header'] = bool(panflute.stringify(elem.header)) |
||
86 | # options: markdown |
||
87 | options['markdown'] = True |
||
88 | |||
89 | # option in YAML |
||
90 | yaml_metadata = yaml.dump(options) |
||
91 | |||
92 | # table in panflute AST |
||
93 | table_body = elem.content |
||
94 | if options['header']: |
||
95 | table_body.insert(0, elem.header) |
||
96 | # table in list |
||
97 | table_list = [ |
||
98 | [panflute.convert_text( |
||
99 | cell.content, |
||
100 | input_format='panflute', |
||
101 | output_format='markdown' |
||
102 | ) for cell in row.content] |
||
103 | for row in table_body] |
||
104 | # table in CSV |
||
105 | with io.StringIO() as file: |
||
106 | writer = csv.writer(file) |
||
107 | writer.writerows(table_list) |
||
108 | csv_table = file.getvalue() |
||
109 | return panflute.CodeBlock("---\n" + yaml_metadata + "---\n" + csv_table, classes=["table"]) |
||
110 | return None |
||
111 | |||
125 |