本文整理汇总了Java中org.python.core.Py.java2py方法的典型用法代码示例。如果您正苦于以下问题:Java Py.java2py方法的具体用法?Java Py.java2py怎么用?Java Py.java2py使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.python.core.Py
的用法示例。
在下文中一共展示了Py.java2py方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: callFunc
import org.python.core.Py; //导入方法依赖的package包/类
public static Object callFunc(Class<?> c, String funcName, Object... binds) {
try {
PyObject obj = ScriptManager.python.get(funcName);
if (obj != null && obj instanceof PyFunction) {
PyFunction func = (PyFunction) obj;
PyObject[] objects = new PyObject[binds.length];
for (int i = 0; i < binds.length; i++) {
Object bind = binds[i];
objects[i] = Py.java2py(bind);
}
return func.__call__(objects).__tojava__(c);
} else
return null;
} catch (PyException ex) {
ex.printStackTrace();
return null;
}
}
示例2: pigToPython
import org.python.core.Py; //导入方法依赖的package包/类
public static PyObject pigToPython(Object object) {
if (object instanceof Tuple) {
return pigTupleToPyTuple((Tuple) object);
} else if (object instanceof DataBag) {
PyList list = new PyList();
for (Tuple bagTuple : (DataBag) object) {
list.add(pigTupleToPyTuple(bagTuple));
}
return list;
} else if (object instanceof Map<?, ?>) {
PyDictionary newMap = new PyDictionary();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
newMap.put(entry.getKey(), pigToPython(entry.getValue()));
}
return newMap;
} else if (object instanceof DataByteArray) {
return Py.java2py(((DataByteArray) object).get());
} else {
return Py.java2py(object);
}
}
示例3: createObject
import org.python.core.Py; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public T createObject(Object[] args, String[] keywords) {
PyObject[] convertedArgs = new PyObject[args.length];
for (int i = 0; i < args.length; i++) {
convertedArgs[i] = Py.java2py(args[i]);
}
return (T)pyClass.__call__(convertedArgs, keywords).__tojava__(javaClass);
}
示例4: create
import org.python.core.Py; //导入方法依赖的package包/类
public static PyObjectValue create(Serializable content, boolean sensitive) {
PyObject pyObject = Py.java2py(content);
try {
PyObjectValueProxyClass proxyClass = getProxyClass(pyObject);
PyObjectValue pyObjectValue = (PyObjectValue) proxyClass.getConstructor()
.newInstance(proxyClass.getParams());
((Proxy) pyObjectValue).setHandler(new PyObjectValueMethodHandler(content, sensitive, pyObject));
return pyObjectValue;
} catch (Exception e) {
throw new RuntimeException("Failed to create a proxy to new instance for PyObjectValue and " +
pyObject.getClass().getSimpleName(), e);
}
}
示例5: setVariable
import org.python.core.Py; //导入方法依赖的package包/类
public void setVariable(String name, Object value) throws Exception {
if (mPySimulator == null) {
logAndThrowException("Python simulator instance not loaded");
}
try {
PyObject pyValue = (value instanceof PyObject) ? (PyObject) value : Py.java2py(value);
mPySimulator.__setattr__(new PyString(name), pyValue);
} catch (PyException e) {
logAndThrowException("Error while setting value of " + name + " variable of Python simulator instance", e);
}
}
示例6: 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);
}
}
示例7: get
import org.python.core.Py; //导入方法依赖的package包/类
/**
* Get arbitrary PyObject
*
* @return PyObject
*/
public PyObject get() {
return Py.java2py(column.get());
}
示例8: java2py
import org.python.core.Py; //导入方法依赖的package包/类
public static PyObject java2py(final Object o) { return Py.java2py(o); }