From: Mark P. <li...@ma...> - 2004-10-21 20:48:41
|
PySystemState, didn't know about that I'll look into it now. We potentially have hundreds or thousands of eval or exec blocks which we compile and hold in memory so they can be called repeatedly with Py.runCode - where we pass different locals for variables on each runCode. Hence why we parse and compile all the strings with the expressions/blocks and hold a reference to the compiled code. This is for the http://drools.org project where the syntax I'm looking to support is something like below: <rule-set> <imports><python:import type="from java.util import Random" /></imports> <rule name="Goodbye Cruel World"> <parameter identifier="goodbye"> <python:class>java.lang.String</python:class> </parameter> <python:condition>goodbye == 'Goodbye'</python:condition> <python:consequence> print "%s Cruel World" % goodbye random = Random(); print "random: %d" % random.nextInt(); </python:consequence> </rule> </rule-set> So the condition is parsed and compiled with : this.node = ( modType ) parser.parse( this.text, "evals"); this.code = Py.compile( this.node, "<jython>" ); And the consequence is parsed and compiled with: this.node = ( modType ) parser.parse( this.text, "exec"); this.code = Py.compile( this.node, "<jython>" ); The declared imports need to be available to all nodes in the RuleSet, however I will have multiple RuleSets all with their own imports declared - so can't have system wide imports. Ideally I would also like to be able to specify standard python import notation. Mark Jim Adrig wrote: > If you are running these from Java, maybe something like what we do > will work? > > First we 'add_package' since we have the cachedir stuff turned off: > > // for Swing GUI building: > PySystemState.add_package("javax"); > PySystemState.add_package("javax.swing"); > PySystemState.add_package("javax.swing.text"); > PySystemState.add_package("java.awt"); // Color, etc. > PySystemState.add_package("java.awt.event"); > > Then create an interpreter and 'execfile' all our standard > imports/functions > > python.execfile( functionFileStream, functionFileName ); // pass > the name for parsing errors > > The 'functionFileStream' contains lots of things like: > > from javax.swing import JCheckBox, JComboBox, JFrame, JMenuBar, > JMenu, JMenuItem, JTable, JButton, JOptionPane, JRadioButton, JTextField > from java.awt import Color, GridLayout, GridBagLayout > > > On Oct 21, 2004, at 12:45 PM, Mark Proctor wrote: > >> I'm compiling expressions and blocks for use in our application, to >> this we pre-append import statements declared elsewhere. It works >> fine when type is "exec" for blocks but fails when its "eval" for >> expressions. Any ideas of how to get this to work? >> >> Mark >> >> protected Interp(String text, Imports imports, String type) >> { >> this.origininalText = text; >> StringBuffer newText = new StringBuffer(); >> >> if ((imports != null)&&(imports.getImportEntries() != null)) >> { >> Iterator it =imports.getImportEntries().iterator(); >> while (it.hasNext()) >> { ImportEntry importEntry = >> (ImportEntry) it.next(); >> if (importEntry instanceof PythonImportEntry) >> { >> newText.append(importEntry.getImportEntry()); >> newText.append(";"); >> newText.append(newline); >> } >> } } >> this.text = >> newText.append(stripOuterIndention( text )).toString(); >> >> this.node = ( modType ) parser.parse( this.text, type ); >> this.code = Py.compile( this.node, "<jython>" ); >> >> >> ------------------------------------------------------- >> This SF.net email is sponsored by: IT Product Guide on ITManagersJournal >> Use IT products in your business? Tell us what you think of them. >> Give us >> Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find >> out more >> http://productguide.itmanagersjournal.com/guidepromo.tmpl >> _______________________________________________ >> Jython-dev mailing list >> Jyt...@li... >> https://lists.sourceforge.net/lists/listinfo/jython-dev >> > > |