Spaces:
Sleeping
Sleeping
Update codeexecutor.py
Browse files- codeexecutor.py +6 -15
codeexecutor.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import os
|
3 |
import re
|
4 |
import subprocess
|
@@ -25,8 +24,7 @@ class PythonREPL:
|
|
25 |
return True, result.stdout.strip()
|
26 |
else:
|
27 |
error_msg = result.stderr.strip()
|
28 |
-
msgs = error_msg.split("
|
29 |
-
")
|
30 |
new_msgs = []
|
31 |
want_next = False
|
32 |
for m in msgs:
|
@@ -44,22 +42,16 @@ class PythonREPL:
|
|
44 |
elif want_next:
|
45 |
new_msgs.append(m)
|
46 |
want_next = False
|
47 |
-
return False, "
|
48 |
-
".join(new_msgs).strip()
|
49 |
|
50 |
def __call__(self, query):
|
51 |
-
query = "import math
|
52 |
-
|
53 |
-
import sympy as sp
|
54 |
-
" + query
|
55 |
-
query = query.strip().split("
|
56 |
-
")
|
57 |
if "print(" not in query[-1]:
|
58 |
if "#" in query[-1]:
|
59 |
query[-1] = query[-1].split("#")[0]
|
60 |
query[-1] = "print(" + query[-1] + ")"
|
61 |
-
query = "
|
62 |
-
".join(query)
|
63 |
|
64 |
with tempfile.TemporaryDirectory() as temp_dir:
|
65 |
temp_file_path = os.path.join(temp_dir, "tmp.py")
|
@@ -120,5 +112,4 @@ def get_majority_vote(answers):
|
|
120 |
return 0
|
121 |
c = Counter(answers)
|
122 |
value, _ = c.most_common()[0]
|
123 |
-
return value
|
124 |
-
|
|
|
|
|
1 |
import os
|
2 |
import re
|
3 |
import subprocess
|
|
|
24 |
return True, result.stdout.strip()
|
25 |
else:
|
26 |
error_msg = result.stderr.strip()
|
27 |
+
msgs = error_msg.split("\n")
|
|
|
28 |
new_msgs = []
|
29 |
want_next = False
|
30 |
for m in msgs:
|
|
|
42 |
elif want_next:
|
43 |
new_msgs.append(m)
|
44 |
want_next = False
|
45 |
+
return False, "\n".join(new_msgs).strip()
|
|
|
46 |
|
47 |
def __call__(self, query):
|
48 |
+
query = "import math\nimport numpy as np\nimport sympy as sp\n" + query
|
49 |
+
query = query.strip().split("\n")
|
|
|
|
|
|
|
|
|
50 |
if "print(" not in query[-1]:
|
51 |
if "#" in query[-1]:
|
52 |
query[-1] = query[-1].split("#")[0]
|
53 |
query[-1] = "print(" + query[-1] + ")"
|
54 |
+
query = "\n".join(query)
|
|
|
55 |
|
56 |
with tempfile.TemporaryDirectory() as temp_dir:
|
57 |
temp_file_path = os.path.join(temp_dir, "tmp.py")
|
|
|
112 |
return 0
|
113 |
c = Counter(answers)
|
114 |
value, _ = c.most_common()[0]
|
115 |
+
return value
|
|