Search in sources :

Example 61 with PyObject

use of org.python.core.PyObject in project gda-core by openGDA.

the class ScannableMotionWithScannableFieldsBaseTest method testGetFieldPositionWithPyArray.

@Test
public void testGetFieldPositionWithPyArray() throws Exception {
    PyObject[] position = new PyObject[] { new PyFloat(0.), new PyInteger(1), new PyFloat(2.), new PyInteger(3) };
    doReturn(position).when(scn).rawGetPosition();
    assertEquals(new PyFloat(0.), scn.getFieldPosition(0));
    assertEquals(new PyInteger(1), scn.getFieldPosition(1));
    assertEquals(new PyFloat(2.), scn.getFieldPosition(2));
    assertEquals(new PyInteger(3), scn.getFieldPosition(3));
}
Also used : PyFloat(org.python.core.PyFloat) PyInteger(org.python.core.PyInteger) PyObject(org.python.core.PyObject) Test(org.junit.Test)

Example 62 with PyObject

use of org.python.core.PyObject in project gda-core by openGDA.

the class ScannableSnapshotTest method testJythonScannableInitialization.

@Test
public void testJythonScannableInitialization() throws Exception {
    PyFloat flt = new PyFloat(100.);
    PyString str = new PyString("test");
    PyList listOutput = new PyList(new PyObject[] { flt, str });
    Scannable jscn = generateMockScannable("jscn", new String[] { "in" }, new String[] { "extra" }, new String[] { "%5.5g", "%s" }, false, "", listOutput);
    ScannableSnapshot s = new ScannableSnapshot(jscn);
    assertEquals("jscn", s.name);
    assertArrayEquals(new String[] { "in" }, s.inputNames);
    assertArrayEquals(new String[] { "extra" }, s.extraNames);
    assertArrayEquals(new String[] { "%5.5g", "%s" }, s.outputFormat);
    assertArrayEquals(new String[] { "", "" }, s.units);
    assertEquals(false, s.busy);
    assertArrayEquals(new Object[] { 100., "test" }, (Object[]) s.lastPosition);
    PyTuple tupleOutput = new PyTuple(new PyObject[] { flt, str });
    jscn = generateMockScannable("jscn", new String[] { "in" }, new String[] { "extra" }, new String[] { "%5.5g", "%s" }, false, "", tupleOutput);
    s = new ScannableSnapshot(jscn);
    assertArrayEquals(new Object[] { 100., "test" }, (Object[]) s.lastPosition);
}
Also used : PyString(org.python.core.PyString) PyList(org.python.core.PyList) PyFloat(org.python.core.PyFloat) Scannable(gda.device.Scannable) PyObject(org.python.core.PyObject) PyString(org.python.core.PyString) PyTuple(org.python.core.PyTuple) Test(org.junit.Test)

Example 63 with PyObject

use of org.python.core.PyObject in project gda-core by openGDA.

the class GdaBuiltinTest method testOverloadedDocString.

@Test
public void testOverloadedDocString() throws Exception {
    PyObject foo = builtinFor(Commands.class, "foo");
    String docs = "Foo strings\n\n" + "foo(String)\n" + "    Foo a single String\n" + "foo(String, String) -> String\n" + "    Foo two strings into one\n" + "\n" + "from gda.jython.GdaBuiltinTest.Commands";
    assertThat(foo.__getattr__("__doc__").toString(), is(docs));
}
Also used : PyObject(org.python.core.PyObject) Test(org.junit.Test)

Example 64 with PyObject

use of org.python.core.PyObject in project gda-core by openGDA.

the class GdaBuiltinTest method correctFunctionIsCalled.

@Test
public void correctFunctionIsCalled() throws Exception {
    GdaBuiltin foo = builtinFor(Commands.class, "foo");
    PyObject first = foo.__call__(Py.java2py("helloWorld"));
    // void methods return None
    assertThat(first, is(Py.None));
    PyObject second = foo.__call__(Py.javas2pys("hello", "World"));
    // return as jython object
    assertThat(second, is(Py.java2py("helloWorld")));
}
Also used : PyObject(org.python.core.PyObject) Test(org.junit.Test)

Example 65 with PyObject

use of org.python.core.PyObject in project bowler-script-kernel by CommonWealthRobotics.

the class JythonHelper method inlineScriptRun.

@Override
public Object inlineScriptRun(String code, ArrayList<Object> args) {
    Properties props = new Properties();
    PythonInterpreter.initialize(System.getProperties(), props, new String[] { "" });
    if (interp == null) {
        interp = new PythonInterpreter();
        interp.exec("import sys");
    }
    for (String pm : DeviceManager.listConnectedDevice(null)) {
        BowlerAbstractDevice bad = DeviceManager.getSpecificDevice(null, pm);
        // passing into the scipt
        try {
            interp.set(bad.getScriptingName(), Class.forName(bad.getClass().getName()).cast(bad));
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.err.println("Device " + bad.getScriptingName() + " is " + bad);
    }
    interp.set("args", args);
    interp.exec(code);
    ArrayList<Object> results = new ArrayList<>();
    PyObject localVariables = interp.getLocals();
    try {
        results.add(interp.get("csg", CSG.class));
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        results.add(interp.get("tab", Tab.class));
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        results.add(interp.get("device", BowlerAbstractDevice.class));
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.debug("Jython return = " + results);
    return results;
}
Also used : PythonInterpreter(org.python.util.PythonInterpreter) BowlerAbstractDevice(com.neuronrobotics.sdk.common.BowlerAbstractDevice) Tab(javafx.scene.control.Tab) CSG(eu.mihosoft.vrl.v3d.CSG) ArrayList(java.util.ArrayList) PyObject(org.python.core.PyObject) Properties(java.util.Properties) PyObject(org.python.core.PyObject) IOException(java.io.IOException)

Aggregations

PyObject (org.python.core.PyObject)80 PyString (org.python.core.PyString)24 PyList (org.python.core.PyList)16 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)12 PyException (org.python.core.PyException)12 IOException (java.io.IOException)9 Serializable (java.io.Serializable)9 PyInteger (org.python.core.PyInteger)8 Test (org.junit.Test)7 PyStringMap (org.python.core.PyStringMap)7 PythonInterpreter (org.python.util.PythonInterpreter)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Map (java.util.Map)6 PyDictionary (org.python.core.PyDictionary)6 PyFloat (org.python.core.PyFloat)6 List (java.util.List)5 PyLong (org.python.core.PyLong)5 PyNone (org.python.core.PyNone)5 PyTuple (org.python.core.PyTuple)5