ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> Jython-dev
Jython-dev
Re: [Jython-dev] imports with parser type eval
by Jim Adrig other posts by this author
Oct 21 2004 10:51PM messages near this date
[Jython-dev] Re: imports with parser type eval | RE: [Jython-dev] When is Jython 2.2 due for release?
After 'execfile'ing our imports, we actually then loop through the=20
resulting Map and retrieve the pointers to all the imports and=20
functions; then for each subsequent execution we just add the imports=20
and functions to 'locals()' directly.

It may not be very 'Python-like' but it is very fast...

- Jim Adrig

On Oct 21, 2004, at 2:53 PM, Mark Proctor wrote:

>   Just found out why I can't use PythonInterp shared amongst RuleSet.=20=

>  While the imports are shared and some of the functions each=20
>  condition/consequence has its own variables passed as locals and no=20
>  mangling or sharing should occur; hence why we were using=20
>  Py.runCode(PyCode code, PyObject locals, PyObject globals)=A0 - All=20
>  variables are passed via a dict andthe locals parameter.
> 
>   Mark
>   Mark Proctor wrote:
>   Just tried that, needed to be wrapped in a function otherwise it=20
>  errors. Gonna get the shared interp thing working.
> 
>   Mark
>   Mark Proctor wrote:
>   just chatting to one of the other developers, think we might be able=20=

>  to have a PythonInterpretor for each RuleSet, which should solve this=20=

>  issue. Otherwise whats potentially wrong with doing an exec with the=20=

>  following to emulate an expression:
>   =A0=A0=A0=A0=A0=A0=A0 String tempText =3D stripOuterIndention( text =
);
>   =A0=A0=A0=A0=A0=A0=A0 if (type.equals("eval"))
>   =A0=A0=A0=A0=A0=A0=A0 {
>   =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 tempText =3D "return (" + tempText =
+ ")";
>   =A0=A0=A0=A0=A0=A0=A0 }
>   =A0=A0=A0=A0=A0=A0=A0 this.text =3D =
newText.append(tempText).toString();
> 
> 
>   Mark
>   Oti wrote:
>  IMHO you cannot eval an import statement:
>    Jython 2.1 on java1.4.2_05 (JIT: null)
>    Type "copyright", "credits" or "license" for more information.
>    >>> exec("from java.lang import String")
>    >>> eval("from java.lang import String")
>    Traceback (innermost last):
>      File "<console>", line 1, in ?
>      File "<string>", line 1
>          from java.lang import String
>          ^
>    SyntaxError: invalid syntax
>    >>>
> 
>  so you have to exec the imports first, e.g. like Jim suggested.
> 
>  Best wishes,
>  Oti.
> 
>  --- Jim Adrig <jim@[...].com>  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 ); //=20=

>  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 =3D text;
>         StringBuffer newText =3D new StringBuffer();
> 
>         if ((imports !=3D null)&&(imports.getImportEntries() !=3D =
null))
>         {
>             Iterator it =3Dimports.getImportEntries().iterator();
>                       while (it.hasNext())
>             {                              ImportEntry importEntry =3D
> 
>  (ImportEntry) it.next();
>                 if (importEntry instanceof PythonImportEntry)
>                 {
>                     newText.append(importEntry.getImportEntry());
>                     newText.append(";");
>                     newText.append(newline);
>                 }
>             }                         }
>                              this.text =3D
>  newText.append(stripOuterIndention( text )).toString();
> 
>         this.node =3D ( modType ) parser.parse( this.text, type );
>         this.code =3D Py.compile( this.node, "<jython>" );
> 
> 
> 
> 
> 
> 
> 
> 
>  =20=
Attachments:
unknown1


Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved