當前位置: 首頁>>代碼示例>>Java>>正文


Java Py.tojava方法代碼示例

本文整理匯總了Java中org.python.core.Py.tojava方法的典型用法代碼示例。如果您正苦於以下問題:Java Py.tojava方法的具體用法?Java Py.tojava怎麽用?Java Py.tojava使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.python.core.Py的用法示例。


在下文中一共展示了Py.tojava方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getSensitive

import org.python.core.Py; //導入方法依賴的package包/類
private boolean getSensitive(Map<String, Serializable> executionResultContext, boolean systemPropertiesInContext) {
    if (systemPropertiesInContext) {
        Map<String, Serializable> context = new HashMap<>(executionResultContext);
        PyObject rawSystemProperties = (PyObject) context.remove(SYSTEM_PROPERTIES_MAP);
        @SuppressWarnings("unchecked")
        Map<String, Value> systemProperties = Py.tojava(rawSystemProperties, Map.class);
        @SuppressWarnings("unchecked")
        Collection<Serializable> systemPropertyValues = (Collection) systemProperties.values();
        return checkSensitivity(systemPropertyValues) || checkSensitivity(context.values());
    } else {
        return (checkSensitivity(executionResultContext.values()));
    }
}
 
開發者ID:CloudSlang,項目名稱:cloud-slang,代碼行數:14,代碼來源:ScriptEvaluator.java

示例2: perform

import org.python.core.Py; //導入方法依賴的package包/類
/**
 * Perform the operation by calling a function in the Python script. This method adapts each of
 * the inputs into Python objects, calls the Python function, and then converts the outputs of the
 * function back into Java objects and assigns them to the outputs array. The Python function
 * should return a tuple, list, or other sequence containing the outputs.  If there is only one
 * output, it can just return a value.  Either way, the number of inputs and outputs should match
 * up with the number of parameters and return values of the function.
 */
@Override
public void perform() {
  PyObject[] pyInputs = new PyObject[inputSockets.size()];
  for (int i = 0; i < inputSockets.size(); i++) {
    pyInputs[i] = Py.java2py(inputSockets.get(i).getValue().get());
  }

  try {
    PyObject pyOutput = this.scriptFile.performFunction().__call__(pyInputs);

    if (pyOutput.isSequenceType()) {
      /*
       * If the Python function returned a sequence type, there must be multiple outputs for
       * this step.
       * Each element in the sequence is assigned to one output socket.
       */
      PySequence pySequence = (PySequence) pyOutput;
      Object[] javaOutputs = Py.tojava(pySequence, Object[].class);

      if (outputSockets.size() != javaOutputs.length) {
        throw new IllegalArgumentException(wrongNumberOfArgumentsMsg(outputSockets.size(),
            javaOutputs.length));
      }

      for (int i = 0; i < javaOutputs.length; i++) {
        outputSockets.get(i).setValue(javaOutputs[i]);
      }
    } else {
      /* If the Python script did not return a sequence, there should only be one
      output socket. */
      if (outputSockets.size() != 1) {
        throw new IllegalArgumentException(wrongNumberOfArgumentsMsg(outputSockets.size(), 1));
      }

      Object javaOutput = Py.tojava(pyOutput, outputSockets.get(0).getSocketHint().getType());
      outputSockets.get(0).setValue(javaOutput);
    }
  } catch (RuntimeException e) {
    /* Exceptions can happen if there's a mistake in a Python script, so just print a
    stack trace and leave the
     * current state of the output sockets alone.
     *
     * TODO: communicate the error to the GUI.
     */
    logger.log(Level.WARNING, e.getMessage(), e);
  }
}
 
開發者ID:WPIRoboticsProjects,項目名稱:GRIP,代碼行數:56,代碼來源:PythonScriptOperation.java

示例3: pythonToPig

