Total Complexity | 3 |
Total Lines | 53 |
Duplicated Lines | 81.13 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | # -*- coding: utf-8 -*- |
||
2 | import warnings |
||
3 | |||
4 | import pytest |
||
5 | from oemof.tools.debugging import ExperimentalFeatureWarning |
||
6 | |||
7 | from oemof import solph |
||
8 | |||
9 | |||
10 | View Code Duplication | def test_multi_input_sink(): |
|
|
|||
11 | num_in = 3 |
||
12 | steps = 10 |
||
13 | costs = -0.1 |
||
14 | |||
15 | es = solph.EnergySystem( |
||
16 | timeindex=solph.create_time_index(year=2023, number=steps), |
||
17 | infer_last_interval=False, |
||
18 | ) |
||
19 | |||
20 | for i in range(num_in): |
||
21 | bus_label = f"bus input {i}" |
||
22 | b = solph.Bus(bus_label) |
||
23 | es.add(b) |
||
24 | es.add( |
||
25 | solph.components.Source(f"source {i}", outputs={b: solph.Flow()}) |
||
26 | ) |
||
27 | # Use of experimental API to access nodes by label. |
||
28 | # Can be removed with future release of network. |
||
29 | with warnings.catch_warnings(): |
||
30 | warnings.simplefilter("ignore", ExperimentalFeatureWarning) |
||
31 | es.add( |
||
32 | solph.components.Sink( |
||
33 | inputs={ |
||
34 | es.node[f"bus input {i}"]: solph.Flow( |
||
35 | nominal_capacity=1, |
||
36 | variable_costs=costs, |
||
37 | ) |
||
38 | for i in range(num_in) |
||
39 | } |
||
40 | ) |
||
41 | ) |
||
42 | |||
43 | model = solph.Model(es) |
||
44 | model.solve("cbc") |
||
45 | |||
46 | assert ( |
||
47 | model.solver_results["Solver"][0]["Termination condition"] |
||
48 | != "infeasible" |
||
49 | ) |
||
50 | meta_results = solph.processing.meta_results(model) |
||
51 | |||
52 | assert meta_results["objective"] == pytest.approx(num_in * steps * costs) |
||
53 |