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