| Conditions | 8 |
| Total Lines | 72 |
| Code Lines | 42 |
| 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:
| 1 | from sqlalchemy import ARRAY, Column, ForeignKey, Integer, String |
||
| 133 | def store_results(session, input_data, result_data): |
||
| 134 | """ |
||
| 135 | Stores inputs and results from oemof into DB |
||
| 136 | |||
| 137 | For each in entry in scalars and sequences of both input- and result-data |
||
| 138 | an OemofScalar or OemofSequence is build and connected to an OemofData |
||
| 139 | object representing either input or result data. At last, both OemofData |
||
| 140 | objects are connected to OemofInputResult object and resulting index is |
||
| 141 | returned. |
||
| 142 | |||
| 143 | Parameters |
||
| 144 | ---------- |
||
| 145 | session: sqlalchemy.session |
||
| 146 | SQLAlchemy session build via sqlalchemy.orm.sessionmaker |
||
| 147 | input_data: dict |
||
| 148 | Output of oemof.outputlib.processing.param_results with nodes as str |
||
| 149 | (use oemof.outputlib.processing.convert_keys_to_str if necessary) |
||
| 150 | result_data: dict |
||
| 151 | Output of oemof.outputlib.processing.param_results with nodes as str |
||
| 152 | (use oemof.outputlib.processing.convert_keys_to_str if necessary) |
||
| 153 | |||
| 154 | Returns |
||
| 155 | ------- |
||
| 156 | int: Index of created OemofInputResult entry |
||
| 157 | """ |
||
| 158 | # Check if nodes are strings: |
||
| 159 | if not isinstance(next(iter(input_data)), str): |
||
| 160 | input_data = convert_keys_to_strings(input_data) |
||
| 161 | if not isinstance(next(iter(result_data)), str): |
||
| 162 | result_data = convert_keys_to_strings(result_data) |
||
| 163 | |||
| 164 | input_result = OemofInputResult() |
||
| 165 | for input_result_attr, data in ( |
||
| 166 | ('input', input_data), ('result', result_data)): |
||
| 167 | scalars = [] |
||
| 168 | sequences = [] |
||
| 169 | for (from_node, to_node), sc_sq_dict in data.items(): |
||
| 170 | for key, value in sc_sq_dict['scalars'].items(): |
||
| 171 | scalars.append( |
||
| 172 | OemofScalar( |
||
| 173 | from_node=from_node, |
||
| 174 | to_node=to_node, |
||
| 175 | attribute=key, |
||
| 176 | value=value, |
||
| 177 | type=type(value).__name__ |
||
| 178 | ) |
||
| 179 | ) |
||
| 180 | session.add_all(scalars) |
||
| 181 | for key, series in sc_sq_dict['sequences'].items(): |
||
| 182 | list_type = 'list' |
||
| 183 | if isinstance(series, pandas.Series): |
||
| 184 | series = series.values.tolist() |
||
| 185 | list_type = 'series' |
||
| 186 | sequences.append( |
||
| 187 | OemofSequence( |
||
| 188 | from_node=from_node, |
||
| 189 | to_node=to_node, |
||
| 190 | attribute=key, |
||
| 191 | value=series, |
||
| 192 | type=list_type |
||
| 193 | ) |
||
| 194 | ) |
||
| 195 | session.add_all(sequences) |
||
| 196 | oemof_data = OemofData() |
||
| 197 | oemof_data.scalars = scalars |
||
| 198 | oemof_data.sequences = sequences |
||
| 199 | setattr(input_result, input_result_attr, oemof_data) |
||
| 200 | session.add(input_result) |
||
| 201 | session.flush() |
||
| 202 | result_id = input_result.input_result_id |
||
| 203 | session.commit() |
||
| 204 | return result_id |
||
| 205 | |||
| 263 |