From: Robert W. B. <rb...@di...> - 2001-03-22 20:05:09
|
Hi Ron, On Thu, 22 Mar 2001 Ron...@Ne... wrote: > > I have a bunch of properties that I get from the system using Java code, > since that is what handles properties. The data I get back is of type > java.lang.String. These things are used all over the place. > > Which makes more sense: write all the code in Java and just drive it > with Jython, or convert the String data to Python strings? In the latter > case, how do I do the conversion? The "more sense" part is too subjective to really say. String, PyString, PyJavaInstance, and PyInstance make things easy either way you go. As far as converting String to PyString, the PyString constructor takes a String so: String s1; PyString s2 = new PyString(s1); is all you need to make a String a PyString. Below is a little java class that shows what I mean about the ease of moving back and forth between java and jython. file: StringStuff.java ------------------------------------------------------------ import org.python.core.*; import org.python.util.PythonInterpreter; public class StringStuff { public static void main(String[] args) { String osname = System.getProperty("os.name"); System.out.println("Java String in Java --> " + osname); PythonInterpreter interp = new PythonInterpreter(); interp.set("osname", new PyString(osname)); interp.exec("print 'PyString in interpreter -->', osname"); // to check type if you want // interp.exec("print type(osname)"); PyString pyosname = (PyString) interp.get("osname"); System.out.println("PyString in Java --> " + pyosname); interp.set("javaosname", new PyJavaInstance(osname)); interp.exec("print 'java String in interpreter, " + "(which is actually a PyInstance) -->'," + " javaosname"); // to check type again if wanted //interp.exec("print type(javaosname)"); } } ------------------------------------------------------------------ My personal, subjective opinion- you can't beat Jython/Python for string processing, i.e. >>>from java.lang import System >>>lotsofproperties = System.getProperties() Ahh, how easy things get :) Cheers, Robert |