|
From: <bc...@wo...> - 2001-09-11 19:41:06
|
[Ted Berg]
This is just a very minor suggestion to your perfectly valid example.
>I am shipping the standard modules as part of my application jar. I list
>jython.jar as a required extension ( placed in lib\ext ) and then append
>"/fully qualified pathname to my jar!Lib" to the sys.path of the interpreter.
>
>Here's the code I use to do so. Just make a call like this:
>
>addDirToInterpPath( myInterp, "Lib" );
>
>---
>
> void addDirToInterpPath( org.python.util.PythonInterpreter interp, String
>subdir )
> {
> interp.exec( "import sys" );
You don't need this. See below.
> // leading and trailing slashes required to get the URL for a directory.
> if ( !subdir.startsWith( "/" ) )
> subdir = "/"+subdir;
> if ( !subdir.endsWith( "/" ) )
> subdir = subdir + "/";
>
> // get the URL
> java.net.URL u = MainFrame.class.getResource( subdir );
>
> // make the URL jython friendly
> StringBuffer buf = new StringBuffer( u.toString() );
>
> // replace the !/ pair with ! so jython can find the directory
> int index = buf.toString().indexOf( "!/" );
> if ( index > -1 )
> {
> buf.replace( index, index + 2, "!" );
> }
>
> // remove the protocol part of the URL
> buf.replace( 0, buf.toString().lastIndexOf( ":" ) + 1, "" );
> if ( buf.toString().endsWith( "/" ) )
> buf.setLength( buf.toString().length() -1 );
>
> // append the path to sys.path
> interp.exec( "sys.path.append( '" + buf.toString() + "' )" );
Calling exec() is quite slow compared to calling the API directly:
PySystemState sys = Py.getSystemState();
sys.path.append(new PyString(buf.toString()));
OTOH when addDirToInterpPath() is only called once it hardly matter at
all except to my sense of rightness.
Thanks for posting your example.
regards,
finn
|