Conditions | 15 |
Total Lines | 74 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Tests | 33 |
CRAP Score | 20.078 |
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_impl() 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 | 1 | import pandas |
|
71 | 1 | @gen.coroutine |
|
72 | def _post_impl(self): |
||
73 | 1 | body = json.loads(self.request.body.decode("utf-8")) |
|
74 | 1 | self.logger.log(logging.DEBUG, f"Processing POST request...") |
|
75 | 1 | if "script" not in body: |
|
76 | 1 | self.error_out(400, "Script is empty.") |
|
77 | 1 | return |
|
78 | |||
79 | # Transforming user script into a proper function. |
||
80 | 1 | user_code = body["script"] |
|
81 | 1 | arguments = None |
|
82 | 1 | arguments_str = "" |
|
83 | 1 | if self.arrow_server is not None and "dataPath" in body: |
|
84 | # arrow flight scenario |
||
85 | arrow_data = self.get_arrow_data(body["dataPath"]) |
||
86 | if arrow_data is not None: |
||
87 | arguments = {"_arg1": arrow_data} |
||
88 | 1 | elif "data" in body: |
|
89 | # legacy scenario |
||
90 | 1 | arguments = body["data"] |
|
91 | |||
92 | 1 | if arguments is not None: |
|
93 | 1 | if not isinstance(arguments, dict): |
|
94 | self.error_out( |
||
95 | 400, "Script parameters need to be provided as a dictionary." |
||
96 | ) |
||
97 | return |
||
98 | 1 | args_in = sorted(arguments.keys()) |
|
99 | 1 | n = len(arguments) |
|
100 | 1 | if sorted('_arg'+str(i+1) for i in range(n)) == args_in: |
|
101 | 1 | arguments_str = ", " + ", ".join(args_in) |
|
102 | else: |
||
103 | 1 | self.error_out( |
|
104 | 400, |
||
105 | "Variables names should follow " |
||
106 | "the format _arg1, _arg2, _argN", |
||
107 | ) |
||
108 | 1 | return |
|
109 | 1 | function_to_evaluate = f"def _user_script(tabpy{arguments_str}):\n" |
|
110 | 1 | for u in user_code.splitlines(): |
|
111 | 1 | function_to_evaluate += " " + u + "\n" |
|
112 | |||
113 | 1 | self.logger.log( |
|
114 | logging.INFO, f"function to evaluate={function_to_evaluate}" |
||
115 | ) |
||
116 | |||
117 | 1 | try: |
|
118 | 1 | result = yield self._call_subprocess(function_to_evaluate, arguments) |
|
119 | 1 | except ( |
|
120 | gen.TimeoutError, |
||
121 | requests.exceptions.ConnectTimeout, |
||
122 | requests.exceptions.ReadTimeout, |
||
123 | ): |
||
124 | self.logger.log(logging.ERROR, self._error_message_timeout) |
||
125 | self.error_out(408, self._error_message_timeout) |
||
126 | return |
||
127 | |||
128 | 1 | if result is not None: |
|
129 | 1 | if self.arrow_server is not None and "dataPath" in body: |
|
130 | # arrow flight scenario |
||
131 | output_data_id = str(uuid.uuid4()) |
||
132 | self.upload_arrow_data(result, output_data_id, { |
||
133 | 'removeOnDelete': 'True', |
||
134 | 'linkedIDs': body["dataPath"] |
||
135 | }) |
||
136 | result = { 'outputDataPath': output_data_id } |
||
137 | self.logger.log(logging.WARN, f'outputDataPath={output_data_id}') |
||
138 | else: |
||
139 | 1 | if isinstance(result, pandas.DataFrame): |
|
140 | result = result.to_dict(orient='list') |
||
141 | 1 | self.write(simplejson.dumps(result, ignore_nan=True)) |
|
142 | else: |
||
143 | 1 | self.write("null") |
|
144 | 1 | self.finish() |
|
145 | |||
214 |