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