RE: [Jython-users] How to import scripts thats not stored as files?
by Sten Ernerot other posts by this author
Mar 16 2004 3:01PM messages near this date
Re: How to add built in functions? (was RE: [Jython-users] How to
import scripts thats not stored as files?)
|
RE: How to add built in functions? (was RE: [Jython-users] How to
import scripts thats not stored as files?)
Hi
After investigating further I actually found a way that works...
Regards
Sten
------------------------------------------------------------------------
public void loadScript(String importTo, URI uri, String as)
throws Exception {
String script = getScript(uri);
if (script == null) {
//FIX throw exception not found
System.out.println("Could not load " + uri);
return;
}
PythonInterpreter interp = getInterpreter();
PyStringMap dict = new PyStringMap();
String name;
if (as == null)
name = toFilename(uri.toString());
else
name = as;
PyModule module = addModule(name);
InputStream in = new StringBufferInputStream(script + "\n\n");
PyCode code = Py.compile(in, name, "exec");
Py.exec(code, module.__dict__, module.__dict__);
if (importTo == null) {
importTo = "main";
}
//get modules
PyObject modules = Py.getSystemState().modules;
PyModule loadInto = (PyModule) modules.__finditem__(importTo);
//FIX if load into empty throw exception?
if (loadInto != null) loadInto.__dict__.__setitem__(name, module);
}
-----Original Message-----
From: Jeff Emanuel [mailto:JEmanuel@[...].com]
Sent: den 16 mars 2004 15:34
To: 'Sten Ernerot'
Subject: RE: [Jython-users] How to import scripts thats not stored as files?
I don't know if it will solve all your problems, but the jython code expects
String keys to be interned.
Try adding
toImport = toImport.intern();
name = name.intern();
before toImport and name are passed as arguments.
-----Original Message-----
From: Sten Ernerot [mailto:ernerot@[...].net]
Sent: Monday, March 15, 2004 5:56 PM
To: Jython-users@[...].net
Subject: [Jython-users] How to import scripts thats not stored as files?
Hi
I want to import a script thats not stored as a file.
I want it to work as "import nameofscript as ImportedScript"
The below code sort of work. Its work in the console but not as a statement
in a file.
Anyone has a better/working solution?
Many thanks
Sten
----------------------------------------------------------------------------
---
in Jython:
myStore.loadScript(__name__, nameofscript, ImportedScript)
----------------------------------------------------------------------------
---
public void loadScript(String importTo, URI uri, String as) throws
Exception {
String script = getScript(uri);
if (script == null) {
System.out.println("Could not load " + uri);
return; //Should throw error
}
PythonInterpreter interp = getInterpreter();
PyStringMap dict = new PyStringMap();
String name;
if (as == null)
name = toFilename(uri.toString());
else
name = as;
//create a module for the script
PyModule module = new PyModule(name, dict);
InputStream in = new StringBufferInputStream(script + "\n\n");
PyCode code = Py.compile(in, name, "exec");
Py.exec(code, module.__dict__, module.__dict__);
if (importTo == null){
interp.getLocals().__setitem__(name, module);
} else{
PyFrame frame = Py.getFrame();
PyModule tmp = (PyModule) frame.getglobal(importTo);
//if importTo is a module thats currently loading
//this doesnt work since it doesnt exist yet
if (tmp != null)
tmp.__dict__.__setitem__(name, module);
}
}
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial
presented by Daniel Robbins, President and CEO of GenToo technologies. Learn
everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
Jython-users mailing list
Jython-users@[...].net
https://lists.sourceforge.net/lists/listinfo/jython-users
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
Jython-users mailing list
Jython-users@[...].net
https://lists.sourceforge.net/lists/listinfo/jython-users
|