When a script function has a keyword args dictionnary,
what is the bast practice to convert the args for the java world…and obtain an HashMap object
PyArgumentMap args = PyArgumentMap.interpretPyArgs(pyArgs, keywords, AbstractScriptModule.class, "myFunction");
PyArgumentMap is a
HashMap. Did whoever wrote it do that for no reason or is that what you want?
my script function has multiple args with some optionnal.
one of those args is intended to be a dictionnary
for String, Interger args … we have args.getString(), …
but args.get() return an Object, I need to cast it to a map
Ah, I see. The value for that argument is probably a
Yes it’s a PyDictionary. Is tyhere any built-in sdk function to convert to an HashMap ?
PyDictionary is already a
ConcurrentMap (so, a
Create a copy if you want:
If you can bump the SDK version to 8.0.11 or higher, you can use PyUtilities, which has a few utility methods; you would
com.inductiveautomation.ignition.common.PyUtilities#streamEntries then use the collector implementation in the same file (or roll your own). As Kevin points out, it’s already a map (of PyObjects), so you will probably want to use
TypeUtilities.pyToJava() on individual elements to get native Java types.
If you do bump the SDK version, you can also use PyArgParser, which is (in my opinion, at least, since I wrote it ) a bit nicer to use re: type coercion. It also has a
getAsMap() method, so you can retrieve a user-supplied argument as a (Java typed) map in one line:
com.inductiveautomation.ignition.common.script.PyArgParser#getAsMap(java.lang.String, java.lang.Class<K>, java.lang.Class<V>)