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-users
Jython-users
Re: How to add built in functions? (was RE: [Jython-users] How to import scripts thats not stored as files?)
by Satya Ghattu other posts by this author
Mar 16 2004 4:28PM messages near this date
How to add built in functions? (was RE: [Jython-users] How to import scripts thats not stored as files?) | RE: [Jython-users] How to import scripts thats not stored as files?
Sten,
see my post with subject (Defining commands in the shell ....).
You should extend the jython grammar which is not recommended.

-satya

Sten Ernerot wrote:

> Hi again
> 
> Anyone know how to add a built in function?
> 
> So in a script you should be able to write:
> 
> myPrint "this is a test"
>   
> 
> >> this is a test (printed from myPrint)
> >>      
> >>
> Print myPrint
>   
> 
> >> <java function myPrint at 12345678>
> >>      
> >>
> 
> Regards
> Sten
> 
> -----Original Message-----
> From: Updike, Clark [mailto:Clark.Updike@[...].edu] 
> Sent: den 16 mars 2004 16:22
> To: 'Sten Ernerot'
> Subject: RE: [Jython-users] How to import scripts thats not stored as files?
> 
> I like it--the jython equivalent of java.net.URLClassLoader.
> Thanks for the follow up post.
> 
> -Clark
> 
> -----Original Message-----
> From: Sten Ernerot [mailto:ernerot@[...].net]
> Sent: Tuesday, March 16, 2004 10:01 AM
> To: Jython-users@[...].net
> Cc: 'Jeff Emanuel'
> Subject: 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
> 
> 
> 
> -------------------------------------------------------
> 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
> 
>   
> 
Attachments:
unknown1

Thread:
Sten Ernerot
Satya Ghattu

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