-
Notifications
You must be signed in to change notification settings - Fork 11
/
code_runner_docker.py
147 lines (126 loc) · 4.07 KB
/
code_runner_docker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from llm_sandbox import SandboxSession
def run_python_code():
with SandboxSession(lang="python", keep_template=True, verbose=True) as session:
output = session.run("print('Hello, World!')")
print(output.text)
output = session.run(
"import numpy as np\nprint(np.random.rand())", libraries=["numpy"]
)
print(output.text)
session.execute_command("pip install pandas")
output = session.run("import pandas as pd\nprint(pd.__version__)")
print(output.text)
session.copy_to_runtime("README.md", "/sandbox/data.csv")
def run_java_code():
with SandboxSession(lang="java", keep_template=True, verbose=True) as session:
output = session.run(
"""
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
""",
)
print(output.text)
def run_javascript_code():
with SandboxSession(lang="javascript", keep_template=True, verbose=True) as session:
output = session.run("console.log('Hello, World!')")
print(output.text)
output = session.run(
"""
const axios = require('axios');
axios.get('https://jsonplaceholder.typicode.com/posts/1')
.then(response => console.log(response.data));
""",
libraries=["axios"],
)
print(output.text)
def run_cpp_code():
with SandboxSession(lang="cpp", keep_template=True, verbose=True) as session:
output = session.run(
"""
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
""",
)
print(output.text)
output = session.run(
"""
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
for (int i : v) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
""",
)
print(output.text)
# run with libraries
output = session.run(
"""
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::reverse(v.begin(), v.end());
for (int i : v) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
""",
libraries=["libstdc++"],
)
print(output.text)
def run_go_code():
with SandboxSession(lang="go", keep_template=True, verbose=True) as session:
output = session.run(
"""
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
""",
)
print(output.text)
# run with libraries
output = session.run(
"""
package main
import (
"fmt"
"github.com/spyzhov/ajson"
)
func main() {
fmt.Println("Hello, World!")
json := []byte(`{"price": 100}`)
root, _ := ajson.Unmarshal(json)
nodes, _ := root.JSONPath("$..price")
for _, node := range nodes {
node.SetNumeric(node.MustNumeric() * 1.25)
node.Parent().AppendObject("currency", ajson.StringNode("", "EUR"))
}
result, _ := ajson.Marshal(root)
fmt.Printf("%s", result)
}
""",
libraries=["github.com/spyzhov/ajson"],
)
print(output.text)
if __name__ == "__main__":
run_python_code()
run_java_code()
run_javascript_code()
run_cpp_code()
run_go_code()