| Conditions | 33 |
| Total Lines | 344 |
| Code Lines | 251 |
| Lines | 127 |
| Ratio | 36.92 % |
| 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.tenantstatistics.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 = 121 |
||
| 72 | |||
| 73 | for i in range(2, 36 + 1): |
||
| 74 | ws.row_dimensions[i].height = 30 |
||
| 75 | |||
| 76 | for i in range(38, 90 + 1): |
||
| 77 | ws.row_dimensions[i].height = 30 |
||
| 78 | |||
| 79 | # Col width |
||
| 80 | ws.column_dimensions['A'].width = 1.5 |
||
| 81 | ws.column_dimensions['B'].width = 20.0 |
||
| 82 | |||
| 83 | for i in range(ord('C'), ord('I')): |
||
| 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='1F496D') |
||
| 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=False, |
||
| 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=False, |
||
| 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=False, |
||
| 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=False, |
||
| 123 | # shrink_to_fit=False, |
||
| 124 | # indent=0) |
||
| 125 | |||
| 126 | # Img |
||
| 127 | img = Image("excelexporters/myems.png") |
||
| 128 | # img = Image("myems.png") |
||
| 129 | ws.add_image(img, 'B1') |
||
| 130 | |||
| 131 | # Title |
||
| 132 | ws['B3'].font = name_font |
||
| 133 | ws['B3'].alignment = b_r_alignment |
||
| 134 | ws['B3'] = 'Name:' |
||
| 135 | ws['C3'].border = b_border |
||
| 136 | ws['C3'].alignment = b_c_alignment |
||
| 137 | ws['C3'].font = name_font |
||
| 138 | ws['C3'] = name |
||
| 139 | |||
| 140 | ws['D3'].font = name_font |
||
| 141 | ws['D3'].alignment = b_r_alignment |
||
| 142 | ws['D3'] = 'Period:' |
||
| 143 | ws['E3'].border = b_border |
||
| 144 | ws['E3'].alignment = b_c_alignment |
||
| 145 | ws['E3'].font = name_font |
||
| 146 | ws['E3'] = period_type |
||
| 147 | |||
| 148 | ws['F3'].font = name_font |
||
| 149 | ws['F3'].alignment = b_r_alignment |
||
| 150 | ws['F3'] = 'Date:' |
||
| 151 | ws['G3'].border = b_border |
||
| 152 | ws['G3'].alignment = b_c_alignment |
||
| 153 | ws['G3'].font = name_font |
||
| 154 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
| 155 | ws.merge_cells("G3:H3") |
||
| 156 | |||
| 157 | if "reporting_period" not in report.keys() or \ |
||
| 158 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
| 159 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 160 | wb.save(filename) |
||
| 161 | |||
| 162 | return filename |
||
| 163 | ################################################# |
||
| 164 | # First: 统计分析 |
||
| 165 | # 5: title |
||
| 166 | # 6: table title |
||
| 167 | # 6~12 table_data |
||
| 168 | # Total: 8 rows |
||
| 169 | # if has not energy data: set low height for rows |
||
| 170 | ################################################# |
||
| 171 | reporting_period_data = report['reporting_period'] |
||
| 172 | |||
| 173 | has_energy_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_energy_data_flag = False |
||
| 179 | |||
| 180 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 181 | wb.save(filename) |
||
| 182 | |||
| 183 | return filename |
||
| 184 | |||
| 185 | View Code Duplication | if has_energy_data_flag: |
|
| 186 | ws['B5'].font = title_font |
||
| 187 | ws['B5'] = name + ' 统计分析' |
||
| 188 | |||
| 189 | category = reporting_period_data['names'] |
||
| 190 | |||
| 191 | # table_title |
||
| 192 | ws['B6'].fill = table_fill |
||
| 193 | ws['B6'].font = title_font |
||
| 194 | ws['B6'].alignment = c_c_alignment |
||
| 195 | ws['B6'] = '报告期' |
||
| 196 | ws['B6'].border = f_border |
||
| 197 | |||
| 198 | ws['C6'].font = title_font |
||
| 199 | ws['C6'].alignment = c_c_alignment |
||
| 200 | ws['C6'] = '算术平均数' |
||
| 201 | ws['C6'].border = f_border |
||
| 202 | |||
| 203 | ws['D6'].font = title_font |
||
| 204 | ws['D6'].alignment = c_c_alignment |
||
| 205 | ws['D6'] = '中位数' |
||
| 206 | ws['D6'].border = f_border |
||
| 207 | |||
| 208 | ws['E6'].font = title_font |
||
| 209 | ws['E6'].alignment = c_c_alignment |
||
| 210 | ws['E6'] = '最小值' |
||
| 211 | ws['E6'].border = f_border |
||
| 212 | |||
| 213 | ws['F6'].font = title_font |
||
| 214 | ws['F6'].alignment = c_c_alignment |
||
| 215 | ws['F6'] = '最大值' |
||
| 216 | ws['F6'].border = f_border |
||
| 217 | |||
| 218 | ws['G6'].font = title_font |
||
| 219 | ws['G6'].alignment = c_c_alignment |
||
| 220 | ws['G6'] = '样本标准差' |
||
| 221 | ws['G6'].border = f_border |
||
| 222 | |||
| 223 | ws['H6'].font = title_font |
||
| 224 | ws['H6'].alignment = c_c_alignment |
||
| 225 | ws['H6'] = '样本方差' |
||
| 226 | ws['H6'].border = f_border |
||
| 227 | |||
| 228 | # table_data |
||
| 229 | for i, value in enumerate(category): |
||
| 230 | row = i * 2 + 7 |
||
| 231 | ws['B' + str(row)].font = name_font |
||
| 232 | ws['B' + str(row)].alignment = c_c_alignment |
||
| 233 | ws['B' + str(row)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + " )" |
||
| 234 | ws['B' + str(row)].border = f_border |
||
| 235 | |||
| 236 | ws['B' + str(row + 1)].font = name_font |
||
| 237 | ws['B' + str(row + 1)].alignment = c_c_alignment |
||
| 238 | ws['B' + str(row + 1)] = "环比" |
||
| 239 | ws['B' + str(row + 1)].border = f_border |
||
| 240 | |||
| 241 | ws['C' + str(row)].font = name_font |
||
| 242 | ws['C' + str(row)].alignment = c_c_alignment |
||
| 243 | ws['C' + str(row)] = round(reporting_period_data['means'][i], 2) \ |
||
| 244 | if reporting_period_data['means'][i] is not None else '' |
||
| 245 | ws['C' + str(row)].border = f_border |
||
| 246 | |||
| 247 | ws['C' + str(row + 1)].font = name_font |
||
| 248 | ws['C' + str(row + 1)].alignment = c_c_alignment |
||
| 249 | ws['C' + str(row + 1)] = str(round(reporting_period_data['means_increment_rate'][i] * 100, 2)) + "%" \ |
||
| 250 | if reporting_period_data['means_increment_rate'][i] is not None else '0.00%' |
||
| 251 | ws['C' + str(row + 1)].border = f_border |
||
| 252 | |||
| 253 | ws['D' + str(row)].font = name_font |
||
| 254 | ws['D' + str(row)].alignment = c_c_alignment |
||
| 255 | ws['D' + str(row)] = round(reporting_period_data['medians'][i], 2) \ |
||
| 256 | if reporting_period_data['medians'][i] is not None else '' |
||
| 257 | ws['D' + str(row)].border = f_border |
||
| 258 | |||
| 259 | ws['D' + str(row + 1)].font = name_font |
||
| 260 | ws['D' + str(row + 1)].alignment = c_c_alignment |
||
| 261 | ws['D' + str(row + 1)] = str(round(reporting_period_data['medians_increment_rate'][i] * 100, 2)) + "%" \ |
||
| 262 | if reporting_period_data['medians_increment_rate'][i] is not None else '0.00%' |
||
| 263 | ws['D' + str(row + 1)].border = f_border |
||
| 264 | |||
| 265 | ws['E' + str(row)].font = name_font |
||
| 266 | ws['E' + str(row)].alignment = c_c_alignment |
||
| 267 | ws['E' + str(row)] = round(reporting_period_data['minimums'][i], 2) \ |
||
| 268 | if reporting_period_data['minimums'][i] is not None else '' |
||
| 269 | ws['E' + str(row)].border = f_border |
||
| 270 | |||
| 271 | ws['E' + str(row + 1)].font = name_font |
||
| 272 | ws['E' + str(row + 1)].alignment = c_c_alignment |
||
| 273 | ws['E' + str(row + 1)] = str(round(reporting_period_data['minimums_increment_rate'][i] * 100, 2)) + "%" \ |
||
| 274 | if reporting_period_data['minimums_increment_rate'][i] is not None else '0.00%' |
||
| 275 | ws['E' + str(row + 1)].border = f_border |
||
| 276 | |||
| 277 | ws['F' + str(row)].font = name_font |
||
| 278 | ws['F' + str(row)].alignment = c_c_alignment |
||
| 279 | ws['F' + str(row)] = round(reporting_period_data['maximums'][i], 2) \ |
||
| 280 | if reporting_period_data['maximums'][i] is not None else '' |
||
| 281 | ws['F' + str(row)].border = f_border |
||
| 282 | |||
| 283 | ws['F' + str(row + 1)].font = name_font |
||
| 284 | ws['F' + str(row + 1)].alignment = c_c_alignment |
||
| 285 | ws['F' + str(row + 1)] = str(round(reporting_period_data['maximums_increment_rate'][i] * 100, 2)) + "%" \ |
||
| 286 | if reporting_period_data['maximums_increment_rate'][i] is not None else '0.00%' |
||
| 287 | ws['F' + str(row + 1)].border = f_border |
||
| 288 | |||
| 289 | ws['G' + str(row)].font = name_font |
||
| 290 | ws['G' + str(row)].alignment = c_c_alignment |
||
| 291 | ws['G' + str(row)] = round(reporting_period_data['stdevs'][i], 2) \ |
||
| 292 | if reporting_period_data['stdevs'][i] is not None else '' |
||
| 293 | ws['G' + str(row)].border = f_border |
||
| 294 | |||
| 295 | ws['G' + str(row + 1)].font = name_font |
||
| 296 | ws['G' + str(row + 1)].alignment = c_c_alignment |
||
| 297 | ws['G' + str(row + 1)] = str(round(reporting_period_data['stdevs_increment_rate'][i] * 100, 2)) + "%" \ |
||
| 298 | if reporting_period_data['stdevs_increment_rate'][i] is not None else '0.00%' |
||
| 299 | ws['G' + str(row + 1)].border = f_border |
||
| 300 | |||
| 301 | ws['H' + str(row)].font = name_font |
||
| 302 | ws['H' + str(row)].alignment = c_c_alignment |
||
| 303 | ws['H' + str(row)] = round(reporting_period_data['variances'][i], 2) \ |
||
| 304 | if reporting_period_data['variances'][i] is not None else '' |
||
| 305 | ws['H' + str(row)].border = f_border |
||
| 306 | |||
| 307 | ws['H' + str(row + 1)].font = name_font |
||
| 308 | ws['H' + str(row + 1)].alignment = c_c_alignment |
||
| 309 | ws['H' + str(row + 1)] = str(round(reporting_period_data['variances_increment_rate'][i] * 100, 2)) + "%" \ |
||
| 310 | if reporting_period_data['variances_increment_rate'][i] is not None else '0.00%' |
||
| 311 | ws['H' + str(row + 1)].border = f_border |
||
| 312 | |||
| 313 | ################################################# |
||
| 314 | # Second: 报告期消耗 |
||
| 315 | # 14~14+row_title: chart |
||
| 316 | # 15+row_title: table title |
||
| 317 | # 15+row_title~: table_data |
||
| 318 | # Total: 1+row_title+time_len rows |
||
| 319 | ################################################ |
||
| 320 | |||
| 321 | has_timestamps_flag = True |
||
| 322 | if "timestamps" not in reporting_period_data.keys() or \ |
||
| 323 | reporting_period_data['timestamps'] is None or \ |
||
| 324 | len(reporting_period_data['timestamps']) == 0: |
||
| 325 | has_timestamps_flag = False |
||
| 326 | |||
| 327 | if has_timestamps_flag: |
||
| 328 | timestamps = reporting_period_data['timestamps'][0] |
||
| 329 | values = reporting_period_data['values'] |
||
| 330 | names = reporting_period_data['names'] |
||
| 331 | ca_len = len(names) |
||
| 332 | time_len = len(timestamps) |
||
| 333 | # title |
||
| 334 | row_title = 10 * ca_len |
||
| 335 | ws['B' + str(14+row_title)].font = title_font |
||
| 336 | ws['B' + str(14+row_title)] = name + ' 详细数据' |
||
| 337 | # table_title |
||
| 338 | ws['B' + str(15+row_title)].fill = table_fill |
||
| 339 | ws['B' + str(15+row_title)].font = name_font |
||
| 340 | ws['B' + str(15+row_title)].alignment = c_c_alignment |
||
| 341 | ws['B' + str(15+row_title)] = "时间" |
||
| 342 | ws['B' + str(15+row_title)].border = f_border |
||
| 343 | |||
| 344 | for i in range(0, ca_len): |
||
| 345 | col = chr(ord('C') + i) |
||
| 346 | |||
| 347 | ws[col + str(15+row_title)].font = name_font |
||
| 348 | ws[col + str(15+row_title)].alignment = c_c_alignment |
||
| 349 | ws[col + str(15+row_title)] = names[i] + " (" + reporting_period_data['units'][i] + ")" |
||
| 350 | ws[col + str(15+row_title)].border = f_border |
||
| 351 | # table_date |
||
| 352 | for i in range(0, time_len): |
||
| 353 | rows = i + 16 + 10 * ca_len |
||
| 354 | |||
| 355 | ws['B' + str(rows)].font = name_font |
||
| 356 | ws['B' + str(rows)].alignment = c_c_alignment |
||
| 357 | ws['B' + str(rows)] = timestamps[i] |
||
| 358 | ws['B' + str(rows)].border = f_border |
||
| 359 | |||
| 360 | for index in range(0, ca_len): |
||
| 361 | col = chr(ord('C') + index) |
||
| 362 | |||
| 363 | ws[col + str(rows)].font = name_font |
||
| 364 | ws[col + str(rows)].alignment = c_c_alignment |
||
| 365 | ws[col + str(rows)] = values[index][i] |
||
| 366 | ws[col + str(rows)].border = f_border |
||
| 367 | |||
| 368 | # 小计 |
||
| 369 | row_subtotals = 16 + time_len + 10 * ca_len |
||
| 370 | ws['B' + str(row_subtotals)].font = name_font |
||
| 371 | ws['B' + str(row_subtotals)].alignment = c_c_alignment |
||
| 372 | ws['B' + str(row_subtotals)] = "小计" |
||
| 373 | ws['B' + str(row_subtotals)].border = f_border |
||
| 374 | |||
| 375 | for i in range(0, ca_len): |
||
| 376 | col = chr(ord('C') + i) |
||
| 377 | |||
| 378 | ws[col + str(row_subtotals)].font = name_font |
||
| 379 | ws[col + str(row_subtotals)].alignment = c_c_alignment |
||
| 380 | ws[col + str(row_subtotals)] = reporting_period_data['subtotals'][i] |
||
| 381 | ws[col + str(row_subtotals)].border = f_border |
||
| 382 | |||
| 383 | # LineChart |
||
| 384 | for i in range(0, ca_len): |
||
| 385 | |||
| 386 | lc = LineChart() |
||
| 387 | lc.title = "报告期消耗" + names[i] + " (" + reporting_period_data['units'][i] + ")" |
||
| 388 | lc.style = 10 |
||
| 389 | lc.height = 8.40 # cm 1.05*8 1.05cm = 30 pt |
||
| 390 | lc.width = 31 |
||
| 391 | lc.x_axis.majorTickMark = 'in' |
||
| 392 | lc.y_axis.majorTickMark = 'in' |
||
| 393 | times = Reference(ws, min_col=2, min_row=16+row_title, max_row=15 + row_title + time_len) |
||
| 394 | lc_data = Reference(ws, min_col=3 + i, min_row=15+row_title, max_row=15 + row_title + time_len) |
||
| 395 | lc.add_data(lc_data, titles_from_data=True) |
||
| 396 | lc.set_categories(times) |
||
| 397 | ser = lc.series[0] |
||
| 398 | ser.marker.symbol = "diamond" |
||
| 399 | ser.marker.size = 5 |
||
| 400 | ws.add_chart(lc, 'B' + str(14 + 10 * i)) |
||
| 401 | |||
| 402 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 403 | wb.save(filename) |
||
| 404 | |||
| 405 | return filename |
||
| 406 |