Conditions | 14 |
Total Lines | 66 |
Lines | 0 |
Ratio | 0 % |
Tests | 31 |
CRAP Score | 14.8363 |
Changes | 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 ArmHelper._parse() 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 | 1 | from plugin.core.helpers.variable import try_convert |
|
129 | 1 | @classmethod |
|
130 | def _parse(cls, lines): |
||
131 | 1 | processors = {} |
|
132 | 1 | extra = {} |
|
133 | |||
134 | # Parse lines into `processors` and `extra` |
||
135 | 1 | section = None |
|
136 | 1 | current = {} |
|
137 | |||
138 | 1 | for line in lines: |
|
139 | # Handle section break |
||
140 | 1 | if line == '': |
|
141 | # Store current attributes |
||
142 | 1 | if section == 'processor': |
|
143 | 1 | num = try_convert(current.pop('processor', None), int) |
|
144 | |||
145 | 1 | if num is None: |
|
146 | num = len(processors) |
||
147 | |||
148 | 1 | processors[num] = current |
|
149 | 1 | elif section == 'extra': |
|
150 | 1 | extra.update(current) |
|
151 | 1 | elif current: |
|
152 | log.debug('Discarding unknown attributes: %r', current) |
||
153 | |||
154 | # Reset state |
||
155 | 1 | section = None |
|
156 | 1 | current = {} |
|
157 | |||
158 | # Continue with next line |
||
159 | 1 | continue |
|
160 | |||
161 | # Parse attribute from line |
||
162 | 1 | parts = [part.strip() for part in line.split(':', 1)] |
|
163 | |||
164 | 1 | if len(parts) < 2: |
|
165 | log.debug('Unable to parse attribute from line: %r', line) |
||
166 | continue |
||
167 | |||
168 | # Retrieve attribute components |
||
169 | 1 | key, value = parts[0], parts[1] |
|
170 | |||
171 | 1 | if not key: |
|
172 | log.debug('Invalid key returned for line: %r', line) |
||
173 | continue |
||
174 | |||
175 | # Transform `key` |
||
176 | 1 | key = key.lower() |
|
177 | 1 | key = key.replace(' ', '_') |
|
178 | |||
179 | # Check for section-identifier |
||
180 | 1 | if not section: |
|
181 | 1 | if key == 'processor': |
|
182 | 1 | section = 'processor' |
|
183 | else: |
||
184 | 1 | section = 'extra' |
|
185 | |||
186 | # Store attribute in current dictionary |
||
187 | 1 | current[key] = value |
|
188 | |||
189 | # Store any leftover extra attributes |
||
190 | 1 | if section == 'extra' and current: |
|
191 | 1 | extra.update(current) |
|
192 | |||
193 | # Return result |
||
194 | 1 | return processors, extra |
|
195 | |||
218 |