From: Waclawczyk J. <Jan...@ic...> - 2001-06-06 13:22:46
|
Hello, I try to ask again (hopefully more precisely): INTRODUCTION. I'm often using jython to test my java software (my jython scripts call java methods, I' using a mix of Junit- and PyUnit-Framework). Some weeks ago Samuele Pedroni wrote me, that there is even automatic type coercion from jython 2-dimensional lists to java 2-dimensional arrays when calling a java method from jython(see http://aspn.activestate.com/ASPN/Mail/msg/Jython-users:493058). It means if you define in a Java class Test1 a method like the the following: static public void workOn2DimArr(String[][] arr) { // some code } then you can call it in the jython script with the 2-dimensional list as argument for this method: >>> import Test1 >>> Test1.workOn2DimArr([["jan", "hello"],["jan", "look"]]) >>> It works also with the integers! I think it's an example for an excellent level of jython-java integration. I use this feature very extensively. (By the way: I would extend the information at http://jython.sourceforge.net/docs/usejava.html with the hint, that there is automatic type coercion support also for multidimensional arrays) QUESTION. I think, the same level of AUTOMATIC type coercion should be possible to jy-dictionaries/Hashtables(or related Classes) (see my first mail) and jy-Lists/Vectors. If it is possible for List/Arrays, why not for dictionaries/Hashtables ? Regards, Janusz original Messages: Date: Tue, 5 Jun 2001 13:39:04 -0400 From: D-Man <ds...@ri...> To: "'jyt...@li...'" <jyt...@li...> Subject: Re: [Jython-users] Automatic type coercion for python dictionaries? On Tue, Jun 05, 2001 at 03:29:08PM +0200, Waclawczyk Janusch wrote: | Hello, | | I try to call in my jython script a java method, which expects | Hashtable as argument. But it fails: | | Jython 2.0 on java1.1.8 (JIT: ) | >>> import Test1 # it is my java test class | >>> Test1.testPyToJavaHash({"key":"value"}) | TypeError: testPyToJavaHash(): 1st arg can't be coerced to java.util.Hashtable | | Why is it not possible? (due to primitive types ? ) | It would be a nice feature ! (e.g. int -> Integer, ...) There is a lot of automatic type coercion done by Jython. See the section "Calling Java Methods and Functions" at http://jython.sourceforge.net/docs/usejava.html. I don't think jython does any Java<->Java coercion but it does Python<->Java coercion. I haven't tried passing Python dictionaries or lists to a Java method that wants an instance of a Java class yet. -D |