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