For this type of integration, use a Jython object factory, this is substantially more efficient than using eval/exec because it avoids recompiling Python code on every single call:

http://jythonpodcast.hostjava.net/jythonbook/en/.99/JythonAndJavaIntegration.html#more-efficient-version-of-loosely-coupled-object-factory

(FYI  - archive readers - this link is unfortunately not going to be permanent, but a search against Jython book and the above terms should be fine in the future.)

On Mon, Mar 15, 2010 at 5:27 AM, <john.m.baker@nomura.com> wrote:
This was correct.  Thanks.

> -----Original Message-----
> From: Alan Kennedy [mailto:jython-dev@xhaus.com]
> Sent: 11 March 2010 15:39
> To: jython users
> Subject: Re: [Jython-users] Invoking Jython from java.
>
> [John]
> > I'm sure this is documented somewhere but I can't spot it - what's
> the best
> > way of running a Jython script that returns a Java object, from a
> piece of
> > Java?
> >
> > Something like:
> >
> >          PythonInterpreter pi = pythonInterpreter();
> >
> >          pi.exec("import jython.database");
> >
> >          something =
> > (Something)pi.eval("jython.database.createSomething("+key+")");
>
> Try something like this (not tested)
>
> PythonInterpreter pi = pythonInterpreter();
> pi.exec("import jython.database");
> pi.exec("result = jython.database.createSomething("+key+")");
> PyObject pyObject = pi.get("result");
> Object temp = pyObject.__tojava__(Something.class);
> if (temp == Py.NoConversion)
>   { throw new Exception("Result is not an instance of Something
> class"); }
> something = (Something)temp;
>
> Or you could probably condense lines 3+4 into
>
> PyObject pyObject =
> pi.eval("jython.database.createSomething("+key+")");
>
> HTH,
>
> Alan.
>
> -----------------------------------------------------------------------
> -------
> Download Intel&#174; Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> _______________________________________________
> Jython-users mailing list
> Jython-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jython-users



This e-mail (including any attachments) is confidential, may contain
proprietary or privileged information and is intended for the named
recipient(s) only. Unintended recipients are prohibited from taking action
on the basis of information in this e-mail and must delete all copies.
Nomura will not accept responsibility or liability for the accuracy or
completeness of, or the presence of any virus or disabling code in, this
e-mail. If verification is sought please request a hard copy. Any reference
to the terms of executed transactions should be treated as preliminary only
and subject to formal written confirmation by Nomura. Nomura reserves the
right to monitor e-mail communications through its networks (in accordance
with applicable laws). No confidentiality or privilege is waived or lost by
Nomura by any mistransmission of this e-mail. Any reference to "Nomura" is
a reference to any entity in the Nomura Holdings, Inc. group. Please read
our Electronic Communications Legal Notice which forms part of this e-mail:
http://www.Nomura.com/email_disclaimer.htm


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Jython-users mailing list
Jython-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-users



--
Jim Baker
jbaker@zyasoft.com