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