Conditions | 3 |
Total Lines | 52 |
Code Lines | 28 |
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 | # -*- coding: utf-8 -*- |
||
35 | def get_ewi_data(): |
||
36 | """ |
||
37 | |||
38 | Returns |
||
39 | ------- |
||
40 | namedtuple |
||
41 | |||
42 | TODO: Keep this in deflex??? |
||
43 | |||
44 | Examples |
||
45 | -------- |
||
46 | # >>> ewi_data = get_ewi_data() |
||
47 | # >>> round(ewi_data.fuel_costs.loc["hard coal", "value"], 2) |
||
48 | # 11.28 |
||
49 | |||
50 | """ |
||
51 | # Download file |
||
52 | url = ( |
||
53 | "https://www.ewi.uni-koeln.de/cms/wp-content/uploads/2019/12" |
||
54 | "/EWI_Merit_Order_Tool_2019_1_4.xlsm" |
||
55 | ) |
||
56 | fn = os.path.join(cfg.get("paths", "general"), "ewi.xls") |
||
57 | tools.download_file(fn, url) |
||
58 | |||
59 | # Create named tuple with all sub tables |
||
60 | ewi_tables = { |
||
61 | "fuel_costs": {"skiprows": 7, "usecols": "C:F", "nrows": 7}, |
||
62 | "transport_costs": {"skiprows": 21, "usecols": "C:F", "nrows": 7}, |
||
63 | "variable_costs": {"skiprows": 31, "usecols": "C:F", "nrows": 8}, |
||
64 | "downtime_factor": { |
||
65 | "skiprows": 31, |
||
66 | "usecols": "H:K", |
||
67 | "nrows": 8, |
||
68 | "scale": 0.01, |
||
69 | }, |
||
70 | "emission": {"skiprows": 31, "usecols": "M:P", "nrows": 7}, |
||
71 | "co2_price": {"skiprows": 17, "usecols": "C:F", "nrows": 1}, |
||
72 | } |
||
73 | ewi_data = {} |
||
74 | cols = ["fuel", "value", "unit", "source"] |
||
75 | xls = pd.ExcelFile(fn) |
||
76 | for table in ewi_tables.keys(): |
||
77 | tmp = xls.parse("Start", header=[0], **ewi_tables[table]).replace( |
||
78 | TRANSLATION_FUEL |
||
79 | ) |
||
80 | tmp.drop_duplicates(tmp.columns[0], keep="first", inplace=True) |
||
81 | tmp.columns = cols |
||
82 | ewi_data[table] = tmp.set_index("fuel") |
||
83 | if "scale" in ewi_tables[table]: |
||
84 | ewi_data[table]["value"] *= ewi_tables[table]["scale"] |
||
85 | |||
86 | return SimpleNamespace(**ewi_data) |
||
87 |