You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I am trying to parse a return dict type from python in java, and I tried different things in chaquopy to get the equivalent for dict object but all of them failed.
pyobject.keySet(): returns different keys (I thing the data members of the dict class).
Also the dict contains different types like ({name='name', age='25', boss='False'}), I did not find any code sample to how use the dict in the demo source code.
So how I can use the dict Python object in java.
Thanks.
The text was updated successfully, but these errors were encountered:
mhsmith
changed the title
Java equivalent to Python dict
Java equivalent to Python containers
Jan 26, 2018
You should be able to use the dict by calling its Python methods using callAttr. For example:
PyObjectd = /* something returning the dict described above */;
Stringname = d.callAttr("get", "name").toString();
intage = d.callAttr("get", "age").toJava(int.class);
Or to iterate through the dict:
PyObjectkeys = py.getBuiltins().callAttr("list", d.callAttr("keys"));
intlen = keys.callAttr("__len__").toJava(int.class);
for (inti = 0; i < len; i++) {
Stringkey = keys.callAttr("__getitem__", i).toString();
Stringvalue = d.callAttr("get", key).toString(); // or .toJava(...)
}
Of course it would be much easier if we could access Python containers through the corresponding Java interfaces. We may implement that in the future, so let's leave this issue open to track it.
Hi,
I am trying to parse a return dict type from python in java, and I tried different things in chaquopy to get the equivalent for dict object but all of them failed.
pyobject.keySet(): returns different keys (I thing the data members of the dict class).
Also the dict contains different types like ({name='name', age='25', boss='False'}), I did not find any code sample to how use the dict in the demo source code.
So how I can use the dict Python object in java.
Thanks.
The text was updated successfully, but these errors were encountered: