Conditions | 12 |
Total Lines | 56 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
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 db.results.restore_results() 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 | """ |
||
215 | def restore_results(session, input_result_id): |
||
216 | """ |
||
217 | Restores input and result data from OemofInputResult from DB |
||
218 | |||
219 | Parameters |
||
220 | ---------- |
||
221 | session: sqlalchemy.session |
||
222 | SQLAlchemy session build via sqlalchemy.orm.sessionmaker |
||
223 | input_result_id: int |
||
224 | Index of OemofInputResult object to restore |
||
225 | |||
226 | Returns |
||
227 | ------- |
||
228 | (dict, dict): |
||
229 | Restored input- and result-data |
||
230 | """ |
||
231 | def type_conversion(value_str, value_type): |
||
232 | if value_type == 'str': |
||
233 | return value_str |
||
234 | elif value_type == 'float': |
||
235 | return float(value_str) |
||
236 | elif value_type == 'int': |
||
237 | return int(value_str) |
||
238 | elif value_type == 'bool': |
||
239 | return bool(value_str) |
||
240 | else: |
||
241 | raise TypeError('Unknown conversion type "' + value_type + '"') |
||
242 | |||
243 | # Find results: |
||
244 | input_result = session.query(OemofInputResult).filter( |
||
245 | OemofInputResult.input_result_id == input_result_id).first() |
||
246 | if input_result is None: |
||
247 | raise IndexError( |
||
248 | 'Could not find OemofInputResult with ID #' + str(input_result_id)) |
||
249 | |||
250 | input_data = {} |
||
251 | result_data = {} |
||
252 | for input_result_attr, data in ( |
||
253 | ('input', input_data), ('result', result_data)): |
||
254 | ir_attr = getattr(input_result, input_result_attr) |
||
255 | for scalar in ir_attr.scalars: |
||
256 | nodes = (scalar.from_node, scalar.to_node) |
||
257 | if nodes not in data: |
||
258 | data[nodes] = {'scalars': {}, 'sequences': {}} |
||
259 | data[nodes]['scalars'][scalar.attribute] = type_conversion( |
||
260 | scalar.value, scalar.type) |
||
261 | for sequence in ir_attr.sequences: |
||
262 | nodes = (sequence.from_node, sequence.to_node) |
||
263 | if nodes not in data: |
||
264 | data[nodes] = {'scalars': {}, 'sequences': {}} |
||
265 | if sequence.type == 'series': |
||
266 | series = pandas.Series(sequence.value) |
||
267 | else: |
||
268 | series = sequence.value |
||
269 | data[nodes]['sequences'][sequence.attribute] = series |
||
270 | return input_data, result_data |
||
271 |