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