loubnabnl HF staff commited on
Commit
38d768b
1 Parent(s): 819e86a

remove unecessary files

Browse files
tools/.ipynb_checkpoints/testing_util-checkpoint.py DELETED
@@ -1,438 +0,0 @@
1
- import json
2
- import sys
3
- import faulthandler
4
-
5
- # used for debugging to time steps
6
- from datetime import datetime
7
-
8
- # to run the solution files we're using a timing based approach
9
- import signal
10
-
11
- import numpy as np
12
- # for capturing the stdout
13
- from io import StringIO
14
- # used for testing the code that reads from input
15
- from unittest.mock import patch, mock_open
16
-
17
- from pyext import RuntimeModule
18
-
19
- from enum import Enum
20
- class CODE_TYPE(Enum):
21
- call_based = 0
22
- standard_input = 1
23
-
24
- # stuff for setting up signal timer
25
- class TimeoutException(Exception):
26
- pass
27
- def timeout_handler(signum, frame):
28
- print("alarm went off")
29
- #return
30
- raise TimeoutException
31
- signal.signal(signal.SIGALRM, timeout_handler)
32
- timeout = 4 # seconds
33
-
34
- # used to capture stdout as a list
35
- # from https://stackoverflow.com/a/16571630/6416660
36
- # alternative use redirect_stdout() from contextlib
37
- class Capturing(list):
38
- def __enter__(self):
39
- self._stdout = sys.stdout
40
- sys.stdout = self._stringio = StringIO()
41
- # Make closing the StringIO a no-op
42
- self._stringio.close = lambda x: 1
43
- return self
44
- def __exit__(self, *args):
45
- self.extend(self._stringio.getvalue().splitlines())
46
- del self._stringio # free up some memory
47
- sys.stdout = self._stdout
48
-
49
-
50
- def run_test(sample, test=None, debug=False):
51
- """
52
- if test(generated_code) is not None it'll try to run the code.
53
- otherwise it'll just return an input and output pair.
54
- """
55
- if debug:
56
- print(f"start = {datetime.now().time()}")
57
-
58
- try:
59
- in_outs = json.loads(sample["input_output"])
60
- except ValueError:
61
- in_outs = None
62
- if in_outs:
63
- if in_outs.get("fn_name") is None:
64
- which_type = CODE_TYPE.standard_input # Standard input
65
- method_name = None
66
- else:
67
- which_type = CODE_TYPE.call_based # Call-based
68
- method_name = in_outs["fn_name"]
69
-
70
- if debug:
71
- print(f"loaded input_output = {datetime.now().time()}")
72
-
73
- if test is None:
74
- return in_outs
75
- elif test is not None:
76
- results = []
77
- sol = "import sys\nimport time\nimport itertools\nfrom itertools import accumulate, product, permutations, combinations\nimport collections\nfrom collections import Counter, OrderedDict, deque, defaultdict, ChainMap\nfrom functools import lru_cache\nimport math\nfrom math import sqrt, sin, cos, tan, ceil, fabs, floor, gcd, exp, log, log2\nimport fractions\nfrom typing import List, Tuple\nimport numpy as np\nimport random\nimport heapq\nfrom heapq import *\n"
78
- if debug:
79
- print(f"loading test code = {datetime.now().time()}")
80
-
81
- if which_type == CODE_TYPE.call_based:
82
- sol += test
83
- if debug:
84
- print(f"sol = {sol}")
85
- signal.alarm(timeout)
86
- try:
87
- tmp_sol = RuntimeModule.from_string("tmp_sol", "", sol)
88
- if "class Solution" not in test:
89
- tmp = tmp_sol
90
- else:
91
- tmp = tmp_sol.Solution()
92
- signal.alarm(0)
93
- except Exception as e:
94
- signal.alarm(0)
95
- if debug:
96
- print(f"type 0 compilation error = {e}")
97
- results.append(-2)
98
- return results
99
- signal.alarm(0)
100
-
101
- elif which_type == CODE_TYPE.standard_input:
102
- # sol
103
- tmp_test = test.split("\n")
104
-
105
- new_test = []
106
- for x in tmp_test:
107
- if (not x.startswith("from ")) and (not x.startswith("import ")):
108
- new_test.append("\t" + x + "\n")
109
- else:
110
- new_test.append(x + "\n")
111
- tmp_test = new_test
112
-
113
- new_test = ""
114
- started = False
115
- for i in tmp_test:
116
- if i.startswith("\t") and not started:
117
- new_test += "stdin = sys.stdin\nstdout = sys.stdout\n"
118
- new_test += "def code():\n"
119
- new_test += i
120
- started = True
121
- elif started and ((i.startswith("from ")) or (i.startswith("import "))):
122
- new_test += "\t" + i
123
- else:
124
- new_test += i
125
- tmp_test = new_test
126
-
127
- sol += tmp_test
128
- if debug:
129
- print(f"sol = {sol}")
130
- method_name = "code"
131
- signal.alarm(timeout)
132
- try:
133
- tmp_sol = RuntimeModule.from_string("tmp_sol", "", sol)
134
- tmp = tmp_sol
135
- signal.alarm(0)
136
- except Exception as e:
137
- signal.alarm(0)
138
- if debug:
139
- print(f"type 1 compilation error = {e}")
140
- results.append(-2)
141
- return results
142
- signal.alarm(0)
143
- if debug:
144
- print(f"get method = {datetime.now().time()}")
145
-
146
- try:
147
- method = getattr(tmp, method_name) # get_attr second arg must be str
148
- except:
149
- signal.alarm(0)
150
- e = sys.exc_info()
151
- print(f"unable to get function error = {e}")
152
- return results
153
-
154
- for index, inputs in enumerate(in_outs["inputs"]):
155
- # JSON forces dictionaries to have string keys; this undoes this (assuming a singleton list)
156
- try:
157
- if isinstance(inputs[0], dict):
158
- inputs = [{int(k): v for k,v in inputs[0].items()}]
159
- except:
160
- True
161
- try:
162
- if isinstance(in_outs["outputs"][index], dict):
163
- in_outs["outputs"][index] = [{int(k): v for k,v in in_outs["outputs"][index].items()}]
164
- except:
165
- True
166
- try:
167
- if isinstance(in_outs["outputs"][index][0], dict):
168
- in_outs["outputs"][index] = [{int(k): v for k,v in in_outs["outputs"][index][0].items()}]
169
- except:
170
- True
171
-
172
- if debug:
173
- print(f"time: {datetime.now().time()} testing index = {index} inputs = {inputs}, {type(inputs)}. type = {which_type}")
174
- if which_type == CODE_TYPE.call_based: # Call-based
175
- signal.alarm(timeout)
176
- faulthandler.enable()
177
- try:
178
- output = method(*inputs)
179
-
180
- # ground truth sequences are not tuples
181
- if isinstance(output, tuple):
182
- output = list(output)
183
-
184
- tmp_result = output == in_outs["outputs"][index]
185
- if isinstance(in_outs["outputs"][index], list) and in_outs["outputs"][index]:
186
- tmp_result = tmp_result or (output == in_outs["outputs"][index][0])
187
-
188
- # ground truth sequences are not tuples
189
- try:
190
- if isinstance(output[0], tuple):
191
- tmp_result = tmp_result or ([list(x) for x in output] == in_outs["outputs"][index][0])
192
- except:
193
- True
194
- results.append(tmp_result)
195
-
196
- # reset the alarm
197
- signal.alarm(0)
198
- except Exception as e:
199
- signal.alarm(0)
200
- faulthandler.disable()
201
- print(f"Standard input runtime error or time limit exceeded error = {e}")
202
- results.append(-1)
203
- continue
204
- faulthandler.disable()
205
- signal.alarm(0)
206
- if debug:
207
- print(f"outputs = {output}, test outputs = {in_outs['outputs'][index]}, inputs = {inputs}, {type(inputs)}, {output == [in_outs['outputs'][index]]}")
208
- elif which_type == CODE_TYPE.standard_input: # Standard input
209
- faulthandler.enable()
210
- signal.alarm(timeout)
211
- passed = False
212
-
213
- if isinstance(inputs, list):
214
- inputs = "\n".join(inputs)
215
- if isinstance(in_outs['outputs'][index], list):
216
- in_outs['outputs'][index] = "\n".join(in_outs['outputs'][index])
217
-
218
- with Capturing() as output:
219
- try:
220
- call_method(method, inputs)
221
- # reset the alarm
222
- signal.alarm(0)
223
- passed = True
224
- except Exception as e:
225
- # runtime error or took too long
226
- signal.alarm(0)
227
- print(f"Call-based runtime error or time limit exceeded error = {repr(e)}{e}")
228
- results.append(-1)
229
- signal.alarm(0)
230
-
231
- if not passed:
232
- if debug:
233
- nl = "\n"
234
- if not isinstance(inputs, list):
235
- print(f"not passed output = {output}, test outputs = {in_outs['outputs'][index]}, inputs = {inputs.replace(nl,' new-line ')}, {type(inputs)}, {output == [in_outs['outputs'][index]]}")
236
- else:
237
- print(f"not passed output = {output}, test outputs = {in_outs['outputs'][index]}, inputs = {inputs}, {type(inputs)}, {output == [in_outs['outputs'][index]]}")
238
- continue
239
-
240
- if passed and debug:
241
- print(f"==> output = {output}, test outputs = {in_outs['outputs'][index]}")
242
-
243
- if custom_compare_(output, in_outs['outputs'][index]):
244
- tmp_result = True
245
- results.append(tmp_result)
246
- continue
247
-
248
- # ground truth sequences are expressed as lists not tuples
249
- if isinstance(output, tuple):
250
- output = list(output)
251
-
252
- tmp_result = False
253
- try:
254
- tmp_result = (output == [in_outs["outputs"][index]])
255
- if isinstance(in_outs["outputs"][index], list):
256
- tmp_result = tmp_result or (output == in_outs["outputs"][index])
257
- if isinstance(output[0], str):
258
- tmp_result = tmp_result or ([e.strip() for e in output] == in_outs["outputs"][index])
259
- except Exception as e:
260
- if debug:
261
- print(f"Failed check1 exception = {e}")
262
- pass
263
-
264
- if tmp_result == True:
265
- results.append(tmp_result)
266
- continue
267
-
268
- # try one more time without \n
269
- if isinstance(in_outs["outputs"][index], list):
270
- for tmp_index, i in enumerate(in_outs["outputs"][index]):
271
- in_outs["outputs"][index][tmp_index] = i.split("\n")
272
- in_outs["outputs"][index][tmp_index] = [x.strip() for x in in_outs["outputs"][index][tmp_index] if x]
273
- else:
274
- in_outs["outputs"][index] = in_outs["outputs"][index].split("\n")
275
- in_outs["outputs"][index] = list(filter(len, in_outs["outputs"][index]))
276
- in_outs["outputs"][index] = list(map(lambda x:x.strip(), in_outs["outputs"][index]))
277
-
278
- try:
279
- tmp_result = (output == [in_outs["outputs"][index]])
280
- if isinstance(in_outs["outputs"][index], list):
281
- tmp_result = tmp_result or (output == in_outs["outputs"][index])
282
- except Exception as e:
283
- if debug:
284
- print(f"Failed check2 exception = {e}")
285
- pass
286
-
287
- if tmp_result == True:
288
- results.append(tmp_result)
289
- continue
290
-
291
- # try by converting the output into a split up list too
292
- if isinstance(output, list):
293
- output = list(filter(len, output))
294
-
295
- if debug:
296
- nl = "\n"
297
- if not isinstance(inputs, list):
298
- print(f"output = {output}, test outputs = {in_outs['outputs'][index]}, inputs = {inputs.replace(nl,' new-line ')}, {type(inputs)}, {output == [in_outs['outputs'][index]]}")
299
- else:
300
- print(f"output = {output}, test outputs = {in_outs['outputs'][index]}, inputs = {inputs}, {type(inputs)}, {output == [in_outs['outputs'][index]]}")
301
-
302
- if tmp_result == True:
303
- results.append(tmp_result)
304
- continue
305
-
306
- try:
307
- tmp_result = (output == [in_outs["outputs"][index]])
308
- if isinstance(in_outs["outputs"][index], list):
309
- tmp_result = tmp_result or (output == in_outs["outputs"][index])
310
- except Exception as e:
311
- if debug:
312
- print(f"Failed check3 exception = {e}")
313
- pass
314
-
315
- try:
316
- output_float = [float(e) for e in output]
317
- gt_float = [float(e) for e in in_outs['outputs'][index]]
318
- tmp_result = tmp_result or ((len(output_float) == len(gt_float)) and np.allclose(output_float, gt_float))
319
- except Exception as e:
320
- pass
321
- try:
322
- if isinstance(output[0], list):
323
- output_float = [float(e) for e in output[0]]
324
- gt_float = [float(e) for e in in_outs['outputs'][index][0]]
325
- tmp_result = tmp_result or ((len(output_float) == len(gt_float)) and np.allclose(output_float, gt_float))
326
- except Exception as e:
327
- pass
328
-
329
- if tmp_result == True:
330
- results.append(tmp_result)
331
- continue
332
-
333
- # try by converting the stuff into split up list
334
- if isinstance(in_outs["outputs"][index], list):
335
- for tmp_index, i in enumerate(in_outs["outputs"][index]):
336
- in_outs["outputs"][index][tmp_index] = set(i.split())
337
- else:
338
- in_outs["outputs"][index] = set(in_outs["outputs"][index].split())
339
-
340
- try:
341
- tmp_result = (output == in_outs["outputs"][index])
342
- except Exception as e:
343
- if debug:
344
- print(f"Failed check4 exception = {e}")
345
- continue
346
-
347
- if tmp_result == True:
348
- results.append(tmp_result)
349
- continue
350
-
351
- # try by converting the output into a split up list too
352
- if isinstance(output, list):
353
- for tmp_index, i in enumerate(output):
354
- output[tmp_index] = i.split()
355
- output = list(filter(len, output))
356
- for tmp_index, i in enumerate(output):
357
- output[tmp_index] = set(i)
358
- else:
359
- output = output.split()
360
- output = list(filter(len, output))
361
- output = set(output)
362
-
363
- try:
364
- tmp_result = (set(frozenset(s) for s in output) == set(frozenset(s) for s in in_outs["outputs"][index]))
365
- except Exception as e:
366
- if debug:
367
- print(f"Failed check5 exception = {e}")
368
-
369
-
370
- # if they are all numbers, round so that similar numbers are treated as identical
371
- try:
372
- tmp_result = tmp_result or (set(frozenset(round(float(t),3) for t in s) for s in output) ==\
373
- set(frozenset(round(float(t),3) for t in s) for s in in_outs["outputs"][index]))
374
- except Exception as e:
375
- if debug:
376
- print(f"Failed check6 exception = {e}")
377
-
378
- if tmp_result == True and debug:
379
- print("PASSED")
380
-
381
- results.append(tmp_result)
382
-
383
- if debug:
384
- nl = "\n"
385
- if not isinstance(inputs, list):
386
- print(f"output = {output}, test outputs = {in_outs['outputs'][index]}, inputs = {inputs.replace(nl,' new-line ')}, {type(inputs)}, {output == [in_outs['outputs'][index]]}")
387
- else:
388
- print(f"output = {output}, test outputs = {in_outs['outputs'][index]}, inputs = {inputs}, {type(inputs)}, {output == [in_outs['outputs'][index]]}")
389
-
390
-
391
- return results
392
-
393
-
394
- def custom_compare_(output, ground_truth):
395
-
396
- if isinstance(output, list):
397
- output_1 = "\n".join(output)
398
- if stripped_string_compare(output_1, ground_truth):
399
- return True
400
-
401
- if isinstance(output, list):
402
- output_2 = [o.lstrip().rstrip() for o in output]
403
- output_2 = "\n".join(output_2)
404
- if stripped_string_compare(output_2, ground_truth):
405
- return True
406
-
407
- return False
408
-
409
- def stripped_string_compare(s1, s2):
410
- s1 = s1.lstrip().rstrip()
411
- s2 = s2.lstrip().rstrip()
412
- return s1 == s2
413
-
414
- def call_method(method, inputs):
415
-
416
- if isinstance(inputs, list):
417
- inputs = "\n".join(inputs)
418
-
419
- inputs_line_iterator = iter(inputs.split("\n"))
420
-
421
- # sys.setrecursionlimit(10000)
422
-
423
- # @patch('builtins.input', side_effect=inputs.split("\n"))
424
- @patch('builtins.open', mock_open(read_data=inputs))
425
- @patch('sys.stdin', StringIO(inputs))
426
- @patch('sys.stdin.readline', lambda *args: next(inputs_line_iterator))
427
- @patch('sys.stdin.readlines', lambda *args: inputs.split("\n"))
428
- @patch('sys.stdin.read', lambda *args: inputs)
429
- # @patch('sys.stdout.write', print)
430
- def _inner_call_method(_method):
431
- try:
432
- return _method()
433
- except SystemExit as e:
434
- pass
435
- finally:
436
- pass
437
- return _inner_call_method(method)
438
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tools/__pycache__/testing_util.cpython-39.pyc DELETED
Binary file (11.6 kB)
 
tools/__pycache__/utils.cpython-39.pyc DELETED
Binary file (7.36 kB)