| Conditions | 35 | 
| Total Lines | 330 | 
| Code Lines | 241 | 
| 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 excelexporters.equipmentoutput.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  | 
            ||
| 65 | def generate_excel(report,  | 
            ||
| 66 | name,  | 
            ||
| 67 | reporting_start_datetime_local,  | 
            ||
| 68 | reporting_end_datetime_local,  | 
            ||
| 69 | period_type):  | 
            ||
| 70 | wb = Workbook()  | 
            ||
| 71 | ws = wb.active  | 
            ||
| 72 | |||
| 73 | # Row height  | 
            ||
| 74 | ws.row_dimensions[1].height = 102  | 
            ||
| 75 | for i in range(2, 2000 + 1):  | 
            ||
| 76 | ws.row_dimensions[i].height = 42  | 
            ||
| 77 | |||
| 78 | # Col width  | 
            ||
| 79 | ws.column_dimensions['A'].width = 1.5  | 
            ||
| 80 | |||
| 81 | ws.column_dimensions['B'].width = 25.0  | 
            ||
| 82 | |||
| 83 |     for i in range(ord('C'), ord('L')): | 
            ||
| 84 | ws.column_dimensions[chr(i)].width = 15.0  | 
            ||
| 85 | |||
| 86 | # Font  | 
            ||
| 87 | name_font = Font(name='Constantia', size=15, bold=True)  | 
            ||
| 88 | title_font = Font(name='宋体', size=15, bold=True)  | 
            ||
| 89 | data_font = Font(name='Franklin Gothic Book', size=11)  | 
            ||
| 90 | |||
| 91 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D')  | 
            ||
| 92 | f_border = Border(left=Side(border_style='medium', color='00000000'),  | 
            ||
| 93 | right=Side(border_style='medium', color='00000000'),  | 
            ||
| 94 | bottom=Side(border_style='medium', color='00000000'),  | 
            ||
| 95 | top=Side(border_style='medium', color='00000000')  | 
            ||
| 96 | )  | 
            ||
| 97 | b_border = Border(  | 
            ||
| 98 | bottom=Side(border_style='medium', color='00000000'),  | 
            ||
| 99 | )  | 
            ||
| 100 | |||
| 101 | b_c_alignment = Alignment(vertical='bottom',  | 
            ||
| 102 | horizontal='center',  | 
            ||
| 103 | text_rotation=0,  | 
            ||
| 104 | wrap_text=True,  | 
            ||
| 105 | shrink_to_fit=False,  | 
            ||
| 106 | indent=0)  | 
            ||
| 107 | c_c_alignment = Alignment(vertical='center',  | 
            ||
| 108 | horizontal='center',  | 
            ||
| 109 | text_rotation=0,  | 
            ||
| 110 | wrap_text=True,  | 
            ||
| 111 | shrink_to_fit=False,  | 
            ||
| 112 | indent=0)  | 
            ||
| 113 | b_r_alignment = Alignment(vertical='bottom',  | 
            ||
| 114 | horizontal='right',  | 
            ||
| 115 | text_rotation=0,  | 
            ||
| 116 | wrap_text=True,  | 
            ||
| 117 | shrink_to_fit=False,  | 
            ||
| 118 | indent=0)  | 
            ||
| 119 | c_r_alignment = Alignment(vertical='bottom',  | 
            ||
| 120 | horizontal='center',  | 
            ||
| 121 | text_rotation=0,  | 
            ||
| 122 | wrap_text=True,  | 
            ||
| 123 | shrink_to_fit=False,  | 
            ||
| 124 | indent=0)  | 
            ||
| 125 | # Img  | 
            ||
| 126 |     img = Image("excelexporters/myems.png") | 
            ||
| 127 | img.width = img.width * 0.85  | 
            ||
| 128 | img.height = img.height * 0.85  | 
            ||
| 129 |     # img = Image("myems.png") | 
            ||
| 130 | ws.add_image(img, 'B1')  | 
            ||
| 131 | |||
| 132 | # Title  | 
            ||
| 133 | ws.row_dimensions[3].height = 60  | 
            ||
| 134 | |||
| 135 | ws['B3'].font = name_font  | 
            ||
| 136 | ws['B3'].alignment = b_r_alignment  | 
            ||
| 137 | ws['B3'] = 'Name:'  | 
            ||
| 138 | ws['C3'].border = b_border  | 
            ||
| 139 | ws['C3'].alignment = b_c_alignment  | 
            ||
| 140 | ws['C3'].font = name_font  | 
            ||
| 141 | ws['C3'] = name  | 
            ||
