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