Conditions | 10 |
Total Lines | 86 |
Code Lines | 43 |
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:
Complex classes like scenario_builder.demand.get_heat_profiles_deflex() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """Create a basic scenario from the internal data structure. |
||
19 | def get_heat_profiles_deflex( |
||
20 | deflex_geo, year, time_index=None, weather_year=None, keep_unit=False |
||
21 | ): |
||
22 | """ |
||
23 | |||
24 | Parameters |
||
25 | ---------- |
||
26 | year |
||
27 | deflex_geo |
||
28 | time_index |
||
29 | weather_year |
||
30 | keep_unit |
||
31 | |||
32 | Returns |
||
33 | ------- |
||
34 | |||
35 | """ |
||
36 | # separate_regions=keep all demand connected to the region |
||
37 | separate_regions = cfg.get_list("creator", "separate_heat_regions") |
||
38 | # Add lower and upper cases to be not case sensitive |
||
39 | separate_regions = [x.upper() for x in separate_regions] + [ |
||
40 | x.lower() for x in separate_regions |
||
41 | ] |
||
42 | |||
43 | # add second fuel to first |
||
44 | # combine_fuels = cfg.get_dict("combine_heat_fuels") |
||
45 | combine_fuels = {"natural gas": "gas"} |
||
46 | |||
47 | # fuels to be dissolved per region |
||
48 | region_fuels = cfg.get_list("creator", "local_fuels") |
||
49 | |||
50 | fn = os.path.join( |
||
51 | cfg.get("paths", "demand"), |
||
52 | "heat_profiles_{year}_{map}".format(year=year, map=deflex_geo.name), |
||
53 | ) |
||
54 | |||
55 | demand_region = ( |
||
56 | demand_heat.get_heat_profiles_by_region( |
||
57 | deflex_geo, year, to_csv=fn, weather_year=weather_year |
||
58 | ) |
||
59 | .groupby(level=[0, 1], axis=1) |
||
60 | .sum() |
||
61 | ) |
||
62 | |||
63 | # Decentralised demand is combined to a nation-wide demand if not part |
||
64 | # of region_fuels. |
||
65 | regions = list( |
||
66 | set(demand_region.columns.get_level_values(0).unique()) |
||
67 | - set(separate_regions) |
||
68 | ) |
||
69 | |||
70 | # If region_fuels is 'all' fetch all fuels to be local. |
||
71 | if "all" in region_fuels: |
||
72 | region_fuels = demand_region.columns.get_level_values(1).unique() |
||
73 | |||
74 | for fuel in demand_region.columns.get_level_values(1).unique(): |
||
75 | demand_region["DE", fuel] = 0 |
||
76 | |||
77 | for region in regions: |
||
78 | for f1, f2 in combine_fuels.items(): |
||
79 | demand_region[region, f1] += demand_region[region, f2] |
||
80 | demand_region.drop((region, f2), axis=1, inplace=True) |
||
81 | cols = list(set(demand_region[region].columns) - set(region_fuels)) |
||
82 | for col in cols: |
||
83 | demand_region["DE", col] += demand_region[region, col] |
||
84 | demand_region.drop((region, col), axis=1, inplace=True) |
||
85 | |||
86 | if time_index is not None: |
||
87 | demand_region.index = time_index |
||
88 | |||
89 | if not keep_unit: |
||
90 | msg = ( |
||
91 | "The unit of the source is 'TJ'. " |
||
92 | "Will be divided by {0} to get 'MWh'." |
||
93 | ) |
||
94 | converter = 0.0036 |
||
95 | demand_region = demand_region.div(converter) |
||
96 | logging.debug(msg.format(converter)) |
||
97 | |||
98 | demand_region.sort_index(1, inplace=True) |
||
99 | |||
100 | for c in demand_region.columns: |
||
101 | if demand_region[c].sum() == 0: |
||
102 | demand_region.drop(c, axis=1, inplace=True) |
||
103 | |||
104 | return demand_region |
||
105 | |||
201 |