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