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