| Conditions | 5 |
| Total Lines | 64 |
| Code Lines | 50 |
| Lines | 64 |
| Ratio | 100 % |
| 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 | import base64 |
||
| 52 | View Code Duplication | def generate_excel(report, space_name, reporting_start_datetime_local, reporting_end_datetime_local): |
|
| 53 | |||
| 54 | wb = Workbook() |
||
| 55 | ws = wb.active |
||
| 56 | ws.title = "OfflineMeter" |
||
| 57 | |||
| 58 | # Col width |
||
| 59 | for i in range(ord('A'), ord('L')): |
||
| 60 | ws.column_dimensions[chr(i)].width = 20.0 |
||
| 61 | |||
| 62 | # Head image |
||
| 63 | ws.row_dimensions[1].height = 105 |
||
| 64 | img = Image("excelexporters/myems.png") |
||
| 65 | ws.add_image(img, 'A1') |
||
| 66 | |||
| 67 | # Query Parameters |
||
| 68 | b_r_alignment = Alignment(vertical='bottom', |
||
| 69 | horizontal='right', |
||
| 70 | text_rotation=0, |
||
| 71 | wrap_text=True, |
||
| 72 | shrink_to_fit=False, |
||
| 73 | indent=0) |
||
| 74 | ws['A3'].alignment = b_r_alignment |
||
| 75 | ws['A3'] = 'Space:' |
||
| 76 | ws['B3'] = space_name |
||
| 77 | ws['A4'].alignment = b_r_alignment |
||
| 78 | ws['A4'] = 'Start Datetime:' |
||
| 79 | ws['B4'] = reporting_start_datetime_local |
||
| 80 | ws['A5'].alignment = b_r_alignment |
||
| 81 | ws['A5'] = 'End Datetime:' |
||
| 82 | ws['B5'] = reporting_end_datetime_local |
||
| 83 | |||
| 84 | # Title |
||
| 85 | title_font = Font(size=12, bold=True) |
||
| 86 | ws['A6'].font = title_font |
||
| 87 | ws['A6'] = 'ID' |
||
| 88 | ws['B6'].font = title_font |
||
| 89 | ws['B6'] = 'Name' |
||
| 90 | ws['C6'].font = title_font |
||
| 91 | ws['C6'] = 'Space' |
||
| 92 | |||
| 93 | ca_len = len(report['energycategories']) |
||
| 94 | for i in range(0, ca_len): |
||
| 95 | col = chr(ord('D') + i) |
||
| 96 | ws[col + '6'].font = title_font |
||
| 97 | ws[col + '6'] = report['energycategories'][i]['name'] + " (" + \ |
||
| 98 | report['energycategories'][i]['unit_of_measure'] + ")" |
||
| 99 | |||
| 100 | current_row_number = 7 |
||
| 101 | for i in range(0, len(report['offline_meters'])): |
||
| 102 | ws['A' + str(current_row_number)] = str(report['offline_meters'][i]['id']) |
||
| 103 | ws['B' + str(current_row_number)] = report['offline_meters'][i]['offline_meter_name'] |
||
| 104 | ws['C' + str(current_row_number)] = report['offline_meters'][i]['space_name'] |
||
| 105 | ca_len = len(report['offline_meters'][i]['values']) |
||
| 106 | for j in range(0, ca_len): |
||
| 107 | col = chr(ord('D') + j) |
||
| 108 | ws[col + str(current_row_number)] = report['offline_meters'][i]['values'][j] |
||
| 109 | |||
| 110 | current_row_number += 1 |
||
| 111 | |||
| 112 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 113 | wb.save(filename) |
||
| 114 | |||
| 115 | return filename |
||
| 116 |