| Conditions | 5 | 
| Total Lines | 101 | 
| Code Lines | 46 | 
| 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 demand series"] = pd.DataFrame( | ||
| 82 | index=range(hours_of_the_year), columns=energy_usage.columns | ||
| 83 | ).fillna(1) | ||
| 84 | |||
| 85 | table["mobility demand series"] = table["mobility demand series"].mul( | ||
| 86 | s, axis=1 | ||
| 87 | ) | ||
| 88 | |||
| 89 | table["mobility demand series"][other] += table["mobility demand series"][ | ||
| 90 | "other" | ||
| 91 | ] | ||
| 92 |     table["mobility demand series"].drop("other", axis=1, inplace=True) | ||
| 93 | |||
| 94 | table["mobility demand series"] = ( | ||
| 95 | table["mobility demand series"].astype(float).round().astype(int) | ||
| 96 | ) | ||
| 97 | |||
| 98 | table["mobility"] = pd.DataFrame( | ||
| 99 | index=["diesel", "petrol", "electricity"], | ||
| 100 | columns=["efficiency", "source", "source region"], | ||
| 101 | ) | ||
| 102 | |||
| 103 | for col in table["mobility"].columns: | ||
| 104 | for idx in table["mobility"].index: | ||
| 105 | section = "mobility: " + idx | ||
| 106 | table["mobility"].loc[idx, col] = cfg.get(section, col) | ||
| 107 | |||
| 108 | # Add "DE" as region level to be consistent to other tables | ||
| 109 | table["mobility"].index = pd.MultiIndex.from_product( | ||
| 110 | [["DE"], table["mobility"].index] | ||
| 111 | ) | ||
| 112 | table["mobility demand series"].columns = pd.MultiIndex.from_product( | ||
| 113 | [["DE"], table["mobility demand series"].columns] | ||
| 114 | ) | ||
| 115 | return table | ||
| 116 |