Thankyou so much for this.  Truly appreciated....


I'd rather be riding.... 


-------- Original message --------
From: Jeff Emanuel <jemanuel@frii.com>
Date: 11/02/2013 5:57 PM (GMT-05:00)
To: jython-dev@lists.sourceforge.net
Subject: Re: [Jython-dev] Adding Code As A Module


This works for me:

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.python.util.PythonInterpreter;
import org.python.core.*;

public class TestJYTHON {

  static String SCRIPTCODE =
      "import sys\n" +
      "\n" +
      "class Test:\n" +
      "  def __init__(self):\n" +
      "    self.name = \"Test\"\n";



  public static void main(String[] args) {

    InputStream stream = new ByteArrayInputStream(SCRIPTCODE.getBytes());

    PythonInterpreter allocator = new PythonInterpreter();

    PyCode code = allocator.compile(SCRIPTCODE);
    PyObject module = imp.createFromCode("X",code,"embedded script");
    allocator.exec("import sys\n");
    allocator.exec("X=sys.modules['X']\n");
    allocator.exec("a = X.Test()\n");
    allocator.exec("print a\n");

  }
}


On 11/2/2013 3:21 PM, Susan And Richard Pike wrote:
Same result.  The Module is there but the code is not loaded / run.


Nov 2, 2013 01:20:18 PM, jemanuel@frii.com wrote:
I haven't done this, but from a quick perusal of org/python/core/imp.java
it looks like you can call imp.createFromCode(String name, PyCode c, String moduleLocation).
First you would need to compile your embedded source to get a PyCode by
calling PythonInterpreter.compile. So my guess is you'd do this:

PyCode code = allocator.compile(SCRIPTCODE);
PyModule module = imp.createFromCode("X",code,"embedded script");
allocator.exec("a = X.Test();");






On 11/2/2013 5:02 AM, Susan And Richard Pike wrote:
> I am using the embedded interpreter and would like to add embedded script code as a module. Alas I am stuck.
>
> example below:
>
> package testjython;
>
> import java.io.ByteArrayInputStream;
> import java.io.InputStream;
> import org.python.util.PythonInterpreter;
>
> public class TestJYTHON {
>
> static String SCRIPTCODE = "import sys\n" +
> "\n" +
> "class Test:\n" +
> " def __init__(self):\n" +
> " self.name = \"Test\"\n";
>
>
>
> public static void main(String[] args) {
>
> InputStream stream = new ByteArrayInputStream(SCRIPTCODE.getBytes());
>
> PythonInterpreter allocator = new PythonInterpreter();
>
> // Import as module "X"
> allocator.execfile(stream, SCRIPTCODE);
>
> allocator.exec("a = X.Test");
> }
> }
>
> ------------------------------------------------------------------------------
> Android is increasing in popularity, but the open development platform that
> developers love is also attractive to malware creators. Download this white
> paper to learn more about secure code signing practices that can help keep
> Android apps secure.
> http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk
> _______________________________________________
> Jython-dev mailing list
> Jython-dev@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jython-dev


------------------------------------------------------------------------------
Android is increasing in popularity, but the open development platform that
developers love is also attractive to malware creators. Download this white
paper to learn more about secure code signing practices that can help keep
Android apps secure.
http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk
_______________________________________________
Jython-dev mailing list
Jython-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-dev


------------------------------------------------------------------------------
Android is increasing in popularity, but the open development platform that
developers love is also attractive to malware creators. Download this white
paper to learn more about secure code signing practices that can help keep
Android apps secure.
http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk


_______________________________________________
Jython-dev mailing list
Jython-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-dev