Search in sources :

Example 1 with PyList

use of org.python.core.PyList in project components by Talend.

the class PythonRowDoFn method flatMap.

private void flatMap(IndexedRecord input, ProcessContext context) throws IOException {
    // Prepare Python environment
    PyObject outputList = pyFn.__call__(new PyString(input.toString()));
    if (outputList instanceof PyList) {
        PyList list = (PyList) outputList;
        for (Object output : list) {
            if (jsonGenericRecordConverter == null) {
                JsonSchemaInferrer jsonSchemaInferrer = new JsonSchemaInferrer(new ObjectMapper());
                Schema jsonSchema = jsonSchemaInferrer.inferSchema(output.toString());
                jsonGenericRecordConverter = new JsonGenericRecordConverter(jsonSchema);
            }
            GenericRecord outputRecord = jsonGenericRecordConverter.convertToAvro(output.toString());
            context.output(outputRecord);
        }
    }
}
Also used : PyString(org.python.core.PyString) PyList(org.python.core.PyList) Schema(org.apache.avro.Schema) PyObject(org.python.core.PyObject) JsonGenericRecordConverter(org.talend.daikon.avro.converter.JsonGenericRecordConverter) GenericRecord(org.apache.avro.generic.GenericRecord) PyObject(org.python.core.PyObject) JsonSchemaInferrer(org.talend.daikon.avro.inferrer.JsonSchemaInferrer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with PyList

use of org.python.core.PyList 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 PyList

use of org.python.core.PyList in project cucumber-jvm by cucumber.

the class JythonStepDefinition method matchedArguments.

@Override
public List<Argument> matchedArguments(Step step) {
    PyObject stepName = new PyString(step.getName());
    PyObject matched_arguments = stepdef.invoke("matched_arguments", stepName);
    if (matched_arguments instanceof PyList) {
        return (PyList) matched_arguments;
    } else {
        return null;
    }
}
Also used : PyString(org.python.core.PyString) PyList(org.python.core.PyList) PyObject(org.python.core.PyObject)

Example 4 with PyList

use of org.python.core.PyList in project cucumber-jvm by cucumber.

the class JythonBackend method dataTableToPyArray.

private PyObject dataTableToPyArray(DataTable table) {
    PyList pyTable = new PyList();
    for (List<String> row : table.raw()) {
        PyList pyRow = new PyList();
        for (String cell : row) {
            pyRow.append(new PyString(cell));
        }
        pyTable.append(pyRow);
    }
    return pyTable;
}
Also used : PyString(org.python.core.PyString) PyList(org.python.core.PyList) PyString(org.python.core.PyString)

Example 5 with PyList

use of org.python.core.PyList in project org.csstudio.display.builder by kasemir.

the class JythonScriptSupport method addToPythonPath.

/**
 * @param path Path to add to head of python search path
 */
private void addToPythonPath(final String path) {
    // Since using default PySystemState (see above), check if already in paths
    final PyList paths = python.getSystemState().path;
    // Prevent concurrent modification
    synchronized (JythonScriptSupport.class) {
        final int index = paths.indexOf(path);
        // Already top entry?
        if (index == 0)
            return;
        // Remove if further down in the list
        if (index > 0)
            paths.remove(index);
        // Add to front of list
        paths.add(0, path);
    }
    logger.log(Level.FINE, "Adding to jython path: {0}", path);
}
Also used : PyList(org.python.core.PyList)

Aggregations

PyList (org.python.core.PyList)6 PyString (org.python.core.PyString)3 PyObject (org.python.core.PyObject)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 Bindings (javax.script.Bindings)1 SimpleBindings (javax.script.SimpleBindings)1 Schema (org.apache.avro.Schema)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 PyTuple (org.python.core.PyTuple)1 JsonGenericRecordConverter (org.talend.daikon.avro.converter.JsonGenericRecordConverter)1 JsonSchemaInferrer (org.talend.daikon.avro.inferrer.JsonSchemaInferrer)1