| Conditions | 10 |
| Total Lines | 63 |
| Code Lines | 54 |
| 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 tcllib.tclcheck.TclCheckMixin.do_check() 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 | #!/usr/bin/env python3 |
||
| 17 | def do_check(self, device=None, https=True, timeout=10, max_tries=5): |
||
| 18 | """Perform update request with given parameters.""" |
||
| 19 | protocol = "https://" if https else "http://" |
||
| 20 | url = protocol + self.g2master + "/check.php" |
||
| 21 | params = OrderedDict() |
||
| 22 | if device: |
||
| 23 | # Need to support both ways for now |
||
| 24 | params["id"] = device.imei |
||
| 25 | params["curef"] = device.curef |
||
| 26 | params["fv"] = device.fwver |
||
| 27 | params["mode"] = device.mode |
||
| 28 | params["type"] = device.type |
||
| 29 | params["cltp"] = device.cltp |
||
| 30 | params["cktp"] = device.cktp |
||
| 31 | params["rtd"] = device.rtd |
||
| 32 | params["chnl"] = device.chnl |
||
| 33 | #params["osvs"] = device.osvs |
||
| 34 | #params["ckot"] = device.ckot |
||
| 35 | else: |
||
| 36 | params["id"] = self.serid |
||
| 37 | params["curef"] = self.curef |
||
| 38 | params["fv"] = self.fv |
||
| 39 | params["mode"] = self.mode.value |
||
| 40 | params["type"] = self.ftype |
||
| 41 | params["cltp"] = self.cltp.value |
||
| 42 | params["cktp"] = self.cktp.value |
||
| 43 | params["rtd"] = self.rtd.value |
||
| 44 | params["chnl"] = self.chnl.value |
||
| 45 | #params["osvs"] = self.osvs |
||
| 46 | #params["ckot"] = self.ckot.value |
||
| 47 | |||
| 48 | last_response = None |
||
| 49 | for _ in range(0, max_tries): |
||
| 50 | try: |
||
| 51 | reqtime_start = time.perf_counter() |
||
| 52 | req = self.sess.get(url, params=params, timeout=timeout) |
||
| 53 | reqtime = time.perf_counter() - reqtime_start |
||
| 54 | reqtime_avg = self.check_time_avg() |
||
| 55 | self.check_time_add(reqtime) |
||
| 56 | last_response = req |
||
| 57 | if req.status_code == 200: |
||
| 58 | self.master_server_vote_on_time(reqtime, reqtime_avg) |
||
| 59 | req.encoding = "utf-8" # Force encoding as server doesn't give one |
||
| 60 | self.write_dump(req.text) |
||
| 61 | return req.text |
||
| 62 | elif req.status_code == 204: |
||
| 63 | self.master_server_vote_on_time(reqtime, reqtime_avg) |
||
| 64 | raise requests.exceptions.HTTPError("No update available.", response=req) |
||
| 65 | elif req.status_code == 404: |
||
| 66 | self.master_server_vote_on_time(reqtime, reqtime_avg) |
||
| 67 | raise requests.exceptions.HTTPError("No data for requested CUREF/FV combination.", response=req) |
||
| 68 | elif req.status_code not in [500, 502, 503]: |
||
| 69 | self.master_server_downvote() |
||
| 70 | req.raise_for_status() |
||
| 71 | raise requests.exceptions.HTTPError("HTTP {}.".format(req.status_code), response=req) |
||
| 72 | except requests.exceptions.Timeout: |
||
| 73 | pass |
||
| 74 | # Something went wrong, try a different server |
||
| 75 | self.master_server_downvote() |
||
| 76 | self.g2master = self.get_master_server() |
||
| 77 | protocol = "https://" if https else "http://" |
||
| 78 | url = protocol + self.g2master + "/check.php" |
||
| 79 | raise requests.exceptions.RetryError("Max tries ({}) reached.".format(max_tries), response=last_response) |
||
| 80 | |||
| 95 |