| 142 | |||
| 143 | ws['D3'].font = name_font  | 
            ||
| 144 | ws['D3'].alignment = b_r_alignment  | 
            ||
| 145 | ws['D3'] = 'Period:'  | 
            ||
| 146 | ws['E3'].border = b_border  | 
            ||
| 147 | ws['E3'].alignment = b_c_alignment  | 
            ||
| 148 | ws['E3'].font = name_font  | 
            ||
| 149 | ws['E3'] = period_type  | 
            ||
| 150 | |||
| 151 | ws['F3'].font = name_font  | 
            ||
| 152 | ws['F3'].alignment = b_r_alignment  | 
            ||
| 153 | ws['F3'] = 'Date:'  | 
            ||
| 154 | ws['G3'].border = b_border  | 
            ||
| 155 | ws['G3'].alignment = b_c_alignment  | 
            ||
| 156 | ws['G3'].font = name_font  | 
            ||
| 157 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local  | 
            ||
| 158 |     ws.merge_cells("G3:H3") | 
            ||
| 159 | |||
| 160 | if "reporting_period" not in report.keys() or \  | 
            ||
| 161 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0:  | 
            ||
| 162 | filename = str(uuid.uuid4()) + '.xlsx'  | 
            ||
| 163 | wb.save(filename)  | 
            ||
| 164 | |||
| 165 | return filename  | 
            ||
| 166 | |||
| 167 | ##################################  | 
            ||
| 168 | |||
| 169 | current_row_number = 6  | 
            ||
| 170 | |||
| 171 | reporting_period_data = report['reporting_period']  | 
            ||
| 172 | |||
| 173 | has_names_data_flag = True  | 
            ||
| 174 | |||
| 175 | if "names" not in reporting_period_data.keys() or \  | 
            ||
| 176 | reporting_period_data['names'] is None or \  | 
            ||
| 177 | len(reporting_period_data['names']) == 0:  | 
            ||
| 178 | has_names_data_flag = False  | 
            ||
| 179 | |||
| 180 | if has_names_data_flag:  | 
            ||
| 181 | ws['B' + str(current_row_number)].font = title_font  | 
            ||
| 182 | ws['B' + str(current_row_number)] = name + ' 报告期产出'  | 
            ||
| 183 | |||
| 184 | current_row_number += 1  | 
            ||
| 185 | |||
| 186 | category = reporting_period_data['names']  | 
            ||
| 187 | ca_len = len(category)  | 
            ||
| 188 | |||
| 189 | ws.row_dimensions[current_row_number].height = 60  | 
            ||
| 190 | ws['B' + str(current_row_number)].fill = table_fill  | 
            ||
| 191 | ws['B' + str(current_row_number)].border = f_border  | 
            ||
| 192 | |||
| 193 | col = 'C'  | 
            ||
| 194 | |||
| 195 | for i in range(0, ca_len):  | 
            ||
| 196 | ws[col + str(current_row_number)].fill = table_fill  | 
            ||
| 197 | ws[col + str(current_row_number)].font = name_font  | 
            ||
