| Conditions | 1 |
| Total Lines | 55 |
| Code Lines | 38 |
| 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 -*- |
||
| 16 | def test_additional_total_limit(): |
||
| 17 | date_time_index = pd.date_range("1/1/2012", periods=3, freq="h") |
||
| 18 | energysystem = solph.EnergySystem( |
||
| 19 | timeindex=date_time_index, |
||
| 20 | infer_last_interval=True, |
||
| 21 | ) |
||
| 22 | |||
| 23 | a = solph.buses.Bus(label="a", balanced=False) |
||
| 24 | b = solph.buses.Bus(label="b", balanced=True) |
||
| 25 | |||
| 26 | # Converter a |
||
| 27 | conv_linear = 4 |
||
| 28 | conv_offset = 5 |
||
| 29 | conv_flow = 1 |
||
| 30 | converter = solph.components.Converter( |
||
| 31 | label="trafo_a", |
||
| 32 | inputs={a: solph.Flow()}, |
||
| 33 | outputs={ |
||
| 34 | b: solph.Flow( |
||
| 35 | nominal_capacity=solph.Investment( |
||
| 36 | ep_costs=1, |
||
| 37 | offset=1, |
||
| 38 | custom_attributes={ |
||
| 39 | "emission": { |
||
| 40 | "linear": conv_linear, |
||
| 41 | "offset": conv_offset, |
||
| 42 | } |
||
| 43 | }, |
||
| 44 | nonconvex=True, |
||
| 45 | maximum=5, |
||
| 46 | ), |
||
| 47 | custom_attributes={"emission": conv_flow}, |
||
| 48 | ) |
||
| 49 | }, |
||
| 50 | conversion_factors={a: 1}, |
||
| 51 | ) |
||
| 52 | energysystem.add(converter) |
||
| 53 | s = solph.components.Sink( |
||
| 54 | label="s", |
||
| 55 | inputs={b: solph.Flow(nominal_capacity=1, fix=[1, 1, 1])}, |
||
| 56 | ) |
||
| 57 | |||
| 58 | energysystem.add(converter, a, b, s) |
||
| 59 | |||
| 60 | model = solph.Model(energysystem) |
||
| 61 | |||
| 62 | model = solph.constraints.additional_total_limit( |
||
| 63 | model, "emission", limit=100 |
||
| 64 | ) |
||
| 65 | |||
| 66 | model.solve(solver="cbc") |
||
| 67 | |||
| 68 | emission_used = model.total_limit_emission() |
||
| 69 | print(emission_used) |
||
| 70 | assert (1 * 3 + 4 + 5) == emission_used |
||
| 71 |