<p><font size="2"><br></font></p><p><font size="2">At our last meeting, I mentioned my use of python as a utility</font>
<br><font size="2">to make a Java application "programmable". There seemed to</font>
<br><font size="2">be some interest in this technique. I used this technique in a </font>
<br><font size="2">web app I built for one of my clients. They needed to change</font>
<br><font size="2">formulas for calculating aluminium window frame sizes without having</font>
<br><font size="2">me involved. Sometime the calculating formula would change</font>
<br><font size="2">by 0.25 of an inch…</font>
</p>
<p><font size="2">So, here is a technique where python interpreter is embedded</font>
<br><font size="2">in Java code, the Java then for the sake of this demo, sets</font>
<br><font size="2">a object in Python called X - does a little bit of manipulation</font>
<br><font size="2">of it on the Python interpreter, and then, sets a variable of</font>
<br><font size="2">the executing class SimpleEmbedded via "this" - and then</font>
<br><font size="2">execute a callback from Python back into Java… This very</font>
<br><font size="2">cool feature alone, makes the <span class="il">Jython</span> interpreter very versatile</font>
<br><font size="2">(and potentially dangerous)…</font>
</p>
<p><font size="2">PyObject shown below is the most primitive Object that</font>
<br><font size="2">can be easily cast if need be into other objects… Python</font>
<br><font size="2">also does have an eval() function which can lead to other</font>
<br><font size="2">very interesting and convoluted possibilities...</font>
</p>
<p><font size="2">References:</font>
</p>
<p><a href="http://www.jython.org/" target="_blank"><u><font color="#0000ff" size="2">http://www.<span class="il">jython</span>.org/</font></u></a>
</p>
<p><font size="2">Embedding Python in Lisp (maybe somebody really well versed in lisp can try it??)</font>
</p>
<p><a href="http://common-lisp.net/project/python-on-lisp/" target="_blank"><u><font color="#0000ff" size="2">http://common-lisp.net/project/python-on-lisp/</font></u></a>
</p>
<p><font size="2">==============================================</font>
</p>
<p><font size="2">Sample running code…</font>
</p>
<p><font size="2">==============================</font>
</p>
<p><font size="2">File: SimpleEmbedded.java</font>
</p>
<p><font size="2">import org.python.util.PythonInterpreter;</font>
<br><font size="2">import org.python.core.*;</font>
</p>
<p><font size="2">public class SimpleEmbedded {</font>
<br>
<br>
<br><font size="2">// this method below will be called back by the Python interpreter</font>
<br> <font size="2">public void nasty(Object PyObject) {</font>
<br>
<br> <font size="2">System.out.println(" python sez - "+PyObject);</font>
<br>
<br> <font size="2">}</font>
<br>
</p>
<p> <font size="2">public SimpleEmbedded() {</font>
</p>
<p> <font size="2"> PythonInterpreter interp = new PythonInterpreter();</font>
</p>
<p> <font size="2"> interp.exec("import sys");</font>
<br> <font size="2"> interp.exec("print sys");</font>
</p>
<p> <font size="2"> interp.set("a", new PyInteger(42));</font>
<br> <font size="2"> interp.exec("print a");</font>
<br> <font size="2"> interp.exec("x = 2+2");</font>
<br> <font size="2"> PyObject x = interp.get("x");</font>
</p>
<p> <font size="2"> System.out.println("x: "+x);</font>
<br> <font size="2">
interp.set("myjavaclass", this); //YEAH BABY... very cool here.. set
Python object myjavaclass the "address" of this object - a callback of
sorts</font></p>
<p> <font size="2"> interp.execfile("c:/test.py");</font>
<br>
<br> <font size="2">}</font>
<br>
<br><font size="2"> public static void main(String []args) throws PyException {</font>
</p>
<p><font size="2"> SimpleEmbedded se = new SimpleEmbedded();</font>
<br><font size="2"> </font>
<br><font size="2"> }</font>
<br><font size="2">}</font>
</p>
<br>
<br>
<br>
<p><font size="2">==============================</font>
</p>
<p><font size="2">Test.py</font>
</p>
<br>
<p><font size="2">### Variable x has been set by the Java program to the inerpreter..</font>
</p>
<p><font size="2">## just a little simple math for the sake of the demo</font>
<br><font size="2">num = x*4</font>
</p>
<p><font size="2">## Calling BACK the Java method nasty -- the ability to do this gives</font>
<br><font size="2">## us the ability to build very very complex 5gen type very high level</font>
<br><font size="2">## utilities that analysts and other non-programmers can use..</font>
</p>
<p><font size="2">myjavaclass.nasty(num)</font>
</p>
<br>
<p><font size="2">==============================</font>
</p>
<br>
<p><font size="2">Output on console... below</font>
</p>
<br>
<br>
<br>
<p><font size="2"><module 'sys' (built-in)></font>
<br><font size="2">42</font>
<br><font size="2">x: 4</font>
<br><font size="2"> python sez - 16</font>
</p>
<br><br>==============================================================<br><br>I did not want to include this code into the git-hub at this point, and if you think it's of<br>itnerest, I will put it into the git-hub...<br>
<br>Regards, Alex.<br><br><br>