Conditions | 6 |
Total Lines | 99 |
Code Lines | 45 |
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. |
||
16 | def scenario_mobility(year, table): |
||
17 | """ |
||
18 | |||
19 | Parameters |
||
20 | ---------- |
||
21 | year |
||
22 | table |
||
23 | |||
24 | Returns |
||
25 | ------- |
||
26 | |||
27 | Examples |
||
28 | -------- |
||
29 | >>> my_table = scenario_mobility(2015, {}) |
||
30 | >>> my_table["mobility_mileage"]["DE"].sum() |
||
31 | diesel 3.769021e+11 |
||
32 | petrol 3.272263e+11 |
||
33 | other 1.334462e+10 |
||
34 | dtype: float64 |
||
35 | >>> my_table["mobility_spec_demand"]["DE"].loc["passenger car"] |
||
36 | diesel 0.067 |
||
37 | petrol 0.079 |
||
38 | other 0.000 |
||
39 | Name: passenger car, dtype: float64 |
||
40 | >>> my_table["mobility_energy_content"]["DE"]["diesel"] |
||
41 | energy_per_liter [MJ/l] 34.7 |
||
42 | Name: diesel, dtype: float64 |
||
43 | """ |
||
44 | if calendar.isleap(year): |
||
45 | hours_of_the_year = 8784 |
||
46 | else: |
||
47 | hours_of_the_year = 8760 |
||
48 | |||
49 | try: |
||
50 | other = cfg.get("creator", "mobility_other") |
||
51 | except configparser.NoSectionError: |
||
52 | other = cfg.get("general", "mobility_other") |
||
53 | |||
54 | mobility_mileage = mobility.get_mileage_by_type_and_fuel(year) |
||
55 | |||
56 | # fetch table of specific demand by fuel and vehicle type (from 2011) |
||
57 | mobility_spec_demand = ( |
||
58 | pd.DataFrame( |
||
59 | cfg.get_dict_list("fuel consumption"), |
||
60 | index=["diesel", "petrol", "other"], |
||
61 | ) |
||
62 | .astype(float) |
||
63 | .transpose() |
||
64 | ) |
||
65 | |||
66 | mobility_spec_demand["other"] = mobility_spec_demand[other] |
||
67 | fuel_usage = mobility_spec_demand.mul(mobility_mileage).sum() |
||
68 | |||
69 | # fetch the energy content of the different fuel types |
||
70 | mobility_energy_content = pd.DataFrame( |
||
71 | cfg.get_dict("energy_per_liter"), index=["energy_per_liter [MJ/l]"] |
||
72 | )[["diesel", "petrol", "other"]] |
||
73 | |||
74 | mobility_energy_content["other"] = mobility_energy_content[other] |
||
75 | |||
76 | # Convert to MW????? BITTE GENAU!!! |
||
77 | energy_usage = fuel_usage.mul(mobility_energy_content).div(3600) |
||
78 | |||
79 | s = energy_usage.div(hours_of_the_year).transpose()[ |
||
80 | "energy_per_liter [MJ/l]" |
||
81 | ] |
||
82 | table["mobility_series"] = pd.DataFrame( |
||
83 | index=range(hours_of_the_year), columns=energy_usage.columns |
||
84 | ).fillna(1) |
||
85 | |||
86 | table["mobility_series"] = table["mobility_series"].mul(s, axis=1) |
||
87 | |||
88 | table["mobility_series"][other] += table["mobility_series"]["other"] |
||
89 | table["mobility_series"].drop("other", axis=1, inplace=True) |
||
90 | |||
91 | table["mobility_series"] = ( |
||
92 | table["mobility_series"].astype(float).round().astype(int) |
||
93 | ) |
||
94 | |||
95 | table["mobility"] = pd.DataFrame( |
||
96 | index=["diesel", "petrol", "electricity"], |
||
97 | columns=["efficiency", "source", "source_region"], |
||
98 | ) |
||
99 | |||
100 | for col in table["mobility"].columns: |
||
101 | for idx in table["mobility"].index: |
||
102 | if col != "source_region": |
||
103 | table["mobility"].loc[idx, col] = cfg.get(col, idx) |
||
104 | else: |
||
105 | table["mobility"].loc[idx, col] = "DE" |
||
106 | |||
107 | # Add "DE" as region level to be consistent to other tables |
||
108 | table["mobility"].index = pd.MultiIndex.from_product( |
||
109 | [["DE"], table["mobility"].index] |
||
110 | ) |
||
111 | table["mobility_series"].columns = pd.MultiIndex.from_product( |
||
112 | [["DE"], table["mobility_series"].columns] |
||
113 | ) |
||
114 | return table |
||
115 |