Hi.
[Waclawczyk Janusch]
> 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
The list -> array thing is there for convenience and because array
are the java way to spell varargs (that also implies readonly usage of
the array) but it doesn't come without danger, I rember a mail of someone
trying to pass a python list to a java stream read method: passing the list
worked but the result was not the expected one <wink>, list -> array
creates a new array with a copy of the contents of the list and modifications
get lost.
m.invoke(o,[2,3,"a"]) # is very nice
l=[0]*200
s.read(l) # fatal
Further with this kind of conversion we know to what the list elements
should be converted (the array el type), this is good for sanity-checking,
that would not be possible for mappings.
I really don't know if a similar copy semantic makes sense for mappings
(probably not), it is difficult to define/implement an alternative
semantic (the topic was already discussed once ) (a python object
inherits from PyObject so cannot inherit form Hashtable, etc, yes
there are some better possibility with java2 collections interfaces,
but ... see the jython-dev archive).
AN ALTERNATIVE:
if you know that you should pass a python dict to a function that takes
an Hashtable (and similar) you could use such a java object also on the python
side, jython already allows to use it somehow like a dict...
Jython 2.0 on java1.3.0 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> from java.util import Hashtable
>>> h=Hashtable()
>>> h['x']=3
>>> h['z']=5
>>> for k in h.keys(): # keys here is the java method (e.g. h do not offer an
has_key):
... print h[k]
...
3
5
>>>
jython allows to use [] and/or for foo in ...
(when/if the construct is naturally meaningful with):
java.util.Vector
java.util.Enumeration
java.util.Dictionary
under java2:
java.util.List
java.util.Iterator
java.util.Map
java.util.Collections
by insertion/extraction python types are naturally mapped:
int <-> java.lang.Integer
str <-> java.lang.String
etc
regards, Samuele Pedroni.
|