Conditions | 27 |
Total Lines | 307 |
Code Lines | 227 |
Lines | 169 |
Ratio | 55.05 % |
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 excelexporters.combinedequipmentload.generate_excel() 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 | import base64 |
||
62 | def generate_excel(report, |
||
63 | name, |
||
64 | reporting_start_datetime_local, |
||
65 | reporting_end_datetime_local, |
||
66 | period_type): |
||
67 | wb = Workbook() |
||
68 | ws = wb.active |
||
69 | |||
70 | # Row height |
||
71 | ws.row_dimensions[1].height = 102 |
||
72 | |||
73 | for i in range(2, 2000 + 1): |
||
74 | ws.row_dimensions[i].height = 42 |
||
75 | |||
76 | # Col width |
||
77 | ws.column_dimensions['A'].width = 1.5 |
||
78 | ws.column_dimensions['B'].width = 25.0 |
||
79 | |||
80 | for i in range(ord('C'), ord('L')): |
||
81 | ws.column_dimensions[chr(i)].width = 15.0 |
||
82 | |||
83 | # Font |
||
84 | name_font = Font(name='Constantia', size=15, bold=True) |
||
85 | title_font = Font(name='宋体', size=15, bold=True) |
||
86 | # data_font = Font(name='Franklin Gothic Book', size=11) |
||
87 | |||
88 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
89 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
90 | right=Side(border_style='medium', color='00000000'), |
||
91 | bottom=Side(border_style='medium', color='00000000'), |
||
92 | top=Side(border_style='medium', color='00000000') |
||
93 | ) |
||
94 | b_border = Border( |
||
95 | bottom=Side(border_style='medium', color='00000000'), |
||
96 | ) |
||
97 | |||
98 | b_c_alignment = Alignment(vertical='bottom', |
||
99 | horizontal='center', |
||
100 | text_rotation=0, |
||
101 | wrap_text=False, |
||
102 | shrink_to_fit=False, |
||
103 | indent=0) |
||
104 | c_c_alignment = Alignment(vertical='center', |
||
105 | horizontal='center', |
||
106 | text_rotation=0, |
||
107 | wrap_text=True, |
||
108 | shrink_to_fit=False, |
||
109 | indent=0) |
||
110 | b_r_alignment = Alignment(vertical='bottom', |
||
111 | horizontal='right', |
||
112 | text_rotation=0, |
||
113 | wrap_text=False, |
||
114 | shrink_to_fit=False, |
||
115 | indent=0) |
||
116 | # c_r_alignment = Alignment(vertical='bottom', |
||
117 | # horizontal='center', |
||
118 | # text_rotation=0, |
||
119 | # wrap_text=False, |
||
120 | # shrink_to_fit=False, |
||
121 | # indent=0) |
||
122 | |||
123 | # Img |
||
124 | img = Image("excelexporters/myems.png") |
||
125 | # img = Image("myems.png") |
||
126 | img.width = img.width * 0.85 |
||
127 | img.height = img.height * 0.85 |
||
128 | ws.add_image(img, 'B1') |
||
129 | |||
130 | # Title |
||
131 | ws['B3'].font = name_font |
||
132 | ws['B3'].alignment = b_r_alignment |
||
133 | ws['B3'] = 'Name:' |
||
134 | ws['C3'].border = b_border |
||
135 | ws['C3'].alignment = b_c_alignment |
||
136 | ws['C3'].font = name_font |
||
137 | ws['C3'] = name |
||
138 | |||
139 | ws['D3'].font = name_font |
||
140 | ws['D3'].alignment = b_r_alignment |
||
141 | ws['D3'] = 'Period:' |
||
142 | ws['E3'].border = b_border |
||
143 | ws['E3'].alignment = b_c_alignment |
||
144 | ws['E3'].font = name_font |
||
145 | ws['E3'] = period_type |
||
146 | |||
147 | ws['F3'].font = name_font |
||
148 | ws['F3'].alignment = b_r_alignment |
||
149 | ws['F3'] = 'Date:' |
||
150 | ws['G3'].border = b_border |
||
151 | ws['G3'].alignment = b_c_alignment |
||
152 | ws['G3'].font = name_font |
||
153 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
154 | ws.merge_cells("G3:H3") |
||
155 | |||
156 | if "reporting_period" not in report.keys() or \ |
||
157 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
158 | filename = str(uuid.uuid4()) + '.xlsx' |
||
159 | wb.save(filename) |
||
160 | |||
161 | return filename |
||
162 | ################################################# |
||
163 | # First: 统计分析 |
||
164 | # 6: title |
||
165 | # 7: table title |
||
166 | # 8~2*ca_len table_data |
||
167 | ################################################# |
||
168 | reporting_period_data = report['reporting_period'] |
||
169 | |||
170 | has_energy_data_flag = True |
||
171 | |||
172 | if "names" not in reporting_period_data.keys() or \ |
||
173 | reporting_period_data['names'] is None or \ |
||
174 | len(reporting_period_data['names']) == 0: |
||
175 | has_energy_data_flag = False |
||
176 | |||
177 | filename = str(uuid.uuid4()) + '.xlsx' |
||
178 | wb.save(filename) |
||
179 | |||
180 | return filename |
||
181 | |||
182 | View Code Duplication | if has_energy_data_flag: |
|
183 | ws['B6'].font = title_font |
||
184 | ws['B6'] = name + ' 统计分析' |
||
185 | |||
186 | category = reporting_period_data['names'] |
||
187 | |||
188 | # table_title |
||
189 | ws['B7'].fill = table_fill |
||
190 | ws['B7'].font = title_font |
||
191 | ws['B7'].alignment = c_c_alignment |
||
192 | ws['B7'] = '报告期' |
||
193 | ws['B7'].border = f_border |
||
194 | |||
195 | ws['C7'].font = title_font |
||
196 | ws['C7'].alignment = c_c_alignment |
||
197 | ws['C7'] = '平均负荷' |
||
198 | ws['C7'].border = f_border |
||
199 | |||
200 | ws['D7'].font = title_font |
||
201 | ws['D7'].alignment = c_c_alignment |
||
202 | ws['D7'] = '最大负荷' |
||
203 | ws['D7'].border = f_border |
||
204 | |||
205 | ws['E7'].font = title_font |
||
206 | ws['E7'].alignment = c_c_alignment |
||
207 | ws['E7'] = '负荷系数' |
||
208 | ws['E7'].border = f_border |
||
209 | |||
210 | # table_data |
||
211 | |||
212 | for i, value in enumerate(category): |
||
213 | row = i * 2 + 8 |
||
214 | ws['B' + str(row)].font = name_font |
||
215 | ws['B' + str(row)].alignment = c_c_alignment |
||
216 | ws['B' + str(row)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + "/H )" |
||
217 | ws['B' + str(row)].border = f_border |
||
218 | |||
219 | ws['B' + str(row + 1)].font = name_font |
||
220 | ws['B' + str(row + 1)].alignment = c_c_alignment |
||
221 | ws['B' + str(row + 1)] = "环比" |
||
222 | ws['B' + str(row + 1)].border = f_border |
||
223 | |||
224 | ws['C' + str(row)].font = name_font |
||
225 | ws['C' + str(row)].alignment = c_c_alignment |
||
226 | ws['C' + str(row)] = round(reporting_period_data['averages'][i], 2) \ |
||
227 | if reporting_period_data['averages'][i] is not None else '' |
||
228 | ws['C' + str(row)].border = f_border |
||
229 | ws['C' + str(row)].number_format = '0.00' |
||
230 | |||
231 | ws['C' + str(row + 1)].font = name_font |
||
232 | ws['C' + str(row + 1)].alignment = c_c_alignment |
||
233 | ws['C' + str(row + 1)] = str(round(reporting_period_data['averages_increment_rate'][i] * 100, 2)) + "%" \ |
||
234 | if reporting_period_data['averages_increment_rate'][i] is not None else '0.00%' |
||
235 | ws['C' + str(row + 1)].border = f_border |
||
236 | |||
237 | ws['D' + str(row)].font = name_font |
||
238 | ws['D' + str(row)].alignment = c_c_alignment |
||
239 | ws['D' + str(row)] = round(reporting_period_data['maximums'][i], 2) \ |
||
240 | if reporting_period_data['maximums'][i] is not None else '' |
||
241 | ws['D' + str(row)].border = f_border |
||
242 | ws['D' + str(row)].number_format = '0.00' |
||
243 | |||
244 | ws['D' + str(row + 1)].font = name_font |
||
245 | ws['D' + str(row + 1)].alignment = c_c_alignment |
||
246 | ws['D' + str(row + 1)] = str(round(reporting_period_data['maximums_increment_rate'][i] * 100, 2)) + "%" \ |
||
247 | if reporting_period_data['maximums_increment_rate'][i] is not None else '0.00%' |
||
248 | ws['D' + str(row + 1)].border = f_border |
||
249 | |||
250 | ws['E' + str(row)].font = name_font |
||
251 | ws['E' + str(row)].alignment = c_c_alignment |
||
252 | ws['E' + str(row)] = round(reporting_period_data['factors'][i], 2) \ |
||
253 | if reporting_period_data['factors'][i] is not None else '' |
||
254 | ws['E' + str(row)].border = f_border |
||
255 | ws['E' + str(row)].number_format = '0.00' |
||
256 | |||
257 | ws['E' + str(row + 1)].font = name_font |
||
258 | ws['E' + str(row + 1)].alignment = c_c_alignment |
||
259 | ws['E' + str(row + 1)] = str(round(reporting_period_data['factors_increment_rate'][i] * 100, 2)) + "%" \ |
||
260 | if reporting_period_data['factors_increment_rate'][i] is not None else '0.00%' |
||
261 | ws['E' + str(row + 1)].border = f_border |
||
262 | |||
263 | ######################################################## |
||
264 | # Third: 详细数据 |
||
265 | # row_sat~ row_sat + 6*cal_len: line |
||
266 | # row_da: table title |
||
267 | # row_da + 1~: table_data |
||
268 | ######################################################## |
||
269 | has_timestamps_flag = True |
||
270 | if "timestamps" not in reporting_period_data.keys() or \ |
||
271 | reporting_period_data['timestamps'] is None or \ |
||
272 | len(reporting_period_data['timestamps']) == 0: |
||
273 | has_timestamps_flag = False |
||
274 | |||
275 | View Code Duplication | if has_timestamps_flag: |
|
276 | timestamps = reporting_period_data['timestamps'][0] |
||
277 | names = reporting_period_data['names'] |
||
278 | ca_len = len(names) |
||
279 | time_len = len(timestamps) |
||
280 | # row_lines == the number of rows of lines |
||
281 | row_lines = 6 * ca_len |
||
282 | # row_sat == the number of rows of statistical analysis table |
||
283 | row_sat = 9 + 2 * ca_len |
||
284 | # row_da == the number of rows of Detailed data |
||
285 | row_da = row_sat + row_lines + 1 |
||
286 | |||
287 | ws['B' + str(row_da)].font = title_font |
||
288 | ws['B' + str(row_da)] = name + ' 详细数据' |
||
289 | # table_title |
||
290 | ws['B' + str(row_da + 1)].fill = table_fill |
||
291 | ws['B' + str(row_da + 1)].font = name_font |
||
292 | ws['B' + str(row_da + 1)].alignment = c_c_alignment |
||
293 | ws['B' + str(row_da + 1)] = "日期时间" |
||
294 | ws['B' + str(row_da + 1)].border = f_border |
||
295 | |||
296 | for i in range(0, ca_len): |
||
297 | col_average = chr(ord('C') + 2 * i) |
||
298 | col_maximum = chr(ord('D') + 2 * i) |
||
299 | |||
300 | ws[col_average + str(row_da + 1)].font = name_font |
||
301 | ws[col_average + str(row_da + 1)].alignment = c_c_alignment |
||
302 | ws[col_average + str(row_da + 1)] = names[i] + " 平均负荷(" + reporting_period_data['units'][ |
||
303 | i] + "/H)" |
||
304 | ws[col_average + str(row_da + 1)].border = f_border |
||
305 | |||
306 | ws[col_maximum + str(row_da + 1)].font = name_font |
||
307 | ws[col_maximum + str(row_da + 1)].alignment = c_c_alignment |
||
308 | ws[col_maximum + str(row_da + 1)] = names[i] + " 最大负荷(" + reporting_period_data['units'][ |
||
309 | i] + "/H)" |
||
310 | ws[col_maximum + str(row_da + 1)].border = f_border |
||
311 | # table_date |
||
312 | for i in range(0, time_len): |
||
313 | rows = i + row_da + 2 |
||
314 | |||
315 | ws['B' + str(rows)].font = name_font |
||
316 | ws['B' + str(rows)].alignment = c_c_alignment |
||
317 | ws['B' + str(rows)] = timestamps[i] |
||
318 | ws['B' + str(rows)].border = f_border |
||
319 | |||
320 | for index in range(0, ca_len): |
||
321 | col_average = chr(ord('C') + 2 * index) |
||
322 | col_maximum = chr(ord('D') + 2 * index) |
||
323 | |||
324 | ws[col_average + str(rows)].font = name_font |
||
325 | ws[col_average + str(rows)].alignment = c_c_alignment |
||
326 | ws[col_average + str(rows)] = reporting_period_data['sub_averages'][index][i] \ |
||
327 | if reporting_period_data['sub_maximums'][index] is not None else '' |
||
328 | ws[col_average + str(rows)].number_format = '0.00' |
||
329 | ws[col_average + str(rows)].border = f_border |
||
330 | |||
331 | ws[col_maximum + str(rows)].font = name_font |
||
332 | ws[col_maximum + str(rows)].alignment = c_c_alignment |
||
333 | ws[col_maximum + str(rows)] = reporting_period_data['sub_maximums'][index][i] \ |
||
334 | if reporting_period_data['sub_maximums'][index] is not None else '' |
||
335 | ws[col_maximum + str(rows)].number_format = '0.00' |
||
336 | ws[col_maximum + str(rows)].border = f_border |
||
337 | |||
338 | # LineChart |
||
339 | for i in range(0, ca_len): |
||
340 | lc = LineChart() |
||
341 | lc.title = "报告期 最大负荷" |
||
342 | lc.style = 10 |
||
343 | lc.x_axis.majorTickMark = 'in' |
||
344 | lc.y_axis.majorTickMark = 'in' |
||
345 | lc.smooth = True |
||
346 | lc.x_axis.crosses = 'min' |
||
347 | lc.height = 8.25 |
||
348 | lc.width = 24 |
||
349 | lc.dLbls = DataLabelList() |
||
350 | lc.dLbls.dLblPos = 't' |
||
351 | lc.dLbls.showVal = True |
||
352 | times = Reference(ws, min_col=2, min_row=row_da + 2, |
||
353 | max_row=row_da + 2 + time_len) |
||
354 | lc_data = Reference(ws, min_col=2 + 2 * (i+1), min_row=row_da + 1, |
||
355 | max_row=row_da + 1 + time_len) |
||
356 | lc.add_data(lc_data, titles_from_data=True) |
||
357 | lc.set_categories(times) |
||
358 | ser = lc.series[0] |
||
359 | ser.marker.symbol = "diamond" |
||
360 | ser.marker.size = 5 |
||
361 | chart_col = 'B' |
||
362 | chart_cell = str(row_sat + 6 * i) |
||
363 | ws.add_chart(lc, chart_col + chart_cell) |
||
364 | |||
365 | filename = str(uuid.uuid4()) + '.xlsx' |
||
366 | wb.save(filename) |
||
367 | |||
368 | return filename |
||
369 |