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