| Conditions | 11 |
| Total Lines | 70 |
| 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 tabpy.tabpy_server.handlers.evaluation_plane_handler.EvaluationPlaneHandler.post() 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 | from tabpy.tabpy_server.handlers import BaseHandler |
||
| 43 | @gen.coroutine |
||
| 44 | def post(self): |
||
| 45 | if self.should_fail_with_not_authorized(): |
||
| 46 | self.fail_with_not_authorized() |
||
| 47 | return |
||
| 48 | |||
| 49 | self._add_CORS_header() |
||
| 50 | try: |
||
| 51 | body = json.loads(self.request.body.decode("utf-8")) |
||
| 52 | if "script" not in body: |
||
| 53 | self.error_out(400, "Script is empty.") |
||
| 54 | return |
||
| 55 | |||
| 56 | # Transforming user script into a proper function. |
||
| 57 | user_code = body["script"] |
||
| 58 | arguments = None |
||
| 59 | arguments_str = "" |
||
| 60 | if "data" in body: |
||
| 61 | arguments = body["data"] |
||
| 62 | |||
| 63 | if arguments is not None: |
||
| 64 | if not isinstance(arguments, dict): |
||
| 65 | self.error_out( |
||
| 66 | 400, "Script parameters need to be provided as a dictionary." |
||
| 67 | ) |
||
| 68 | return |
||
| 69 | args_in = sorted(arguments.keys()) |
||
| 70 | n = len(arguments) |
||
| 71 | if sorted('_arg'+str(i+1) for i in range(n)) == args_in: |
||
| 72 | arguments_str = ", " + ", ".join(args_in) |
||
| 73 | else: |
||
| 74 | self.error_out( |
||
| 75 | 400, |
||
| 76 | "Variables names should follow " |
||
| 77 | "the format _arg1, _arg2, _argN", |
||
| 78 | ) |
||
| 79 | return |
||
| 80 | |||
| 81 | function_to_evaluate = f"def _user_script(tabpy{arguments_str}):\n" |
||
| 82 | for u in user_code.splitlines(): |
||
| 83 | function_to_evaluate += " " + u + "\n" |
||
| 84 | |||
| 85 | self.logger.log( |
||
| 86 | logging.INFO, f"function to evaluate={function_to_evaluate}" |
||
| 87 | ) |
||
| 88 | |||
| 89 | try: |
||
| 90 | result = yield self._call_subprocess(function_to_evaluate, arguments) |
||
| 91 | except ( |
||
| 92 | gen.TimeoutError, |
||
| 93 | requests.exceptions.ConnectTimeout, |
||
| 94 | requests.exceptions.ReadTimeout, |
||
| 95 | ): |
||
| 96 | self.logger.log(logging.ERROR, self._error_message_timeout) |
||
| 97 | self.error_out(408, self._error_message_timeout) |
||
| 98 | return |
||
| 99 | |||
| 100 | self.write(simplejson.dumps(result, ignore_nan=True)) |
||
| 101 | self.finish() |
||
| 102 | |||
| 103 | except Exception as e: |
||
| 104 | err_msg = f"{e.__class__.__name__} : {str(e)}" |
||
| 105 | if err_msg != "KeyError : 'response'": |
||
| 106 | err_msg = format_exception(e, "POST /evaluate") |
||
| 107 | self.error_out(500, "Error processing script", info=err_msg) |
||
| 108 | else: |
||
| 109 | self.error_out( |
||
| 110 | 404, |
||
| 111 | "Error processing script", |
||
| 112 | info="The endpoint you're " |
||
| 113 | "trying to query did not respond. Please make sure the " |
||
| 133 |