Search in sources :

Example 41 with PyObject

use of org.python.core.PyObject in project score by CloudSlang.

the class EmbeddedPythonExecutorWrapper method processExecResults.

private PythonExecutionResult processExecResults() {
    Iterator<PyObject> localsIterator = pythonInterpreter.getLocals().asIterable().iterator();
    Map<String, Serializable> returnValue = new HashMap<>();
    while (localsIterator.hasNext()) {
        PyObject next = localsIterator.next();
        String key = next.asString();
        PyObject value = pythonInterpreter.get(key);
        if (!isLocalEntryExcluded(key, value)) {
            returnValue.put(key, resolveJythonObjectToJavaForExec(value, key));
        }
    }
    return new PythonExecutionResult(returnValue);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) PyString(org.python.core.PyString) PyObject(org.python.core.PyObject) PythonExecutionResult(io.cloudslang.runtime.api.python.PythonExecutionResult)

Example 42 with PyObject

use of org.python.core.PyObject in project scriptographer by scriptographer.

the class JythonCallable method call.

public Object call(Object obj, Object[] args) throws JythonException {
    // function on it.
    try {
        PyObject[] wrappedArgs = new PyObject[args == null ? 1 : args.length + 1];
        // self
        wrappedArgs[0] = Py.java2py(obj);
        // args
        for (int i = 1; i < wrappedArgs.length; i++) wrappedArgs[i] = Py.java2py(args[i - 1]);
        PyObject ret = function.__call__(wrappedArgs);
        // unwrap if the return value is a native java object:
        Object res = ret.__tojava__(Object.class);
        return res != Py.NoConversion ? res : ret;
    } catch (PyException e) {
        throw new JythonException(e);
    }
}
Also used : PyException(org.python.core.PyException) PyObject(org.python.core.PyObject) PyObject(org.python.core.PyObject)

Example 43 with PyObject

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

Example 44 with PyObject

use of org.python.core.PyObject 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 45 with PyObject

use of org.python.core.PyObject in project OpenRefine by OpenRefine.

the class JythonHasFieldsWrapper method __finditem__.

@Override
public PyObject __finditem__(PyObject key) {
    String k = (String) key.__tojava__(String.class);
    Object v = _obj.getField(k, _bindings);
    if (v != null) {
        if (v instanceof PyObject) {
            return (PyObject) v;
        } else if (v instanceof HasFields) {
            return new JythonHasFieldsWrapper((HasFields) v, _bindings);
        } else if (Py.getAdapter().canAdapt(v)) {
            return Py.java2py(v);
        } else {
            return new JythonObjectWrapper(v);
        }
    } else {
        return null;
    }
}
Also used : PyObject(org.python.core.PyObject) HasFields(com.google.refine.expr.HasFields) PyObject(org.python.core.PyObject)

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