Conditions | 17 |
Total Lines | 91 |
Code Lines | 55 |
Lines | 91 |
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:
Complex classes like reports.metertracking.Reporting.on_get() 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 falcon |
||
25 | @staticmethod |
||
26 | def on_get(req, resp): |
||
27 | print(req.params) |
||
28 | space_id = req.params.get('spaceid') |
||
29 | |||
30 | ################################################################################################################ |
||
31 | # Step 1: valid parameters |
||
32 | ################################################################################################################ |
||
33 | if space_id is None: |
||
34 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID') |
||
35 | else: |
||
36 | space_id = str.strip(space_id) |
||
37 | if not space_id.isdigit() or int(space_id) <= 0: |
||
38 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID') |
||
39 | else: |
||
40 | space_id = int(space_id) |
||
41 | |||
42 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
43 | cursor = cnx.cursor(dictionary=True) |
||
44 | |||
45 | cursor.execute(" SELECT name " |
||
46 | " FROM tbl_spaces " |
||
47 | " WHERE id = %s ", (space_id,)) |
||
48 | row = cursor.fetchone() |
||
49 | |||
50 | if row is None: |
||
51 | if cursor: |
||
52 | cursor.close() |
||
53 | if cnx: |
||
54 | cnx.disconnect() |
||
55 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
56 | description='API.SPACE_NOT_FOUND') |
||
57 | else: |
||
58 | space_name = row['name'] |
||
59 | |||
60 | ################################################################################################################ |
||
61 | # Step 2: build a space tree |
||
62 | ################################################################################################################ |
||
63 | |||
64 | query = (" SELECT id, name, parent_space_id " |
||
65 | " FROM tbl_spaces " |
||
66 | " ORDER BY id ") |
||
67 | cursor.execute(query) |
||
68 | rows_spaces = cursor.fetchall() |
||
69 | node_dict = dict() |
||
70 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
71 | for row in rows_spaces: |
||
72 | parent_node = node_dict[row['parent_space_id']] if row['parent_space_id'] is not None else None |
||
73 | node_dict[row['id']] = AnyNode(id=row['id'], parent=parent_node, name=row['name']) |
||
74 | |||
75 | ################################################################################################################ |
||
76 | # Step 3: query all meters in the space tree |
||
77 | ################################################################################################################ |
||
78 | meter_list = list() |
||
79 | space_dict = dict() |
||
80 | |||
81 | for node in LevelOrderIter(node_dict[space_id]): |
||
82 | space_dict[node.id] = node.name |
||
83 | |||
84 | cursor.execute(" SELECT m.id, m.name AS meter_name, s.name AS space_name, " |
||
85 | " cc.name AS cost_center_name, ec.name AS energy_category_name, " |
||
86 | " m.description " |
||
87 | " FROM tbl_spaces s, tbl_spaces_meters sm, tbl_meters m, tbl_cost_centers cc, " |
||
88 | " tbl_energy_categories ec " |
||
89 | " WHERE s.id IN ( " + ', '.join(map(str, space_dict.keys())) + ") " |
||
90 | " AND sm.space_id = s.id AND sm.meter_id = m.id " |
||
91 | " AND m.cost_center_id = cc.id AND m.energy_category_id = ec.id ", ) |
||
92 | rows_meters = cursor.fetchall() |
||
93 | if rows_meters is not None and len(rows_meters) > 0: |
||
94 | for row in rows_meters: |
||
95 | meter_list.append({"id": row['id'], |
||
96 | "meter_name": row['meter_name'], |
||
97 | "space_name": row['space_name'], |
||
98 | "cost_center_name": row['cost_center_name'], |
||
99 | "energy_category_name": row['energy_category_name'], |
||
100 | "description": row['description']}) |
||
101 | |||
102 | if cursor: |
||
103 | cursor.close() |
||
104 | if cnx: |
||
105 | cnx.disconnect() |
||
106 | |||
107 | ################################################################################################################ |
||
108 | # Step 4: construct the report |
||
109 | ################################################################################################################ |
||
110 | result = {'meters': meter_list} |
||
111 | # export result to Excel file and then encode the file to base64 string |
||
112 | result['excel_bytes_base64'] = \ |
||
113 | excelexporters.metertracking.export(result, |
||
114 | space_name) |
||
115 | resp.body = json.dumps(result) |
||
116 |