import org.python.core.Py; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public static Object pythonToPig(PyObject pyObject) throws ExecException {
    try {
        Object javaObj = null;
        // Add code for all supported pig types here
        // Tuple, bag, map, int, long, float, double, chararray, bytearray 
        if (pyObject instanceof PyTuple) {
            PyTuple pyTuple = (PyTuple) pyObject;
            Object[] tuple = new Object[pyTuple.size()];
            int i = 0;
            for (PyObject tupleObject : pyTuple.getArray()) {
                tuple[i++] = pythonToPig(tupleObject);
            }
            javaObj = tupleFactory.newTuple(Arrays.asList(tuple));
        } else if (pyObject instanceof PyList) {
            DataBag list = bagFactory.newDefaultBag();
            for (PyObject bagTuple : ((PyList) pyObject).asIterable()) {
                // If the item of the array is not a tuple, 
                // wrap it into tuple before adding to bag
                Object pigBagItem = pythonToPig(bagTuple);
                Tuple pigBagTuple;
                if (!(pigBagItem instanceof Tuple)) {
                    pigBagTuple = TupleFactory.getInstance().newTuple(1);
                    pigBagTuple.set(0, pigBagItem);
                } else {
                    pigBagTuple = (Tuple)pigBagItem;
                }
                list.add(pigBagTuple);
            }
            javaObj = list;
        } else if (pyObject instanceof PyDictionary) {
            Map<?, Object> map = Py.tojava(pyObject, Map.class);
            Map<Object, Object> newMap = new HashMap<Object, Object>();
            for (Map.Entry<?, Object> entry : map.entrySet()) {
                if (entry.getValue() instanceof PyObject) {
                    newMap.put(entry.getKey(), pythonToPig((PyObject) entry.getValue()));
                } else {
                    // Jython sometimes uses directly the java class: for example for integers
                    newMap.put(entry.getKey(), entry.getValue());
                }
            }
            javaObj = newMap;
        } else if (pyObject instanceof PyLong) {
            javaObj = pyObject.__tojava__(Long.class);
        } else if (pyObject instanceof PyBoolean) {
        	javaObj = pyObject.__tojava__(Boolean.class);
        } else if (pyObject instanceof PyInteger) {
            javaObj = pyObject.__tojava__(Integer.class);
        } else if (pyObject instanceof PyFloat) {
            // J(P)ython is loosely typed, supports only float type, 
            // hence we convert everything to double to save precision
            javaObj = pyObject.__tojava__(Double.class);
        } else if (pyObject instanceof PyString) {
            javaObj = pyObject.__tojava__(String.class);
        } else if (pyObject instanceof PyNone) {
            return null;
        } else {
            javaObj = pyObject.__tojava__(byte[].class);
            // if we successfully converted to byte[]
            if(javaObj instanceof byte[]) {
                javaObj = new DataByteArray((byte[])javaObj);
            }
            else {
                throw new ExecException("Non supported pig datatype found, cast failed: "+(pyObject==null?null:pyObject.getClass().getName()));
            }
        }
        if(javaObj.equals(Py.NoConversion)) {
            throw new ExecException("Cannot cast into any pig supported type: "+(pyObject==null?null:pyObject.getClass().getName()));
        }
        return javaObj;
    } catch (Exception e) {
        throw new ExecException("Cannot convert jython type ("+(pyObject==null?null:pyObject.getClass().getName())+") to pig datatype "+ e, e);
    }
}
 
開發者ID:sigmoidanalytics,項目名稱:spork-streaming,代碼行數:75,代碼來源:JythonUtils.java

示例4: pythonToPig

import org.python.core.Py; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public static Object pythonToPig(PyObject pyObject) throws ExecException {
    try {
        Object javaObj = null;
        // Add code for all supported pig types here
        // Tuple, bag, map, int, long, float, double, chararray, bytearray 
        if (pyObject instanceof PyTuple) {
            PyTuple pyTuple = (PyTuple) pyObject;
            Object[] tuple = new Object[pyTuple.size()];
            int i = 0;
            for (PyObject tupleObject : pyTuple.getArray()) {
                tuple[i++] = pythonToPig(tupleObject);
            }
            javaObj = tupleFactory.newTuple(Arrays.asList(tuple));
        } else if (pyObject instanceof PyList) {
            DataBag list = bagFactory.newDefaultBag();
            for (PyObject bagTuple : ((PyList) pyObject).asIterable()) {
                // In jython, list need not be a bag of tuples, as it is in case of pig
                // So we fail with cast exception if we dont find tuples inside bag
                // This is consistent with java udf (bag should be filled with tuples)
                list.add((Tuple) pythonToPig(bagTuple));
            }
            javaObj = list;
        } else if (pyObject instanceof PyDictionary) {
            Map<?, Object> map = Py.tojava(pyObject, Map.class);
            Map<Object, Object> newMap = new HashMap<Object, Object>();
            for (Map.Entry<?, Object> entry : map.entrySet()) {
                if (entry.getValue() instanceof PyObject) {
                    newMap.put(entry.getKey(), pythonToPig((PyObject) entry.getValue()));
                } else {
                    // Jython sometimes uses directly the java class: for example for integers
                    newMap.put(entry.getKey(), entry.getValue());
                }
            }
            javaObj = newMap;
        } else if (pyObject instanceof PyLong) {
            javaObj = pyObject.__tojava__(Long.class);
        } else if (pyObject instanceof PyInteger) {
            javaObj = pyObject.__tojava__(Integer.class);
        } else if (pyObject instanceof PyFloat) {
            // J(P)ython is loosely typed, supports only float type, 
            // hence we convert everything to double to save precision
            javaObj = pyObject.__tojava__(Double.class);
        } else if (pyObject instanceof PyString) {
            javaObj = pyObject.__tojava__(String.class);
        } else if (pyObject instanceof PyNone) {
            return null;
        } else {
            javaObj = pyObject.__tojava__(byte[].class);
            // if we successfully converted to byte[]
            if(javaObj instanceof byte[]) {
                javaObj = new DataByteArray((byte[])javaObj);
            }
            else {
                throw new ExecException("Non supported pig datatype found, cast failed: "+(pyObject==null?null:pyObject.getClass().getName()));
            }
        }
        if(javaObj.equals(Py.NoConversion)) {
            throw new ExecException("Cannot cast into any pig supported type: "+(pyObject==null?null:pyObject.getClass().getName()));
        }
        return javaObj;
    } catch (Exception e) {
        throw new ExecException("Cannot convert jython type ("+(pyObject==null?null:pyObject.getClass().getName())+") to pig datatype "+ e, e);
    }
}
 
開發者ID:PonIC,項目名稱:PonIC,代碼行數:66,代碼來源:JythonUtils.java

示例5: tojava

import org.python.core.Py; //導入方法依賴的package包/類
public static <T> Object tojava(final PyObject o, final Class<T> c) { return Py.tojava(o, c); } 
開發者ID:MitchWeaver,項目名稱:sjgs,代碼行數:2,代碼來源:PyUtils.java


注:本文中的org.python.core.Py.tojava方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。