| Conditions | 1 |
| Total Lines | 70 |
| Code Lines | 48 |
| 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 | # -*- coding: utf-8 -*- |
||
| 17 | def test_equate_flows(): |
||
| 18 | date_time_index = pd.date_range("1/1/2012", periods=3, freq="h") |
||
| 19 | energysystem = solph.EnergySystem( |
||
| 20 | timeindex=date_time_index, |
||
| 21 | infer_last_interval=False, |
||
| 22 | ) |
||
| 23 | |||
| 24 | b1 = solph.buses.Bus(label="b1", balanced=False) |
||
| 25 | s0 = solph.components.Sink( |
||
| 26 | label="s1", |
||
| 27 | inputs={ |
||
| 28 | b1: solph.Flow( |
||
| 29 | variable_costs=-0.5, |
||
| 30 | max=[0.5, 1], |
||
| 31 | nominal_capacity=4, |
||
| 32 | custom_attributes={"keyword1": "group 1"}, |
||
| 33 | ) |
||
| 34 | }, |
||
| 35 | ) |
||
| 36 | s1 = solph.components.Sink( |
||
| 37 | label="s2", |
||
| 38 | inputs={ |
||
| 39 | b1: solph.Flow( |
||
| 40 | variable_costs=0.1, |
||
| 41 | nominal_capacity=2, |
||
| 42 | custom_attributes={"keyword2": "group 2"}, |
||
| 43 | ) |
||
| 44 | }, |
||
| 45 | ) |
||
| 46 | s2 = solph.components.Sink( |
||
| 47 | label="s3", |
||
| 48 | inputs={ |
||
| 49 | b1: solph.Flow( |
||
| 50 | variable_costs=0.2, |
||
| 51 | nominal_capacity=3, |
||
| 52 | custom_attributes={"keyword2": "group 2"}, |
||
| 53 | ) |
||
| 54 | }, |
||
| 55 | ) |
||
| 56 | s3 = solph.components.Sink( |
||
| 57 | label="s4", |
||
| 58 | inputs={ |
||
| 59 | b1: solph.Flow( |
||
| 60 | variable_costs=0.2, |
||
| 61 | nominal_capacity=3, |
||
| 62 | custom_attributes={"keyword3": "no group"}, |
||
| 63 | ) |
||
| 64 | }, |
||
| 65 | ) |
||
| 66 | energysystem.add(b1, s0, s1, s2, s3) |
||
| 67 | |||
| 68 | model = solph.Model(energysystem) |
||
| 69 | |||
| 70 | solph.constraints.equate_flows_by_keyword( |
||
| 71 | model, "keyword1", "keyword2", 0.75 |
||
| 72 | ) |
||
| 73 | |||
| 74 | model.solve() |
||
| 75 | |||
| 76 | results = solph.processing.results(model) |
||
| 77 | |||
| 78 | flow = [ |
||
| 79 | list(results[(b1, s)]["sequences"]["flow"][:-1]) |
||
| 80 | for s in [s0, s1, s2, s3] |
||
| 81 | ] |
||
| 82 | |||
| 83 | assert flow[0] == pytest.approx([2, 4]) |
||
| 84 | assert flow[1] == pytest.approx([1.5, 2]) |
||
| 85 | assert flow[2] == pytest.approx([0, 1]) |
||
| 86 | assert flow[3] == pytest.approx([0, 0]) |
||
| 87 |