def testKernel(self): with sos_kernel() as kc: execute(kc=kc, code='a = 1') stdout, stderr = assemble_output(kc.iopub_channel) self.assertEqual(stdout.strip(), '', f'Stdout is not empty, "{stdout}" received') self.assertEqual(stderr.strip(), '', f'Stderr is not empty, "{stderr}" received') execute(kc=kc, code='%use Python3\n%get a\nprint(a)') stdout, stderr = assemble_output(kc.iopub_channel) self.assertEqual(stderr.strip(), '', f'Stderr is not empty, "{stderr}" received') self.assertEqual(stdout.strip(), '1', f'Stdout should be 1, "{stdout}" received')
def testShell(self): with sos_kernel() as kc: iopub = kc.iopub_channel msg_id, content = execute(kc=kc, code="!ls test_kernel.py") stdout, stderr = assemble_output(iopub) self.assertEqual(stdout, 'test_kernel.py\n') self.assertEqual(stderr, '')
def testMagicPut(self): with sos_kernel() as kc: iopub = kc.iopub_channel msg_id, content = execute(kc=kc, code="%use R") stdout, stderr = assemble_output(iopub) self.assertEqual(stderr, '') msg_id, content = execute(kc=kc, code="a <- 1024") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="%put a") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="%put __k_k") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="%use sos") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="a") res = get_result(iopub) self.assertEqual(res, 1024) # strange name msg_id, content = execute(kc=kc, code="%use R") wait_for_idle(kc) msg_id, content = execute(kc=kc, code=".a.b <- 22") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="%put .a.b") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="%use sos") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="_a_b") res = get_result(iopub) self.assertEqual(res, 22)
def testInterpolation(self): with sos_kernel() as kc: iopub = kc.iopub_channel msg_id, content = execute(kc=kc, code='print("a=${100+11}")') stdout, stderr = assemble_output(iopub) self.assertTrue(stdout.endswith('a=111\n')) self.assertEqual(stderr, '')
def testGetPythonDataFrameFromMatlab(self): # Python -> Matlab/Matlab with sos_kernel() as kc: iopub = kc.iopub_channel # create a data frame execute(kc=kc, code=''' import pandas as pd import numpy as np import scipy.io as sio arr = np.random.randn(1000) arr[::10] = np.nan df = pd.DataFrame({'column_{0}'.format(i): arr for i in range(10)}) ''') clear_channels(iopub) execute(kc=kc, code="%use MATLAB") wait_for_idle(kc) #_, stderr = assemble_output(iopub) #self.assertEqual(stderr, '') execute(kc=kc, code="%get df") wait_for_idle(kc) execute(kc=kc, code="display(size(df))") stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip().split(), ['1000', '10']) execute(kc=kc, code="%use sos") wait_for_idle(kc)
def testPythonToJavaDataframe(self): with sos_kernel() as kc: iopub = kc.iopub_channel execute(kc=kc, code=''' import numpy as np import pandas as pd dataframe = pd.DataFrame(np.random.randn(1000,4), columns=list('ABCD')) ''') wait_for_idle(kc) execute(kc=kc, code='%use Java') wait_for_idle(kc) execute(kc=kc, code='%get dataframe') wait_for_idle(kc) execute( kc=kc, code= 'System.out.println(dataframe.rowCount() * dataframe.columnCount());' ) stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '4000') execute(kc=kc, code="%use sos") wait_for_idle(kc)
def testKernel(self): with sos_kernel() as kc: execute(kc=kc, code='a = 1') stdout, stderr = assemble_output(kc.get_iopub_msg) self.assertEqual(stdout.strip(), '', f'Stdout is not empty, "{stdout}" received') self.assertEqual(stderr.strip(), '', f'Stderr is not empty, "{stderr}" received')
def get_std_output(iopub): '''Obtain stderr and remove some unnecessary warning from https://github.com/jupyter/jupyter_client/pull/201#issuecomment-314269710''' stdout, stderr = assemble_output(iopub) return stdout, '\n'.join([ x for x in stderr.splitlines() if 'sticky' not in x and 'RuntimeWarning' not in x and 'communicator' not in x ])
def test_pylab(self): """Does %pylab work in the in-process kernel?""" matplotlib = pytest.importorskip( 'matplotlib', reason='This test requires matplotlib') kc = self.kc kc.execute('%pylab') out, err = assemble_output(kc.get_iopub_msg) self.assertIn('matplotlib', out)
def testPythonToJavaScalars(self): with sos_kernel() as kc: iopub = kc.iopub_channel execute(kc=kc, code=''' import numpy as np int1 = 10 int2 = 1000000000000000000 int4 = np.intc(20) float1 = 0.1 float2 = 1e+50 string1 = 'abc' bool1 = True ''') wait_for_idle(kc) execute(kc=kc, code='%use Java') wait_for_idle(kc) execute(kc=kc, code='%get int1 int2 int4 float1 float2 string1 bool1') wait_for_idle(kc) #Test int1 execute(kc=kc, code='System.out.println(int1);') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '10') #Test int2 execute(kc=kc, code='System.out.println(int2);') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '1000000000000000000') #Test int4 execute(kc=kc, code='System.out.println(int4);') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '20') #Test float1 execute(kc=kc, code='System.out.println(float1);') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip()[:3], '0.1') #Test float2 execute(kc=kc, code='System.out.println(float2);') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '1.0E50') #Test string1 execute(kc=kc, code='System.out.println(string1);') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'abc') #Test bool1 execute(kc=kc, code='System.out.println(bool1);') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'true') execute(kc=kc, code="%use sos") wait_for_idle(kc)
def testCD(self): with sos_kernel() as kc: iopub = kc.iopub_channel msg_id, content = execute(kc=kc, code="%cd ..") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="print(os.getcwd())") stdout, stderr = assemble_output(iopub) self.assertTrue(stdout.strip().endswith('jupyter')) self.assertEqual(stderr, '') msg_id, content = execute(kc=kc, code="%cd test")
def testPutRDataFrameToPython(self): # R -> Python with sos_kernel() as kc: iopub = kc.iopub_channel # create a data frame execute(kc=kc, code='%use R') wait_for_idle(kc) execute(kc=kc, code="%put mtcars") assemble_output(iopub) # the message can contain "Loading required package feathre" #self.assertEqual(stderr, '') execute(kc=kc, code="%use sos") wait_for_idle(kc) execute(kc=kc, code="mtcars.shape") res = get_result(iopub) self.assertEqual(res, (32, 11)) execute(kc=kc, code="mtcars.index[0]") res = get_result(iopub) self.assertEqual(res, 'Mazda RX4')
def testSubKernel(self): with sos_kernel() as kc: iopub = kc.iopub_channel msg_id, content = execute(kc=kc, code="%use R") stdout, stderr = assemble_output(iopub) self.assertEqual(stderr, '') msg_id, content = execute(kc=kc, code="a <- 1024") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="a") res = get_display_data(iopub) self.assertEqual(res, '[1] 1024') msg_id, content = execute(kc=kc, code="%use sos") wait_for_idle(kc)
def test_stdout(self): """ Does the in-process kernel correctly capture IO? """ kernel = InProcessKernel() with capture_output() as io: kernel.shell.run_cell('print("foo")') assert io.stdout == 'foo\n' kc = BlockingInProcessKernelClient(kernel=kernel, session=kernel.session) kernel.frontends.append(kc) kc.execute('print("bar")') out, err = assemble_output(kc.iopub_channel) assert out == 'bar\n'
def test_capfd(self): """Does correctly capture fd""" kernel = InProcessKernel() with capture_output() as io: kernel.shell.run_cell('print("foo")') assert io.stdout == "foo\n" kc = BlockingInProcessKernelClient(kernel=kernel, session=kernel.session) kernel.frontends.append(kc) kc.execute("import os") kc.execute('os.system("echo capfd")') out, err = assemble_output(kc.iopub_channel) assert out == "capfd\n"
def testPutRDataFrameToPython(self): with sos_kernel() as kc: iopub = kc.iopub_channel # create a data frame msg_id, content = execute(kc=kc, code='%use R') wait_for_idle(kc) msg_id, content = execute(kc=kc, code="%put mtcars") stdout, stderr = assemble_output(iopub) # the message can contain "Loading required package feathre" #self.assertEqual(stderr, '') msg_id, content = execute(kc=kc, code="%use sos") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="mtcars.shape") res = get_result(iopub) self.assertEqual(res, (32, 11)) msg_id, content = execute(kc=kc, code="%use sos") wait_for_idle(kc)
def testPythonToCppDataframe(self): with sos_kernel() as kc: iopub = kc.iopub_channel execute(kc=kc, code=''' import numpy as np import pandas as pd dataframe = pd.DataFrame(np.random.randn(1000,4), columns=list('ABCD')) ''') wait_for_idle(kc) execute(kc=kc, code='%use C++14') wait_for_idle(kc) execute(kc=kc, code='%get dataframe') wait_for_idle(kc) execute(kc=kc, code='std::cout << dataframe.size();') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '4000') execute(kc=kc, code="%use sos") wait_for_idle(kc)
def testGetPythonDataFrameFromR(self): with sos_kernel() as kc: iopub = kc.iopub_channel # create a data frame msg_id, content = execute(kc=kc, code=''' import pandas as pd import numpy as np arr = np.random.randn(1000) arr[::10] = np.nan df = pd.DataFrame({'column_{0}'.format(i): arr for i in range(10)}) ''') clear_channels(iopub) msg_id, content = execute(kc=kc, code="%use R") stdout, stderr = assemble_output(iopub) self.assertEqual(stderr, '') msg_id, content = execute(kc=kc, code="%get df") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="dim(df)") res = get_display_data(iopub) self.assertEqual(res, '[1] 1000 10') msg_id, content = execute(kc=kc, code="%use sos") wait_for_idle(kc)
def testJavaToPythonDataframe(self): with sos_kernel() as kc: iopub = kc.iopub_channel execute(kc=kc, code='%use Java') wait_for_idle(kc) execute(kc=kc, code=''' String[] animals = {"bear", "cat", "giraffe"}; double[] cuteness = {90.1, 84.3, 99.7}; Table cuteAnimals = Table.create("Cute Animals").addColumns( StringColumn.create("Animal types", animals), DoubleColumn.create("rating", cuteness)); ''') wait_for_idle(kc) execute(kc=kc, code='%put cuteAnimals') wait_for_idle(kc) execute(kc=kc, code='%use sos') wait_for_idle(kc) execute(kc=kc, code='print(cuteAnimals["rating"][2])') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '99.7')
def testMagicGet(self): with sos_kernel() as kc: iopub = kc.iopub_channel msg_id, content = execute(kc=kc, code="a = 1025") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="_b_a = 22") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="%use R") stdout, stderr = assemble_output(iopub) self.assertEqual(stderr, '') msg_id, content = execute(kc=kc, code="%get a") wait_for_idle(kc) msg_id, content = execute(kc=kc, code="a") res = get_display_data(iopub) self.assertEqual(res, '[1] 1025') # msg_id, content = execute(kc=kc, code="%get _b_a") wait_for_idle(kc) msg_id, content = execute(kc=kc, code=".b_a") res = get_display_data(iopub) self.assertEqual(res, '[1] 22') msg_id, content = execute(kc=kc, code="%use sos") wait_for_idle(kc)
def testJavatoPythonScalars(self): with sos_kernel() as kc: iopub = kc.iopub_channel execute(kc=kc, code='%use Java') wait_for_idle(kc) execute(kc=kc, code=''' int i = 1; short si = 32; long li = 2000000000; float f = 0.1f; double d = 1e+300; boolean b = true; char c = '*'; Map<String, String> m1 = new HashMap<>(); m1.put(\"USA\", \"Washington\"); m1.put(\"United Kingdom\", \"London\"); m1.put(\"India\", \"New Delhi\"); Map<Integer, Boolean> m2 = new HashMap<>(); m2.put(1, true); m2.put(2, false); m2.put(3, true); ArrayList<Integer> l = new ArrayList<Integer>(Arrays.asList(1,2,3)); ''') wait_for_idle(kc) execute(kc=kc, code='%put i si li f d b c m1 m2 l') wait_for_idle(kc) execute(kc=kc, code='%use sos') wait_for_idle(kc) execute(kc=kc, code='print(i)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '1') execute(kc=kc, code='print(si)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '32') execute(kc=kc, code='print(li)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '2000000000') execute(kc=kc, code='print(f)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '0.1') execute(kc=kc, code='print(d)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '1e+300') execute(kc=kc, code='print(b)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'True') execute(kc=kc, code='print(c)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '*') execute(kc=kc, code='print(m1["USA"])') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'Washington') execute(kc=kc, code='print(m2[2])') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'False') execute(kc=kc, code='print(l)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '[1 2 3]')
def test_pylab(self): """Does %pylab work in the in-process kernel?""" kc = self.kc kc.execute('%pylab') out, err = assemble_output(kc.iopub_channel) self.assertIn('matplotlib', out)
def testCpptoPythonScalars(self): with sos_kernel() as kc: iopub = kc.iopub_channel execute(kc=kc, code='%use C++14') wait_for_idle(kc) execute(kc=kc, code=''' int i = 1; short int si = 32; long int li = 2000000000; long long int lli = 2000000000000000000; float f = 0.1f; double d = 1e+300; long double ld = 1e+1000L; bool b = true; char c = '*'; std::map<int, int> m = {{1,2},{2,3}}; std::map<std::string, float> m2 = {{"Alice", -1.0f},{"Bob", 1.0f}}; std::map<std::string, bool> m3 = {{"Alice", true},{"Bob", false}}; std::vector<int> v = {1,2,3,4,5}; std::vector<bool> v2 = {true,false,true,false,true}; std::vector<std::string> v3 = {"q","w","e","r","t","y"}; xt::xarray<double> arr {{1.1, 2.2, 3.3}, {4.4, 5.5, 6.6}, {7.7, 8.8, 9.9}}; xt::xarray<std::string> arr2 {{"1.1", "2.2", "a"}, {"4.4", "5.5", "6.6"}, {"7.7", "8.8", "9.9"}}; ''') wait_for_idle(kc) execute( kc=kc, code='%put i si li lli f d ld b c m m2 m3 v v2 v3 arr arr2') wait_for_idle(kc) execute(kc=kc, code='%use sos') wait_for_idle(kc) execute(kc=kc, code='print(i)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '1') execute(kc=kc, code='print(si)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '32') execute(kc=kc, code='print(lli)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '2000000000000000000') execute(kc=kc, code='print(f)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip()[:10], '0.10000000') execute(kc=kc, code='print(d)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '1e+300') execute(kc=kc, code='print(ld)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '1e+1000') execute(kc=kc, code='print(b)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'True') execute(kc=kc, code='print(c)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '*') execute(kc=kc, code='print(m[2])') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '3') execute(kc=kc, code='print(m2["Alice"])') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '-1.0') execute(kc=kc, code='print(m3["Bob"])') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'False') execute(kc=kc, code='print(v)') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '[1 2 3 4 5]') execute(kc=kc, code='print(sum(v2))') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '3') execute(kc=kc, code='print("".join(v3))') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'qwerty') execute(kc=kc, code='print(arr[1,1])') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '5.5') execute(kc=kc, code='print(arr2[0,2])') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'a')
def testKernel(self): with sos_kernel() as kc: execute(kc=kc, code='a = 1\nprint(a)') stdout, stderr = assemble_output(kc.iopub_channel) self.assertEqual(stderr, '') self.assertEqual(stdout.strip(), '1')
def testPythonToCppScalars(self): with sos_kernel() as kc: iopub = kc.iopub_channel execute(kc=kc, code=''' import numpy as np int1 = 10 int2 = 1000000000000000000 int4 = np.intc(20) float1 = 0.1 float2 = 1e+50 float3 = np.longdouble("1e+1000") string1 = 'abc' bool1 = True ''') clear_channels(iopub) execute(kc=kc, code='%use C++14') wait_for_idle(kc) execute( kc=kc, code='%get int1 int2 int4 float1 float2 float3 string1 bool1') wait_for_idle(kc) #Test int1 execute(kc=kc, code='std::cout << int1;') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '10') #Test int2 execute(kc=kc, code='std::cout << int2;') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '1000000000000000000') # #Test int2 # execute(kc=kc, code='std::cout << int2;') # stdout, _ = assemble_output(iopub) # self.assertEqual(stdout.strip(),'1000000000000000000') #Test int4 execute(kc=kc, code='std::cout << int4;') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '20') #Test float1 execute(kc=kc, code='std::cout << float1;') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '0.1') #Test float2 execute(kc=kc, code='std::cout << float2;') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), '1e+50') #Test float3 execute(kc=kc, code='std::cout << float3;') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'inf') #Test string1 execute(kc=kc, code='std::cout << string1;') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'abc') #Test bool1 execute(kc=kc, code='std::cout << (bool1?"true":"false");') stdout, _ = assemble_output(iopub) self.assertEqual(stdout.strip(), 'true') execute(kc=kc, code="%use sos") wait_for_idle(kc)