开发者

Does using python files in Jython require python classes?

I am trying to implement Python code I've written in Java and I want to use Jython. There are a number of websites that provide information about how to build Jython object factories, but all of the examples use Python classes. For example:

http://jythonpodcast.hostjava.net/jythonbook/en/1.0/JythonAndJavaIntegration.html#using-jython-within开发者_StackOverflow-java-applications

However, I am not using classes in my code. That is, I've got a number of files that each contain several function definitions and but no classes. Therefore, I wanted to know if anyone could point me to a resource or provide information about if it is possible to use Jython to bridge the gap between Java and Python without having classes.

Thanks.


If I understand what you're asking correctly (I take it you want to invoke a Python function from Java code), I think that is possible.

This is completely untested and a bit speculative, but it looks like Jython provides a Java object model that will allow you to do what you want. The important thing to understand here is that Python stores functions as attributes of a dictionary for each module (Python file). Thus, in pure Python, one can get and use the function os.path.join() doing something like:

import os
getattr(getattr(os, 'path'), 'join').__call__('.', 'directory')

Not the most Pythonic syntax around, but I think it helps clarify how one can use the PyObject interface to load an invoke a Python function from a Python module.

To do this from Java it seems that you would need to follow a pattern similar to List 10-5 in the section of the Jython book you linked to. There, in the first constructor, is an example of how to import a Python class (also stored as an attribute of a Python module). Modifying this somewhat, you'd end up with something like:

class PythonExecutor {
    public static void main(String...args) {
        String moduleName = "...";
        String functionName = "...";

        PyObject importer = state.getBuiltins().__getitem__(Py.newString("__import__"));

        // module = __import__(moduleName)
        PyObject module = importer.__call__(Py.newString(moduleName));

        // function = getattr(module, functionName)
        PyObject function = module.__getattr__(functionName);

        // function.__call__()
        function.__call__();
    }
}

Of course, you can already see from the above snippet some of the disadvantages Java has compared to dynamic languages. This is particularly acute if your Python function requires any sort of parameters, as any Java objects would need to be marshalled into PyObjects. Again, the listing mentioned above has several examples using the java2py() method of the Py class to convert a Java object into a Python object. Similarly, if your Python function returns some sort of result, you will likely need to convert it into a Java object using the __tojava__() function on PyObject.

To handle all of these details, it may be worth your time to construct a Java adapter for the Python modules you will need to access from your Java code, similar to the way the JythonObjectFactory provides several methods which convert Java objects into Python objects, invoke the __init__() method on a Python class, and return the result as a Java object.

Hopefully, that's enough to point you in the right direction on this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