Conditions | 29 |
Total Lines | 61 |
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 AttrListTreeprocessor.run() 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 | """ |
||
86 | def run(self, doc): |
||
87 | for elem in doc.iter(): |
||
88 | if isBlockLevel(elem.tag): |
||
89 | # Block level: check for attrs on last line of text |
||
90 | RE = self.BLOCK_RE |
||
91 | if isheader(elem) or elem.tag == 'dt': |
||
92 | # header or def-term: check for attrs at end of line |
||
93 | RE = self.HEADER_RE |
||
94 | if len(elem) and elem.tag == 'li': |
||
95 | # special case list items. children may include a ul or ol. |
||
96 | pos = None |
||
97 | # find the ul or ol position |
||
98 | for i, child in enumerate(elem): |
||
99 | if child.tag in ['ul', 'ol']: |
||
100 | pos = i |
||
101 | break |
||
102 | if pos is None and elem[-1].tail: |
||
103 | # use tail of last child. no ul or ol. |
||
104 | m = RE.search(elem[-1].tail) |
||
105 | if m: |
||
106 | self.assign_attrs(elem, m.group(1)) |
||
107 | elem[-1].tail = elem[-1].tail[:m.start()] |
||
108 | elif pos is not None and pos > 0 and elem[pos-1].tail: |
||
109 | # use tail of last child before ul or ol |
||
110 | m = RE.search(elem[pos-1].tail) |
||
111 | if m: |
||
112 | self.assign_attrs(elem, m.group(1)) |
||
113 | elem[pos-1].tail = elem[pos-1].tail[:m.start()] |
||
114 | elif elem.text: |
||
115 | # use text. ul is first child. |
||
116 | m = RE.search(elem.text) |
||
117 | if m: |
||
118 | self.assign_attrs(elem, m.group(1)) |
||
119 | elem.text = elem.text[:m.start()] |
||
120 | elif len(elem) and elem[-1].tail: |
||
121 | # has children. Get from tail of last child |
||
122 | m = RE.search(elem[-1].tail) |
||
123 | if m: |
||
124 | self.assign_attrs(elem, m.group(1)) |
||
125 | elem[-1].tail = elem[-1].tail[:m.start()] |
||
126 | if isheader(elem): |
||
127 | # clean up trailing #s |
||
128 | elem[-1].tail = elem[-1].tail.rstrip('#').rstrip() |
||
129 | elif elem.text: |
||
130 | # no children. Get from text. |
||
131 | m = RE.search(elem.text) |
||
132 | if not m and elem.tag == 'td': |
||
133 | m = re.search(self.BASE_RE, elem.text) |
||
134 | if m: |
||
135 | self.assign_attrs(elem, m.group(1)) |
||
136 | elem.text = elem.text[:m.start()] |
||
137 | if isheader(elem): |
||
138 | # clean up trailing #s |
||
139 | elem.text = elem.text.rstrip('#').rstrip() |
||
140 | else: |
||
141 | # inline: check for attrs at start of tail |
||
142 | if elem.tail: |
||
143 | m = self.INLINE_RE.match(elem.tail) |
||
144 | if m: |
||
145 | self.assign_attrs(elem, m.group(1)) |
||
146 | elem.tail = elem.tail[m.end():] |
||
147 | |||
179 |