| Conditions | 12 |
| Total Lines | 63 |
| Code Lines | 46 |
| Lines | 63 |
| 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 gsmmodem.GSMModemItem.on_put() 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 |
||
| 149 | View Code Duplication | @staticmethod |
|
| 150 | def on_put(req, resp, id_): |
||
| 151 | """Handles PUT requests""" |
||
| 152 | try: |
||
| 153 | raw_json = req.stream.read().decode('utf-8') |
||
| 154 | except Exception as ex: |
||
| 155 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 156 | |||
| 157 | if not id_.isdigit() or int(id_) <= 0: |
||
| 158 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 159 | description='API.INVALID_GSM_MODEM_ID') |
||
| 160 | |||
| 161 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 162 | |||
| 163 | if 'serial_port' not in new_values['data'].keys() or \ |
||
| 164 | not isinstance(new_values['data']['serial_port'], str) or \ |
||
| 165 | len(str.strip(new_values['data']['serial_port'])) == 0: |
||
| 166 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 167 | description='API.INVALID_SERIAL_PORT') |
||
| 168 | |||
| 169 | serial_port = str.strip(new_values['data']['serial_port']) |
||
| 170 | |||
| 171 | if 'baud_rate' not in new_values['data'].keys() or \ |
||
| 172 | not isinstance(new_values['data']['baud_rate'], int) or \ |
||
| 173 | new_values['data']['baud_rate'] <= 0: |
||
| 174 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 175 | description='API.INVALID_BAUD_RATE') |
||
| 176 | baud_rate = float(new_values['data']['baud_rate']) |
||
| 177 | |||
| 178 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 179 | cursor = cnx.cursor() |
||
| 180 | |||
| 181 | cursor.execute(" SELECT serial_port " |
||
| 182 | " FROM tbl_gsm_modems " |
||
| 183 | " WHERE id = %s ", |
||
| 184 | (id_,)) |
||
| 185 | if cursor.fetchone() is None: |
||
| 186 | cursor.close() |
||
| 187 | cnx.disconnect() |
||
| 188 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 189 | description='API.GSM_MODEM_NOT_FOUND') |
||
| 190 | |||
| 191 | cursor.execute(" SELECT serial_port " |
||
| 192 | " FROM tbl_gsm_modems " |
||
| 193 | " WHERE serial_port = %s AND id != %s ", (serial_port, id_)) |
||
| 194 | if cursor.fetchone() is not None: |
||
| 195 | cursor.close() |
||
| 196 | cnx.disconnect() |
||
| 197 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 198 | description='API.GSM_MODEM_SERIAL_PORT_IS_ALREADY_IN_USE') |
||
| 199 | |||
| 200 | update_row = (" UPDATE tbl_gsm_modems " |
||
| 201 | " SET serial_port = %s, baud_rate = %s " |
||
| 202 | " WHERE id = %s ") |
||
| 203 | cursor.execute(update_row, (serial_port, |
||
| 204 | baud_rate, |
||
| 205 | id_,)) |
||
| 206 | cnx.commit() |
||
| 207 | |||
| 208 | cursor.close() |
||
| 209 | cnx.disconnect() |
||
| 210 | |||
| 211 | resp.status = falcon.HTTP_200 |
||
| 212 |