Conditions | 3 |
Total Lines | 64 |
Code Lines | 52 |
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 | import base64 |
||
52 | 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 = "MeterTracking" |
||
57 | |||
58 | # Column width |
||
59 | for i in range(ord('A'), ord('H')): |
||
60 | ws.column_dimensions[chr(i)].width = 30.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'] = 'Name' |
||
88 | ws['B6'].font = title_font |
||
89 | ws['B6'] = 'Space' |
||
90 | ws['C6'].font = title_font |
||
91 | ws['C6'] = 'Cost Center' |
||
92 | ws['D6'].font = title_font |
||
93 | ws['D6'] = 'Energy Category' |
||
94 | ws['E6'].font = title_font |
||
95 | ws['E6'] = 'Description' |
||
96 | ws['F6'].font = title_font |
||
97 | ws['F6'] = 'Start Value' |
||
98 | ws['G6'].font = title_font |
||
99 | ws['G6'] = 'End Value' |
||
100 | |||
101 | current_row_number = 7 |
||
102 | for i in range(0, len(report['meters'])): |
||
103 | ws['A' + str(current_row_number)] = report['meters'][i]['meter_name'] |
||
104 | ws['B' + str(current_row_number)] = report['meters'][i]['space_name'] |
||
105 | ws['C' + str(current_row_number)] = report['meters'][i]['cost_center_name'] |
||
106 | ws['D' + str(current_row_number)] = report['meters'][i]['energy_category_name'] |
||
107 | ws['E' + str(current_row_number)] = report['meters'][i]['description'] |
||
108 | ws['F' + str(current_row_number)] = report['meters'][i]['start_value'] |
||
109 | ws['G' + str(current_row_number)] = report['meters'][i]['end_value'] |
||
110 | current_row_number += 1 |
||
111 | |||
112 | filename = str(uuid.uuid4()) + '.xlsx' |
||
113 | wb.save(filename) |
||
114 | |||
115 | return filename |
||
116 |