Again Ply and Jython ...

View: New views
2 Messages — Rating Filter:   Alert me  

Again Ply and Jython ...

by Markus Krosche-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello together,

I figured out something and I want to know the difference between the following two approaches:
(1) I have installed jython and the lastest version of ply (http://www.dabeaz.com/ply/) by running jython.bat setup.py install (which comes with the ply package).
It simply installs the py files inside the 'site-packages' folder.

Then I run the 'calc.py' example which comes with the ply package.

It runs fine. Everything works.


(2) I use Jython in my Java software and I want to use the module lex and yacc (which are part of the ply package).

Here is my code:

public class JythonScriptEngineProvider
{
    public ScriptEngine createEngine(final List<String> pathList)
    {
        PySystemState pySystemState = new PySystemState();
        for(String path: pathList)
        {
            pySystemState.path.append(Py.newString(path));
        }
        Py.setSystemState(pySystemState);
        final ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
        return engine;
    }
}


The pathList argument can be used to set the python path. And I am using the above class as follows:

List<String> pythonPath = new ArrayList<String>();
pythonPath.add("<my path to the jython lib>");
pythonPath.add("<my path to the jython lib>/site-packages");

ScriptEngine scriptEngine = new JythonScriptEngineProvider().createEngine(pythonPath);
scriptEngine.eval("import sys");
scriptEngine.eval("print sys.path");
scriptEngine.eval("import ply.lex as lex");
scriptEngine.eval("print lex");
scriptEngine.eval("lex.lex()");

The same error will result when calling the 'calc.py' example like this:

final String pyFile = "C:/DATA/download/ply-3.3/example/calc/calc.py";
scriptEngine.eval(new FileReader(new File(pyFile)));

The error is:

  File "<script>", line 1, in <module>
  File "C:\DATA\spt_workspace\MEPOtrunk\res\python\site-packages\ply\lex.py", line 887, in lex
    ldict = get_caller_module_dict(2)
  File "C:\DATA\spt_workspace\MEPOtrunk\res\python\site-packages\ply\lex.py", line 436, in get_caller_module_dict
    ldict = f.f_globals.copy()
  AttributeError: 'scope' object has no attribute 'copy'


Where is my mistake? What is the problem inside the code. I guess I am doing something completely wrong. But I did not understand it.

Thank you very much
Markus


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users

Re: Again Ply and Jython ...

by Markus Krosche-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

At least for my application I have solved the problem partly.
If I am not using the ScriptEngine it works.

In my simple test I did the following:

List<String> pythonPath = new ArrayList<String>();
pythonPath.add("<my path to jython lib>");
pythonPath.add("<my path to jython lib>/site-packages");
       
PySystemState pySystemState = new PySystemState();
for(String path: pythonPath)
{
     pySystemState.path.append(Py.newString(path));
}
Py.setSystemState(pySystemState);
PythonInterpreter p = new PythonInterpreter();
p.execfile("C:/DATA/download/ply-3.3/example/calc/calc.py");


The only question is now why it does not work when using the ScriptEngine ...

Best regards
Markus



2009/10/7 Markus Krosche <markus.krosche@...>
Hello together,

I figured out something and I want to know the difference between the following two approaches:
(1) I have installed jython and the lastest version of ply (http://www.dabeaz.com/ply/) by running jython.bat setup.py install (which comes with the ply package).
It simply installs the py files inside the 'site-packages' folder.

Then I run the 'calc.py' example which comes with the ply package.

It runs fine. Everything works.


(2) I use Jython in my Java software and I want to use the module lex and yacc (which are part of the ply package).

Here is my code:

public class JythonScriptEngineProvider
{
    public ScriptEngine createEngine(final List<String> pathList)
    {
        PySystemState pySystemState = new PySystemState();
        for(String path: pathList)
        {
            pySystemState.path.append(Py.newString(path));
        }
        Py.setSystemState(pySystemState);
        final ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
        return engine;
    }
}


The pathList argument can be used to set the python path. And I am using the above class as follows:

List<String> pythonPath = new ArrayList<String>();
pythonPath.add("<my path to the jython lib>");
pythonPath.add("<my path to the jython lib>/site-packages");

ScriptEngine scriptEngine = new JythonScriptEngineProvider().createEngine(pythonPath);
scriptEngine.eval("import sys");
scriptEngine.eval("print sys.path");
scriptEngine.eval("import ply.lex as lex");
scriptEngine.eval("print lex");
scriptEngine.eval("lex.lex()");

The same error will result when calling the 'calc.py' example like this:

final String pyFile = "C:/DATA/download/ply-3.3/example/calc/calc.py";
scriptEngine.eval(new FileReader(new File(pyFile)));

The error is:

  File "<script>", line 1, in <module>
  File "C:\DATA\spt_workspace\MEPOtrunk\res\python\site-packages\ply\lex.py", line 887, in lex
    ldict = get_caller_module_dict(2)
  File "C:\DATA\spt_workspace\MEPOtrunk\res\python\site-packages\ply\lex.py", line 436, in get_caller_module_dict
    ldict = f.f_globals.copy()
  AttributeError: 'scope' object has no attribute 'copy'


Where is my mistake? What is the problem inside the code. I guess I am doing something completely wrong. But I did not understand it.

Thank you very much
Markus



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users