1

I have a Java application which calls some Python code. I have some problem in understanding how returning lists from Python to java.

Let's assume I have a very simple python module as follow:

def myFunc(param):
    print param
    return [ ["a","b"] , ["c","d"] , ["e","f"] ]

And a java piece of code that call this function:

String funcName = "myFunc";
try {
    PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.exec("from myModule import * \n");
    PyObject someFunc = interpreter.get(funcName);
    someFunc.__call__(new PyString(workspacePath),new PyString(deployName));
}
catch(Exception e){
    e.printStackTrace();
    throw new Exception("Error while generating client resources.", e);
}

How can I transform the returned object to something that i can easily iterate over? How can I extract the string which are present in the list?

I tried messing around a bit with PyList or Iterable PyObject but with no success so far. Any idea?

Thanks

2
  • First you need to keep the returned PyList, PyList pl = someFunc.__call__(etc) Commented Jan 23, 2015 at 14:47
  • the problem is that the returnd type for the moment is PyNone that i cannot directly cast to PyList. someFunc.__call__(new PyString(deployPath)).asIterable() also does not return an Iterable object. Commented Jan 23, 2015 at 15:02

1 Answer 1

1

You can construct Java objects. If the intended consumer is a Java program, that may be the thing to do.

import java.util.ArrayList as ArrayList
import java.lang.String as String
...
javaList = ArrayList()
for subList in [ ["a","b"] , ["c","d"] , ["e","f"] ]:
    javaSubList = ArrayList()
    for s in subList:
        javaSubList.add(String(s))
    javaList.add(javaSubList)
return javaList
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.