Conditions | 2 |
Total Lines | 56 |
Code Lines | 18 |
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 | """Create a basic scenario from the internal data structure. |
||
13 | def scenario_mobility(year, table): |
||
14 | """ |
||
15 | |||
16 | Parameters |
||
17 | ---------- |
||
18 | year |
||
19 | table |
||
20 | |||
21 | Returns |
||
22 | ------- |
||
23 | |||
24 | Examples |
||
25 | -------- |
||
26 | >>> table = scenario_mobility(2015, {}) |
||
27 | >>> table["mobility_mileage"]["DE"].sum() |
||
28 | diesel 3.769021e+11 |
||
29 | petrol 3.272263e+11 |
||
30 | other 1.334462e+10 |
||
31 | dtype: float64 |
||
32 | >>> table["mobility_spec_demand"]["DE"].loc["passenger car"] |
||
33 | diesel 0.067 |
||
34 | petrol 0.079 |
||
35 | other 0.000 |
||
36 | Name: passenger car, dtype: float64 |
||
37 | >>> table["mobility_energy_content"]["DE"]["diesel"] |
||
38 | energy_per_liter [MJ/l] 34.7 |
||
39 | Name: diesel, dtype: float64 |
||
40 | """ |
||
41 | |||
42 | table["mobility_mileage"] = mobility.get_mileage_by_type_and_fuel(year) |
||
43 | |||
44 | # fetch table of specific demand by fuel and vehicle type (from 2011) |
||
45 | table["mobility_spec_demand"] = ( |
||
46 | pd.DataFrame( |
||
47 | cfg.get_dict_list("fuel consumption"), |
||
48 | index=["diesel", "petrol", "other"], |
||
49 | ) |
||
50 | .astype(float) |
||
51 | .transpose() |
||
52 | ) |
||
53 | |||
54 | # fetch the energy content of the different fuel types |
||
55 | table["mobility_energy_content"] = pd.DataFrame( |
||
56 | cfg.get_dict("energy_per_liter"), index=["energy_per_liter [MJ/l]"] |
||
57 | )[["diesel", "petrol", "other"]] |
||
58 | |||
59 | for key in [ |
||
60 | "mobility_mileage", |
||
61 | "mobility_spec_demand", |
||
62 | "mobility_energy_content", |
||
63 | ]: |
||
64 | # Add "DE" as region level to be consistent to other tables |
||
65 | table[key].columns = pd.MultiIndex.from_product( |
||
66 | [["DE"], table[key].columns] |
||
67 | ) |
||
68 | return table |
||
69 |