public Object invoke(Object... args) throws Exception { PyObject[] pyArgs = new PyObject[args.length]; for (int i = 0; i < args.length; i++) pyArgs[i] = Py.java2py(args[i]); PyObject pyResult = fn.__call__(pyArgs); if (pyResult == null) return null; // todo: number conversions should be handled higher up in the code, and not at the Jython level. if (pyResult instanceof PyLong || pyResult instanceof PyInteger) return pyResult.__tojava__(Long.class); Object result = pyResult.__tojava__(Object.class); if (result == Py.NoConversion) throw new RuntimeException("Cannot convert Python object " + pyResult + " to java."); return result; }
@Override public PyObject __call__(ThreadState state, PyObject[] args) { return __call__(state, args, Py.NoKeywords); }
@Override public PyObject __call__(PyObject arg1, PyObject arg2) { return __call__(Py.getThreadState(), arg1, arg2); }
@Override public PyObject __call__(PyObject[] args) { return __call__(Py.getThreadState(), args); }
@Override public PyObject __call__(PyObject arg0, PyObject arg1, PyObject arg2, PyObject arg3) { return __call__(Py.getThreadState(), arg0, arg1, arg2, arg3); }
@Override public PyObject __call__(PyObject arg1, PyObject[] args, String[] keywords) { return __call__(Py.getThreadState(), arg1, args, keywords); }
Object call(PyFunction f, Object... args) { PyObject[] pyargs = new PyObject[args.length]; for (int i = 0; i < args.length; i++) { pyargs[i] = Py.java2py(args[i]); } return f.__call__(pyargs); } }
@Override public PyObject __call__() { return __call__(Py.getThreadState()); }
@Override public PyObject __call__(PyObject arg) { return __call__(Py.getThreadState(), arg); }
@Override public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) { return __call__(Py.getThreadState(), arg1, arg2, arg3); }
@Override public PyObject __call__(PyObject[] args, String[] keywords) { return __call__(Py.getThreadState(), args, keywords); }
public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable { // Handle invocation when invoked through Proxy (as coerced to single method interface) if (method.getDeclaringClass() == Object.class) { return method.invoke( this, args ); } else if (args == null || args.length == 0) { return __call__().__tojava__(method.getReturnType()); } else { return __call__(Py.javas2pys(args)).__tojava__(method.getReturnType()); } }
@Override public Object exec(Tuple tuple) throws IOException { try { if (tuple == null || (num_parameters == 0 && !((PyTableCode)function.__code__).varargs)) { // ignore input tuple PyObject out = function.__call__(); return JythonUtils.pythonToPig(out); } else { // this way we get the elements of the tuple as parameters instead // of one tuple object PyObject[] params = JythonUtils.pigTupleToPyTuple(tuple).getArray(); return JythonUtils.pythonToPig(function.__call__(params)); } } catch (PyException e) { throw new ExecException("Error executing function", e); } catch (Exception e) { throw new IOException("Error executing function", e); } }
public static boolean XLargerThanY(int x, int y) throws ScriptException { PyScriptEngineFactory factory = new PyScriptEngineFactory(); ScriptEngine engine = factory.getScriptEngine(); PyFunction function = (PyFunction) engine.eval("x > y"); PyBoolean ans = (PyBoolean) function.__call__(new PyInteger(x), new PyInteger(y)); return ans.getBooleanValue(); }
import org.python.core.PyFunction; import org.python.core.PyInteger; import org.python.core.PyObject; import org.python.util.PythonInterpreter; public class PythonScript { public static void main(String args[]) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("/home/XXX/XXX/test.py"); PyFunction pyFuntion = (PyFunction)interpreter.get("add",PyFunction.class); int a = 10, b = 20 ; PyObject pyobj = pyFuntion.__call__(new PyInteger(a), new PyInteger(b)); System.out.println("result = " + pyobj.toString()); } }
/** * Highlight the given code piece using the provided lexer and formatter. * * @param code * @param lexer * @param formatter * @return */ public String highlight(String code, Lexer lexer, Formatter formatter) { PyFunction function = pythonInterpreter.get("highlight", PyFunction.class); PyString pyCode = new PyString(code); PyObject pyLexer = lexer.getLexer(); PyObject pyFormatter = formatter.getFormatter(); return function.__call__(pyCode, pyLexer, pyFormatter).asString(); }
+ outputSchemaFunc + " is not a schema function"); Object newSchema = ((pf.__call__(Py.java2py(input))).__tojava__(Object.class)); if (newSchema instanceof ResourceSchema) { return(Schema.getPigSchema((ResourceSchema) newSchema));