Skip to content

Commit

Permalink
3/24/2018 7:22PM
Browse files Browse the repository at this point in the history
1. Found out from SO the name of R kernel "ir"
2. Installed the bash kernel
3. Made multi_client_test.py to test the different kernels
4. Made a nice test suite of commands to execute in each language
  • Loading branch information
abalter committed Mar 25, 2018
1 parent 4842bbf commit 1897f7d
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 5 deletions.
Binary file added __pycache__/multi_kernel_test.cpython-36.pyc
Binary file not shown.
Empty file added awk
Empty file.
28 changes: 24 additions & 4 deletions client_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import queue
from jupyter_client.manager import start_new_kernel

kernel_manager, client = start_new_kernel()

kernel_manager, client = start_new_kernel(kernel_name="bash")

def runCode(code):

Expand Down Expand Up @@ -53,7 +52,7 @@ def runCode(code):



commands = \
python_commands = \
[
'!pwd',
'!echo "hello"',
Expand All @@ -69,8 +68,29 @@ def runCode(code):
'c=1/b'
]

R_commands = \
[
'a<-5',
'b<-0',
'rnorm(5)',
'b',
'describe(cars)',
'print("hello there")',
'print(a*10)',
'c=1/b'
]

bash_commands = \
[
'ls',
'pwd',
'echo "hello" | sed \'s/el/99/\'',
'a=10',
'echo $a'
]


for command in commands:
for command in bash_commands:
print(">>>" + command)
out = runCode(command)
print(out)
Expand Down
10 changes: 10 additions & 0 deletions data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
A,B,C,D
4,0,1,8
3,4,2,3
5,3,3,0
2,6,1,15
1,7,2,15
7,10,3,-1
7,11,1,10
5,9,2,7
9,13,3,10
Empty file added data.txtawk
Empty file.
2 changes: 1 addition & 1 deletion ideas/ideas-scratch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ print(out)
# >>> a + b = 30

### Run a command in a bash kernel
cmd = """awk 'NR==1{print "A","D","E"}NR>3&&NR%2==0{print $1,$2*$3}' data.txt > new_data.txt"""
cmd = """awk 'NR==1{print "A","D","E"}NR>3&&NR%2==0{print $1,$4,$2*$3}' data.txt > new_data.txt"""
out = bash1.run(cmd)

### Create a DataFrame in a python kernel
Expand Down
184 changes: 184 additions & 0 deletions multi_kernel_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 -F \',\' \'NR==1{print "A","D","E"}NR>1&&NR%2==0{print $1,$4,($2*$3)}\' data.txt',
'awk -F \',\' \'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 = 'bash'

run(language)

6 changes: 6 additions & 0 deletions new_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
A D E
4 8 0
5 0 9
1 15 14
7 10 11
9 10 39

0 comments on commit 1897f7d

Please sign in to comment.