当前位置: 首页>>代码示例>>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;未经允许,请勿转载。