Conditions | 10 |
Total Lines | 97 |
Code Lines | 83 |
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.metertracking.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 |
||
57 | def generate_excel(report, space_name, energy_category_name, reporting_start_datetime_local, reporting_end_datetime_local, language): |
||
58 | |||
59 | locale_path = './i18n/' |
||
60 | if language == 'zh_CN': |
||
61 | trans = gettext.translation('myems', locale_path, languages=['zh_CN']) |
||
62 | elif language == 'de': |
||
63 | trans = gettext.translation('myems', locale_path, languages=['de']) |
||
64 | elif language == 'en': |
||
65 | trans = gettext.translation('myems', locale_path, languages=['en']) |
||
66 | else: |
||
67 | trans = gettext.translation('myems', locale_path, languages=['en']) |
||
68 | trans.install() |
||
69 | _ = trans.gettext |
||
70 | |||
71 | wb = Workbook() |
||
72 | ws = wb.active |
||
73 | ws.title = "MeterTracking" |
||
74 | |||
75 | # Column width |
||
76 | for i in range(ord('A'), ord('I')): |
||
77 | ws.column_dimensions[chr(i)].width = 25.0 |
||
78 | |||
79 | # Head image |
||
80 | ws.row_dimensions[1].height = 105 |
||
81 | img = Image("excelexporters/myems.png") |
||
82 | ws.add_image(img, 'A1') |
||
83 | |||
84 | # Query Parameters |
||
85 | b_r_alignment = Alignment(vertical='bottom', |
||
86 | horizontal='right', |
||
87 | text_rotation=0, |
||
88 | wrap_text=True, |
||
89 | shrink_to_fit=False, |
||
90 | indent=0) |
||
91 | ws['A3'].alignment = b_r_alignment |
||
92 | ws['A3'] = _('Space') + ':' |
||
93 | ws['B3'] = space_name |
||
94 | ws['A4'].alignment = b_r_alignment |
||
95 | ws['A4'] = _('Start Datetime') + ':' |
||
96 | ws['B4'] = reporting_start_datetime_local |
||
97 | ws['A5'].alignment = b_r_alignment |
||
98 | ws['A5'] = _('End Datetime') + ':' |
||
99 | ws['B5'] = reporting_end_datetime_local |
||
100 | ws['A6'].alignment = b_r_alignment |
||
101 | ws['A6'] = _('Start Integrity Rate') + ':' |
||
102 | ws['B6'] = (str(report['start_integrity_rate'] * Decimal(100.0)) + '%') \ |
||
103 | if report['start_integrity_rate'] is not None else None |
||
104 | ws['A7'].alignment = b_r_alignment |
||
105 | ws['A7'] = _('End Integrity Rate') + ':' |
||
106 | ws['B7'] = (str(report['end_integrity_rate'] * Decimal(100.0)) + '%') \ |
||
107 | if report['end_integrity_rate'] is not None else None |
||
108 | ws['A8'].alignment = b_r_alignment |
||
109 | ws['A8'] = _('Full Integrity Rate') + ':' |
||
110 | ws['B8'] = (str(report['full_integrity_rate'] * Decimal(100.0)) + '%') \ |
||
111 | if report['full_integrity_rate'] is not None else None |
||
112 | ws['A9'].alignment = b_r_alignment |
||
113 | ws['A9'] = _('Energy Category') + ':' |
||
114 | ws['B9'] = energy_category_name if energy_category_name is not None else _('All') |
||
115 | |||
116 | # Title |
||
117 | title_font = Font(size=12, bold=True) |
||
118 | ws['A10'].font = title_font |
||
119 | ws['A10'] = _('ID') |
||
120 | ws['B10'].font = title_font |
||
121 | ws['B10'] = _('Name') |
||
122 | ws['C10'].font = title_font |
||
123 | ws['C10'] = _('Space') |
||
124 | ws['D10'].font = title_font |
||
125 | ws['D10'] = _('Cost Center') |
||
126 | ws['E10'].font = title_font |
||
127 | ws['E10'] = _('Energy Category') |
||
128 | ws['F10'].font = title_font |
||
129 | ws['F10'] = _('Description') |
||
130 | ws['G10'].font = title_font |
||
131 | ws['G10'] = _('Start Value') |
||
132 | ws['H10'].font = title_font |
||
133 | ws['H10'] = _('End Value') |
||
134 | ws['I10'].font = title_font |
||
135 | ws['I10'] = _('Difference Value') |
||
136 | |||
137 | current_row_number = 11 |
||
138 | for i in range(0, len(report['meters'])): |
||
139 | ws['A' + str(current_row_number)] = report['meters'][i]['id'] |
||
140 | ws['B' + str(current_row_number)] = report['meters'][i]['meter_name'] |
||
141 | ws['C' + str(current_row_number)] = report['meters'][i]['space_name'] |
||
142 | ws['D' + str(current_row_number)] = report['meters'][i]['cost_center_name'] |
||
143 | ws['E' + str(current_row_number)] = report['meters'][i]['energy_category_name'] |
||
144 | ws['F' + str(current_row_number)] = report['meters'][i]['description'] |
||
145 | ws['G' + str(current_row_number)] = report['meters'][i]['start_value'] |
||
146 | ws['H' + str(current_row_number)] = report['meters'][i]['end_value'] |
||
147 | ws['I' + str(current_row_number)] = report['meters'][i]['difference_value'] |
||
148 | current_row_number += 1 |
||
149 | |||
150 | filename = str(uuid.uuid4()) + '.xlsx' |
||
151 | wb.save(filename) |
||
152 | |||
153 | return filename |
||
154 |