Conditions | 19 |
Total Lines | 87 |
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 callfunction() 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 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
167 | @functools.wraps(f) |
||
168 | def callfunction(*args, **kwargs): |
||
169 | function_name = f.__name__ |
||
170 | args = list(args) |
||
171 | types = copy.copy(arg_types) |
||
172 | more = [args.pop(0)] |
||
173 | |||
174 | if types: |
||
175 | argspec = inspect.getargspec(f) |
||
176 | names = argspec.args[1:] |
||
177 | |||
178 | for name in names: |
||
179 | try: |
||
180 | a = args.pop(0) |
||
181 | more.append(types.pop(0)(a)) |
||
182 | except IndexError: |
||
183 | try: |
||
184 | kwargs[name] = types.pop(0)(kwargs[name]) |
||
185 | except IndexError: |
||
186 | LOG.warning("Type definition for '%s' argument of '%s' " |
||
187 | "is missing.", name, f.__name__) |
||
188 | except KeyError: |
||
189 | pass |
||
190 | |||
191 | if body_cls: |
||
192 | if pecan.request.body: |
||
193 | data = pecan.request.json |
||
194 | else: |
||
195 | data = {} |
||
196 | |||
197 | obj = body_cls(**data) |
||
198 | try: |
||
199 | obj = obj.validate() |
||
200 | except (jsonschema.ValidationError, ValueError) as e: |
||
201 | raise exc.HTTPBadRequest(detail=e.message, |
||
202 | comment=traceback.format_exc()) |
||
203 | except Exception as e: |
||
204 | raise exc.HTTPInternalServerError(detail=e.message, |
||
205 | comment=traceback.format_exc()) |
||
206 | |||
207 | # Set default pack if one is not provided for resource create |
||
208 | if function_name == 'post' and not hasattr(obj, 'pack'): |
||
209 | extra = { |
||
210 | 'resource_api': obj, |
||
211 | 'default_pack_name': DEFAULT_PACK_NAME |
||
212 | } |
||
213 | LOG.debug('Pack not provided in the body, setting a default pack name', |
||
214 | extra=extra) |
||
215 | setattr(obj, 'pack', DEFAULT_PACK_NAME) |
||
216 | |||
217 | more.append(obj) |
||
218 | |||
219 | args = tuple(more) + tuple(args) |
||
220 | |||
221 | noop_codes = [http_client.NOT_IMPLEMENTED, |
||
222 | http_client.METHOD_NOT_ALLOWED, |
||
223 | http_client.FORBIDDEN] |
||
224 | |||
225 | if status_code and status_code in noop_codes: |
||
226 | pecan.response.status = status_code |
||
227 | return json_encode(None) |
||
228 | |||
229 | try: |
||
230 | result = f(*args, **kwargs) |
||
231 | except TypeError as e: |
||
232 | message = str(e) |
||
233 | # Invalid number of arguments passed to the function meaning invalid path was |
||
234 | # requested |
||
235 | # Note: The check is hacky, but it works for now. |
||
236 | func_name = f.__name__ |
||
237 | pattern = '%s\(\) takes exactly \d+ arguments \(\d+ given\)' % (func_name) |
||
238 | |||
239 | if re.search(pattern, message): |
||
240 | raise exc.HTTPNotFound() |
||
241 | else: |
||
242 | raise e |
||
243 | |||
244 | if status_code: |
||
245 | pecan.response.status = status_code |
||
246 | if content_type == 'application/json': |
||
247 | if is_debugging_enabled(): |
||
248 | indent = 4 |
||
249 | else: |
||
250 | indent = None |
||
251 | return json_encode(result, indent=indent) |
||
252 | else: |
||
253 | return result |
||
254 | |||
260 |
Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with
r
orR
are they interpreted as regular expressions.The escape sequence that was used indicates that you might have intended to write a regular expression.
Learn more about the available escape sequences. in the Python documentation.