From: Jim B. <jb...@zy...> - 2009-07-21 16:41:29
|
The object factory presented here http://kenai.com/projects/plyjy/sources/mercurial/content/src/org/plyjy/factory/PySystemObjectFactory.java?rev=2 is quite usable. However, Tobias and I discussed this further, and we do need to refine it further (use a static factory method instead of a public constructor, genericize, change its over lengthy name, cache a private instance of PySystemState). If we can stabilize this API soon, it could be included for the forthcoming 2.5.1 release. - Jim On Tue, Jul 21, 2009 at 5:57 AM, Tobias Ivarsson <th...@gm...> wrote: > Gabriel, > > I would rather say that there currently is no way of defining interfaces > for Java code from Jython. You can still use Python code from Java, you just > need to define the interfaces (or base classes) in Java and provide the > implementation in Python. The object factory pattern is very useful for > accessing these implementations from Java. > > Jim Baker and Josh Juneau have a library over at Kenai that makes Jython > object factories as easy as "new JythonObjectFactory(...);" (or something > like that): http://kenai.com/projects/plyjy, I don't know if this is > production ready yet, but you can at least look at their code to find out > how to implement an object factory, it's less than a page of code (I've > included the code in presentation slides from time to time). > > So, a summary of the best way to use Python code from Java at the moment: > > 1. Define the interface(s) for the Python code in Java: > package com.pany.package; > public interface MyCodeThingy { > String someMethod(int value); > } > > 2. Implement the interface(s) in Python: > from com.pany.package import MyCodeThingy > class MyImplementation(MyCodeThingy): > def __init__(self, prefix): > self.prefix = prefix > def someMethod(self, value): > return "%s - %s" % (self.prefix, value) # Really advanced code > - you could NEVER do this in Java > > 3. Instantiate an object factory for the Python code in Java: > ObjectFactory<MyCodeThingy> codeThingyFactory = new > JythonObjectFactory<>(...); // Not sure about the actual interface here > > 4. Use the object factory to create instances of your Python class in Java: > String prefix = "The answer to the question about life, universe and > everything"; > MyCodeThingy thing = codeThingyFactory.create(prefix); > String result = thingy.someMethod(42); > assert result.equals("The answer to the question about life, universe > and everything - 42"); > > Jim, Josh - How close to being usable like this example is PlyJy at the > moment? > > Happy hacking, > Tobias > > > On Tue, Jul 21, 2009 at 12:53 PM, Gabriel Rossetti < > gab...@ar...> wrote: > >> Hello Tobias, >> >> So if I understand it correctly there is currently no way to use python >> code in Java (since jythonc is gone and the current proposal not being >> implemented)? >> >> Cheers, >> Gabriel >> >> Tobias Ivarsson wrote: >> >>> Hi Gabriel, >>> >>> These are not examples of how to do it in the current Jython 2.5 release, >>> but a suggestion for what this could could look like when the Jythonc >>> replacement is done. The Jythonc replacement is still in an early draft >>> stage and will likely be tentatively introduced in some 2.5.x release, we >>> had originally planned to introduce a first version for people to play with >>> in 2.5.1, but it is starting to look like we will have to push out 2.5.1 >>> earlier than planned due to some recent bug fixes. >>> >>> Cheers, >>> Tobias >>> >>> On Tue, Jul 21, 2009 at 11:06 AM, Gabriel Rossetti < >>> gab...@ar... <mailto:gab...@ar...>> wrote: >>> >>> Hello everyone, >>> >>> I instaled Jython 2.5 and tried the example on this page : >>> http://wiki.python.org/jython/ReplaceJythonc and it doesn't work, >>> I get : >>> >>> NameError: name 'javamethod' is not defined >>> >>> does anyone know how to fix this, I can't find much concerning this on >>> google. >>> >>> Thank you, >>> Gabriel >>> >>> >>> ------------------------------------------------------------------------------ >>> Enter the BlackBerry Developer Challenge >>> This is your chance to win up to $100,000 in prizes! For a limited >>> time, >>> vendors submitting new applications to BlackBerry App World(TM) >>> will have >>> the opportunity to enter the BlackBerry Developer Challenge. See >>> full prize >>> details at: http://p.sf.net/sfu/Challenge >>> _______________________________________________ >>> Jython-users mailing list >>> Jyt...@li... >>> <mailto:Jyt...@li...> >>> https://lists.sourceforge.net/lists/listinfo/jython-users >>> >>> >>> > > > ------------------------------------------------------------------------------ > Enter the BlackBerry Developer Challenge > This is your chance to win up to $100,000 in prizes! For a limited time, > vendors submitting new applications to BlackBerry App World(TM) will have > the opportunity to enter the BlackBerry Developer Challenge. See full prize > details at: http://p.sf.net/sfu/Challenge > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users > > -- Jim Baker jb...@zy... |