-
Notifications
You must be signed in to change notification settings - Fork 4
/
multi_kernel_test.py
184 lines (149 loc) · 4.8 KB
/
multi_kernel_test.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import sys
import queue
from jupyter_client.manager import start_new_kernel
def startKernel(kernel_name):
return start_new_kernel(kernel_name=kernel_name)
def executeCommand(client, command):
### Execute the code
client.execute(command)
### Get the execution status
### When the execution state is "idle" it is complete
io_msg = client.get_iopub_msg(timeout=1)
io_msg_content = io_msg['content']
### We're going to catch this here before we start polling
if 'execution_state' in io_msg_content and io_msg_content['execution_state'] == 'idle':
return "no output"
out = ""
### Continue polling for execution to complete
### which is indicated by having an execution state of "idle"
while True:
### Save the last message content. This will hold the solution.
### The next one has the idle execution state indicating the execution
###is complete, but not the stdout output
### Poll the message
try:
# print("in try")
io_msg = client.get_iopub_msg(timeout=1)
io_msg_content = io_msg['content']
# print("io_msg_content: " + str(io_msg_content))
if (
'execution_state' in io_msg_content
and io_msg_content['execution_state'] == 'idle'
):
# print("idle --> break")
break
except queue.Empty:
print("timeout get_iopub_msg")
break
### Check the message for various possibilities
if 'data' in io_msg_content: # Indicates completed operation
# print("has data")
out = io_msg_content['data']['text/plain']
elif 'name' in io_msg_content and io_msg_content['name'] == "stdout": # indicates output
# print("has name")
out += io_msg_content['text']
elif 'traceback' in io_msg_content: # Indicates error
print("ERROR")
out = '\n'.join(io_msg_content['traceback']) # Put error into nice format
else:
pass
# out = ''
# print("out: " + out)
return out
python_commands = \
[
'!pwd',
'!echo "hello"',
'!ls',
'1+1',
'a=5',
'b=0',
'print()',
'b',
'print()',
'print("hello there")',
'print(a*10)',
'c=1/b',
'd = {"a":1,"b":"Two","c":[1,2,3]}',
'd',
'import json',
'j = json.loads(str(d).replace(\"\\\'\",\"\\"\"))',
'j',
'import pandas as pd',
('df = pd.DataFrame({'
'"A": [4, 3, 5, 2, 1, 7, 7, 5, 9],'
'"B": [0, 4, 3, 6, 7, 10, 11, 9, 13],'
'"C": [1, 2, 3, 1, 2, 3, 1, 2, 3],'
'"D": [8, 3, 0, 15, 15, -1, 10, 7, 10]'
'})'
),
'df',
'df.describe()',
'df["A"].mean()',
'df.to_csv("data.txt", index=False)'
]
r_commands = \
[
'a<-5',
'b<-0',
'rnorm(5)',
'b',
'summary(cars)',
'y=c(12,15,28,17,18)',
'x=c(22,39,50,25,18)',
'mean(y)',
'mean(x)',
'print("hello there")',
'print(a*10)',
'print(with(PlantGrowth, tapply(weight, group, mean)))',
'with(PlantGrowth, aov(weight ~ group)) -> aov.out',
'print(summary.aov(aov.out))',
'print(summary.lm(aov.out))'
]
bash_commands = \
[
'ls -al',
'pwd',
'echo $(pwd)',
'echo "hello" | sed \'s/el/99/\'',
'a=10',
'echo $a',
'cat data.txt',
'awk \'{FS=","; OFS="\\t";} NR==1{print "A","D","E"} NR>1 && NR%2==0{print $1,$4,($2*$3)}\' data.txt',
'awk \'{FS=","; OFS="\\t";} NR==1{print "A","D","E"} NR>1 && NR%2==0{print $1,$4,($2*$3)}\' data.txt > new_data.txt',
'cat new_data.txt',
'head -4 new_data.txt'
]
kernel_data = \
{
'bash': {'kernel_name': 'bash', 'commands': bash_commands},
'python': {'kernel_name': 'python', 'commands': python_commands},
'R' : {'kernel_name': 'ir', 'commands': r_commands}
}
def run(language):
print("running with " + language)
kernel = kernel_data[language]['kernel_name']
commands = kernel_data[language]['commands']
manager, client = startKernel(kernel)
for command in commands:
print(">>>" + command)
out = executeCommand(client, command)
print(out)
manager.shutdown_kernel()
#un('bash')
"""
language = 'bash'
kernel = kernel_data[language]['kernel_name']
commands = kernel_data[language]['commands']
manager, client = startKernel(kernel)
for command in commands:
print(">>>" + command)
out = executeCommand(client, command)
print(out)
"""
if __name__ == "__main__":
if len(sys.argv) > 1:
language = sys.argv[1]
else:
language = 'python'
run(language)