Search in sources :

Example 1 with PyTuple

use of org.python.core.PyTuple in project spring-integration by spring-projects.

the class PythonScriptExecutorTests method test3.

@Test
public void test3() {
    ScriptSource source = new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/test3.py"));
    Object obj = executor.executeScript(source);
    PyTuple tuple = (PyTuple) obj;
    assertEquals(1, tuple.get(0));
}
Also used : ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) PyTuple(org.python.core.PyTuple) StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) ScriptSource(org.springframework.scripting.ScriptSource) ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 2 with PyTuple

use of org.python.core.PyTuple in project metrics by dropwizard.

the class PickledGraphiteTest method unpickleOutput.

private String unpickleOutput() throws Exception {
    StringBuilder results = new StringBuilder();
    // the charset is important. if the GraphitePickleReporter and this test
    // don't agree, the header is not always correctly unpacked.
    String payload = output.toString("UTF-8");
    PyList result = new PyList();
    int nextIndex = 0;
    while (nextIndex < payload.length()) {
        Bindings bindings = new SimpleBindings();
        bindings.put("payload", payload.substring(nextIndex));
        unpickleScript.eval(bindings);
        result.addAll(result.size(), (PyList) bindings.get("metrics"));
        nextIndex += ((BigInteger) bindings.get("batchLength")).intValue();
    }
    for (Object aResult : result) {
        PyTuple datapoint = (PyTuple) aResult;
        String name = datapoint.get(0).toString();
        PyTuple valueTuple = (PyTuple) datapoint.get(1);
        Object timestamp = valueTuple.get(0);
        Object value = valueTuple.get(1);
        results.append(name).append(" ").append(value).append(" ").append(timestamp).append("\n");
    }
    return results.toString();
}
Also used : SimpleBindings(javax.script.SimpleBindings) PyList(org.python.core.PyList) PyTuple(org.python.core.PyTuple) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Example 3 with PyTuple

use of org.python.core.PyTuple in project spring-integration by spring-projects.

the class PythonScriptExecutorTests method test3WithVariables.

@Test
public void test3WithVariables() {
    ScriptSource source = new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/test3.py"));
    HashMap<String, Object> variables = new HashMap<String, Object>();
    variables.put("foo", "bar");
    Object obj = executor.executeScript(source, variables);
    PyTuple tuple = (PyTuple) obj;
    assertNotNull(tuple);
    assertEquals(1, tuple.get(0));
}
Also used : ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) HashMap(java.util.HashMap) PyTuple(org.python.core.PyTuple) StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) ScriptSource(org.springframework.scripting.ScriptSource) ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 4 with PyTuple

use of org.python.core.PyTuple in project apex-malhar by apache.

the class PythonOperator method getBindings.

@Override
public Map<String, Object> getBindings() {
    Map<String, Object> bindings = new HashMap<String, Object>();
    PyStringMap keyValueMap = (PyStringMap) interp.getLocals();
    PyIterator keyValueSet = (PyIterator) keyValueMap.iteritems();
    for (Object temp : keyValueSet) {
        PyTuple tempEntry = (PyTuple) temp;
        Iterator<PyObject> iter = tempEntry.iterator();
        bindings.put((String) iter.next().__tojava__(String.class), iter.next());
    }
    return bindings;
}
Also used : HashMap(java.util.HashMap) PyStringMap(org.python.core.PyStringMap) PyIterator(org.python.core.PyIterator) PyObject(org.python.core.PyObject) PyTuple(org.python.core.PyTuple) PyObject(org.python.core.PyObject)

Aggregations

PyTuple (org.python.core.PyTuple)4 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 ScriptSource (org.springframework.scripting.ScriptSource)2 ResourceScriptSource (org.springframework.scripting.support.ResourceScriptSource)2 StaticScriptSource (org.springframework.scripting.support.StaticScriptSource)2 Bindings (javax.script.Bindings)1 SimpleBindings (javax.script.SimpleBindings)1 PyIterator (org.python.core.PyIterator)1 PyList (org.python.core.PyList)1 PyObject (org.python.core.PyObject)1 PyStringMap (org.python.core.PyStringMap)1