|
From: Heshan S. <hes...@gm...> - 2008-04-28 06:08:33
|
On Fri, Apr 25, 2008 at 9:03 PM, Jeff Emanuel <jem...@fr...> wrote:
> 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
>
> Hi Jeff,
I used the method above to convert the Dictionary to a Map. When
the method returns a javaMap , I am collecting it using a PyObject (within
my java code).Therefore I have to convert it to a Map.At that moment it
gives a class cast exception. Is there an alternative way of doing this
correctly?
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()");
String str4 = "dictToJavaMap(" + obj + ")";
PyObject obj2 = interp.eval(str4);
//aDictionary[k].get('operationName')
//String str = "" + obj + "[].get('operationName')";
//interp.exec(str);
Map map = (Map) obj2;
/*
The above line gives a exception like below
Exception in thread "main" java.lang.ClassCastException:
org.python.core.PyJavaInstance cannot be cast to java.util.Map
*/
Iterator k = map.keySet().iterator();
while (k.hasNext()) {
String key = (String) k.next();
System.out.println("Key " + key + "; Value " +
(String) map.get(key));
}
Thanx in advance
--
Regards,
Heshan Suriyaarachchi
|