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