| Conditions | 10 | 
| Total Lines | 51 | 
| Lines | 0 | 
| Ratio | 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 Client.make_call() 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 pycurl, json | ||
| 76 | def make_call(self): | ||
| 77 | backoff_network_error = 0.25 | ||
| 78 | backoff_http_error = 1 | ||
| 79 | backoff_rate_limit = 60 | ||
| 80 | attempt = 0 | ||
| 81 | while True and self.keep_trying == 1 and attempt < MAX_ATTEMPTS: | ||
| 82 | attempt += 1 | ||
| 83 | self.setup_connection() | ||
| 84 | try: | ||
| 85 | self.conn.perform() | ||
| 86 | except: | ||
| 87 | # Network error, use linear back off up to 16 seconds | ||
| 88 | if self.keep_trying == 0: | ||
| 89 | continue | ||
| 90 |         sys.stderr.write ('Network error: %s' % self.conn.errstr()) | ||
| 91 |         sys.stderr.write ('Waiting %s seconds before trying again' % backoff_network_error) | ||
| 92 | time.sleep(backoff_network_error) | ||
| 93 | backoff_network_error = min(backoff_network_error + 1, 16) | ||
| 94 | continue | ||
| 95 | # HTTP Error | ||
| 96 | sc = self.conn.getinfo(pycurl.HTTP_CODE) | ||
| 97 | if sc == 200: | ||
| 98 |         #sys.stderr.write('HTTP request successful.') | ||
| 99 | self.conn.close() | ||
| 100 | break | ||
| 101 | elif sc == 420: | ||
| 102 | # Rate limit, use exponential back off starting with 1 minute and double each attempt | ||
| 103 |         sys.stderr.write ('Rate limit, waiting %s seconds' % backoff_rate_limit) | ||
| 104 | time.sleep(backoff_rate_limit) | ||
| 105 | backoff_rate_limit *= 2 | ||
| 106 | elif sc == 401: | ||
| 107 | # Authentication error | ||
| 108 |         sys.stderr.write ('Authentication error, check user/password.') | ||
| 109 | self.action.error = 1 | ||
| 110 | break | ||
| 111 | elif sc == 404: | ||
| 112 | # Authorization error | ||
| 113 |         sys.stderr.write ('Object not found, or authorization error. Verify request of check permissions.') | ||
| 114 | self.action.error = 2 | ||
| 115 | break | ||
| 116 | elif sc == 400: | ||
| 117 | # Authorization error | ||
| 118 |         sys.stderr.write ('Bad request.') | ||
| 119 | self.action.error = 3 | ||
| 120 | break | ||
| 121 | else: | ||
| 122 | # HTTP error, use exponential back off up to 320 seconds | ||
| 123 |         sys.stderr.write('HTTP error %s, %s' % (sc, self.conn.errstr())) | ||
| 124 |         sys.stderr.write('Waiting %s seconds' % backoff_http_error) | ||
| 125 | time.sleep(backoff_http_error) | ||
| 126 | backoff_http_error = min(backoff_http_error * 2, 10) | ||
| 127 | |||
| 129 |