| Conditions | 3 |
| Total Lines | 71 |
| Code Lines | 49 |
| 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 | """ |
||
| 17 | def prepare_input_data(): |
||
| 18 | url_temperature = ( |
||
| 19 | "https://oemof.org/wp-content/uploads/2025/12/temperature.csv" |
||
| 20 | ) |
||
| 21 | url_energy = "https://oemof.org/wp-content/uploads/2025/12/energy.csv" |
||
| 22 | |||
| 23 | print( |
||
| 24 | "Data is licensed from M. Schlemminger, T. Ohrdes, E. Schneider," |
||
| 25 | " and M. Knoop. Under Creative Commons Attribution 4.0 International" |
||
| 26 | " License. It is also available at doi: 10.5281/zenodo.5642902." |
||
| 27 | " (We use single family home 26 plus the south-facing PV" |
||
| 28 | " from that dataset.)" |
||
| 29 | ) |
||
| 30 | |||
| 31 | file_path = Path(__file__).parent |
||
| 32 | |||
| 33 | temperature_file = Path(file_path, "temperature.csv") |
||
| 34 | if not temperature_file.exists(): |
||
| 35 | urlretrieve(url_temperature, temperature_file) |
||
| 36 | df_temperature = pd.read_csv( |
||
| 37 | temperature_file, |
||
| 38 | index_col="Unix Epoch", |
||
| 39 | ) |
||
| 40 | timedelta = np.empty(len(df_temperature)) |
||
| 41 | timedelta[:-1] = ( |
||
| 42 | df_temperature.index[1:] - df_temperature.index[:-1] |
||
| 43 | ) / 3600 |
||
| 44 | timedelta[-1] = np.nan |
||
| 45 | |||
| 46 | df_temperature.index = pd.to_datetime( |
||
| 47 | df_temperature.index, |
||
| 48 | unit="s", |
||
| 49 | utc=True, |
||
| 50 | ) |
||
| 51 | |||
| 52 | building_area = 120 # m² (from publication) |
||
| 53 | specific_heat_demand = 60 # kWh/m²/a (educated guess) |
||
| 54 | holidays = dict(Germany().holidays(2019)) |
||
| 55 | |||
| 56 | # We estimate the heat demand from the ambient temperature using demandlib. |
||
| 57 | # This returns energy per time step in units of kWh. |
||
| 58 | df_temperature["heat demand (kWh)"] = demandlib.bdew.HeatBuilding( |
||
| 59 | df_temperature.index, |
||
| 60 | holidays=holidays, |
||
| 61 | temperature=df_temperature["Air Temperature (°C)"], |
||
| 62 | shlp_type="EFH", |
||
| 63 | building_class=1, |
||
| 64 | wind_class=1, |
||
| 65 | annual_heat_demand=building_area * specific_heat_demand, |
||
| 66 | name="EFH", |
||
| 67 | ).get_bdew_profile() |
||
| 68 | |||
| 69 | df_temperature["heat demand (W)"] = ( |
||
| 70 | df_temperature["heat demand (kWh)"] * 1e3 / timedelta |
||
| 71 | ) |
||
| 72 | |||
| 73 | energy_file = Path(file_path, "energy.csv") |
||
| 74 | if not energy_file.exists(): |
||
| 75 | urlretrieve(url_energy, energy_file) |
||
| 76 | df_engergy = pd.read_csv( |
||
| 77 | energy_file, |
||
| 78 | index_col=0, |
||
| 79 | ) |
||
| 80 | df_engergy.index = pd.to_datetime( |
||
| 81 | df_engergy.index, |
||
| 82 | unit="s", |
||
| 83 | utc=True, |
||
| 84 | ) |
||
| 85 | |||
| 86 | print(df_engergy) |
||
| 87 | print(df_temperature) |
||
| 88 | |||
| 92 |