From: Jaroslav G. <j_g...@ya...> - 2001-02-11 10:39:00
|
Hello Jython users/developers, I need to use embedded Jython for markup language template processing/generating because of Python's ability to seamlessly process string based data (in opposite to Java). I wrote a simple template processing (test) application based on the embedding example distributed with Jython. (see source code bellow) It works but it is significantly slow, probably because an template is parsed every time the renderFace() method is called. I am looking for more efficient solution, but Jython's documentation is very spare about this. The solution should work this way: The template is parsed the first time after initialization/change. The parsed Jython code is cached and re-used every time renderFace() method is called. The code cache is cleared upon setTemplate() method call. Consider please, the Foo and Writer instances can change with every renderFace() invocation. Any suggestions? Thanks Jaroslav Gergic ***Renderer.java*** import org.python.core.PyException; import org.python.core.PyInteger; import org.python.core.PyObject; import org.python.util.PythonInterpreter; public class Renderer { String template = null; PythonInterpreter pyi = null; public Renderer(String template) { pyi = new PythonInterpreter(); setTemplate(template); } public String getTemplate() { return template; } public void setTemplate(String tmpl) { template = tmpl; } public void renderFace(Foo obj, Writer out) { try { pyi.set("foo", obj); pyi.set("out", out); pyi.exec(template); } catch(Exception e) { e.printStackTrace(); } } } ***template.py*** out.write(""" <html> <head> <title>Test</title> </head> <body> <h1>Test</h1> """) out.write("<p>name is {" + foo.name + " }\n") out.write(""" </body> </html> """) ***end sources*** ===== Jaroslav Gergic (Gergi) mailto:j_g...@ya... http://nenya.ms.mff.cuni.cz/~gergic/ __________________________________________________ Do You Yahoo!? Get personalized email addresses from Yahoo! Mail - only $35 a year! http://personal.mail.yahoo.com/ |