| 198 | ws[col + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 199 | ws[col + str(current_row_number)].border = f_border  | 
            ||
| 200 | ws[col + str(current_row_number)] = \  | 
            ||
| 201 |                 reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" | 
            ||
| 202 | |||
| 203 | col = chr(ord(col) + 1)  | 
            ||
| 204 | |||
| 205 | current_row_number += 1  | 
            ||
| 206 | |||
| 207 | ws['B' + str(current_row_number)].font = title_font  | 
            ||
| 208 | ws['B' + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 209 | ws['B' + str(current_row_number)].border = f_border  | 
            ||
| 210 | ws['B' + str(current_row_number)] = '产出'  | 
            ||
| 211 | |||
| 212 | col = 'C'  | 
            ||
| 213 | |||
| 214 | for i in range(0, ca_len):  | 
            ||
| 215 | ws[col + str(current_row_number)].font = name_font  | 
            ||
| 216 | ws[col + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 217 | ws[col + str(current_row_number)].border = f_border  | 
            ||
| 218 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2)  | 
            ||
| 219 | |||
| 220 | col = chr(ord(col) + 1)  | 
            ||
| 221 | |||
| 222 | current_row_number += 1  | 
            ||
| 223 | |||
| 224 | # ws['B' + str(current_row_number)].font = title_font  | 
            ||
| 225 | # ws['B' + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 226 | # ws['B' + str(current_row_number)].border = f_border  | 
            ||
| 227 | # ws['B' + str(current_row_number)] = '单位面积值'  | 
            ||
| 228 | #  | 
            ||
| 229 | # col = 'C'  | 
            ||
| 230 | #  | 
            ||
| 231 | # for i in range(0, ca_len):  | 
            ||
| 232 | # ws[col + str(current_row_number)].font = name_font  | 
            ||
| 233 | # ws[col + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 234 | # ws[col + str(current_row_number)].border = f_border  | 
            ||
| 235 | # ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_per_unit_area'][i], 2)  | 
            ||
| 236 | #  | 
            ||
| 237 | # col = chr(ord(col) + 1)  | 
            ||
| 238 | #  | 
            ||
| 239 | # current_row_number += 1  | 
            ||
| 240 | |||
| 241 | ws['B' + str(current_row_number)].font = title_font  | 
            ||
| 242 | ws['B' + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 243 | ws['B' + str(current_row_number)].border = f_border  | 
            ||
| 244 | ws['B' + str(current_row_number)] = '环比'  | 
            ||
| 245 | |||
| 246 | col = 'C'  | 
            ||
| 247 | |||
| 248 | for i in range(0, ca_len):  | 
            ||
| 249 | ws[col + str(current_row_number)].font = name_font  | 
            ||
| 250 | ws[col + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 251 | ws[col + str(current_row_number)].border = f_border  | 
            ||
| 252 | ws[col + str(current_row_number)] = str(  | 
            ||
| 253 | round(reporting_period_data['increment_rates'][i] * 100, 2)) + '%' \  | 
            ||
| 254 | if reporting_period_data['increment_rates'][i] is not None else '-'  | 
            ||
| 255 | |||
| 256 | col = chr(ord(col) + 1)  | 
            ||
| 257 | |||
| 258 | current_row_number += 2  | 
            ||
| 259 | |||
| 260 | ####################################  | 
            ||
| 261 | |||
| 262 | has_values_data = True  | 
            ||
| 263 | has_timestamps_data = True  | 
            ||
| 264 | |||
| 265 | if 'values' not in reporting_period_data.keys() or \  | 
            ||
| 266 | reporting_period_data['values'] is None or \  | 
            ||
| 267 | len(reporting_period_data['values']) == 0:  | 
            ||
| 268 | has_values_data = False  | 
            ||
| 269 | |||
| 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 or \  | 
            ||
| 273 | len(reporting_period_data['timestamps'][0]) == 0:  | 
            ||
| 274 | has_timestamps_data = False  | 
            ||
| 275 | |||
| 276 | if has_values_data and has_timestamps_data:  | 
            ||
| 277 | ca_len = len(reporting_period_data['names'])  | 
            ||
| 278 | time = reporting_period_data['timestamps'][0]  | 
            ||
| 279 | |||
| 280 | ws['B' + str(current_row_number)].font = title_font  | 
            ||
| 281 | ws['B' + str(current_row_number)] = name + ' 详细数据'  | 
            ||
| 282 | |||
| 283 | current_row_number += 1  | 
            ||
| 284 | |||
| 285 | chart_start_row_number = current_row_number  | 
            ||
| 286 | |||
| 287 | current_row_number += ca_len * 6  | 
            ||
| 288 | table_start_row_number = current_row_number  | 
            ||
| 289 | |||
| 290 | ws.row_dimensions[current_row_number].height = 60  | 
            ||
| 291 | ws['B' + str(current_row_number)].fill = table_fill  | 
            ||
| 292 | ws['B' + str(current_row_number)].font = title_font  | 
            ||
| 293 | ws['B' + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 294 | ws['B' + str(current_row_number)].border = f_border  | 
            ||
| 295 | ws['B' + str(current_row_number)] = '日期时间'  | 
            ||
| 296 | |||
| 297 | col = 'C'  | 
            ||
| 298 | |||
| 299 | for i in range(0, ca_len):  | 
            ||
| 300 | ws[col + str(current_row_number)].fill = table_fill  | 
            ||
| 301 | ws[col + str(current_row_number)].font = title_font  | 
            ||
| 302 | ws[col + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 303 | ws[col + str(current_row_number)].border = f_border  | 
            ||
| 304 | ws[col + str(current_row_number)] = \  | 
            ||
| 305 |                     reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" | 
            ||
| 306 | col = chr(ord(col) + 1)  | 
            ||
| 307 | |||
| 308 | current_row_number += 1  | 
            ||
| 309 | |||
| 310 | for i in range(0, len(time)):  | 
            ||
| 311 | ws['B' + str(current_row_number)].font = title_font  | 
            ||
| 312 | ws['B' + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 313 | ws['B' + str(current_row_number)].border = f_border  | 
            ||
| 314 | ws['B' + str(current_row_number)] = time[i]  | 
            ||
| 315 | |||
| 316 | col = 'C'  | 
            ||
| 317 | for j in range(0, ca_len):  | 
            ||
| 318 | ws[col + str(current_row_number)].font = title_font  | 
            ||
| 319 | ws[col + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 320 | ws[col + str(current_row_number)].border = f_border  | 
            ||
| 321 | ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \  | 
            ||
| 322 | if reporting_period_data['values'][j][i] is not None else 0.00  | 
            ||
| 323 | col = chr(ord(col) + 1)  | 
            ||
| 324 | |||
| 325 | current_row_number += 1  | 
            ||
| 326 | |||
| 327 | table_end_row_number = current_row_number - 1  | 
            ||
| 328 | |||
| 329 | ws['B' + str(current_row_number)].font = title_font  | 
            ||
| 330 | ws['B' + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 331 | ws['B' + str(current_row_number)].border = f_border  | 
            ||
| 332 | ws['B' + str(current_row_number)] = '小计'  | 
            ||
| 333 | |||
| 334 | col = 'C'  | 
            ||
| 335 | |||
| 336 | for i in range(0, ca_len):  | 
            ||
| 337 | ws[col + str(current_row_number)].font = title_font  | 
            ||
| 338 | ws[col + str(current_row_number)].alignment = c_c_alignment  | 
            ||
| 339 | ws[col + str(current_row_number)].border = f_border  | 
            ||
| 340 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2)  | 
            ||
| 341 | col = chr(ord(col) + 1)  | 
            ||
| 342 | |||
| 343 | current_row_number += 2  | 
            ||
| 344 | |||
| 345 | format_time_width_number = 1.0  | 
            ||
| 346 | min_len_number = 1.0  | 
            ||
| 347 | min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0  | 
            ||
| 348 | |||
| 349 | if period_type == 'hourly':  | 
            ||
| 350 | format_time_width_number = 4.0  | 
            ||
| 351 | min_len_number = 2  | 
            ||
| 352 | min_width_number = 12.0  | 
            ||
| 353 | elif period_type == 'daily':  | 
            ||
| 354 | format_time_width_number = 2.5  | 
            ||
| 355 | min_len_number = 4  | 
            ||
| 356 | min_width_number = 14.0  | 
            ||
| 357 | elif period_type == 'monthly':  | 
            ||
| 358 | format_time_width_number = 2.1  | 
            ||
| 359 | min_len_number = 4  | 
            ||
| 360 | min_width_number = 12.4  | 
            ||
| 361 | elif period_type == 'yearly':  | 
            ||
| 362 | format_time_width_number = 1.5  | 
            ||
| 363 | min_len_number = 5  | 
            ||
| 364 | min_width_number = 11.5  | 
            ||
| 365 | |||
| 366 | for i in range(0, ca_len):  | 
            ||
| 367 | line = LineChart()  | 
            ||
| 368 | line.title = '报告期产出 - ' + \  | 
            ||
| 369 |                              reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" | 
            ||
| 370 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)  | 
            ||
| 371 | line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)  | 
            ||
| 372 | line.add_data(line_data, titles_from_data=True)  | 
            ||
| 373 | line.set_categories(labels)  | 
            ||
| 374 | line_data = line.series[0]  | 
            ||
| 375 | line_data.marker.symbol = "circle"  | 
            ||
| 376 | line_data.smooth = True  | 
            ||
| 377 | line.x_axis.crosses = 'min'  | 
            ||
| 378 | line.height = 8.25  | 
            ||
| 379 | line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number  | 
            ||
| 380 | if line.width > 24:  | 
            ||
| 381 | line.width = 24  | 
            ||
| 382 | line.dLbls = DataLabelList()  | 
            ||
| 383 | line.dLbls.dLblPos = 't'  | 
            ||
| 384 | line.dLbls.showVal = True  | 
            ||
| 385 | line.dLbls.showPercent = False  | 
            ||
| 386 | chart_col = 'B'  | 
            ||
| 387 | chart_cell = chart_col + str(chart_start_row_number)  | 
            ||
| 388 | chart_start_row_number += 6  | 
            ||
| 389 | ws.add_chart(line, chart_cell)  | 
            ||
| 390 | |||
| 391 | filename = str(uuid.uuid4()) + '.xlsx'  | 
            ||
| 392 | wb.save(filename)  | 
            ||
| 393 | |||
| 394 | return filename  | 
            ||
| 395 | |||
| 397 |