|
From: Jeff E. <jem...@fr...> - 2008-04-25 15:33:29
|
That's correct, unfortunately PyDictionary does not
implement java.util.Map.
You need to copy the dictionary to a Java map before
passing it to Java code.
def dictToJavaMap(dict):
import java
javaMap =java.util.HashMap()
for i in dict.items():
javaMap[i[0]]=i[1]
return javaMap
Heshan Suriyaarachchi wrote:
> Hi,
> I am trying to execute the below java code.But when I try to assign the
> dictionary to map it gives an classcast exception as below
>
> Exception in thread "main" java.lang.ClassCastException:
> org.python.core.PyDictionary cannot be cast to java.util.Map
>
>
> # myfile.py
> # method which returns a Dictionary
> def getDictionary():
> Dictionary2 = {}
> Dictionary2['add'] = {'returns': 'int', 'operationName': 'add', 'var1':
> 'integer', 'var2': 'integer'}
> Dictionary2['deduct'] = {'returns': 'double', 'operationName': 'deduct',
> 'var1': 'integer', 'var2': 'integer'}
> Dictionary2['f'] = {'a': 'double', 'returns': 'double', 'operationName':
> 'f'}
> return Dictionary2
>
> # test.java
> # main method which calls the above method
> public static void main(String args []) throws PyException {
> System.out.println("testing 123");
> PySystemState.initialize();
> PythonInterpreter interp = new PythonInterpreter();
> interp.exec("import sys");
> interp.exec("print sys.path");
>
> String str3 =
> "/home/heshan/workspace/IdeaProjects/PYtest/src/essentialScripts/myfile.py";
> interp.execfile(str3);
> PyObject obj = interp.eval("getDictionary()");
> Map map = (Map) obj;
>
> Iterator k = map.keySet().iterator();
> while (k.hasNext()) {
> String key = (String) k.next();
> System.out.println("Key " + key + "; Value " +
> (String) map.get(key));
> }
> }
>
>
>
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
> Don't miss this year's exciting event. There's still time to save $100.
> Use priority code J8TL2D2.
> http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Jython-users mailing list
> Jyt...@li...
> https://lists.sourceforge.net/lists/listinfo/jython-users
|