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