How to verify if parameter is of type list or JsonifiableArrayList?

I want to verify that the data being received by a binding transform is in the form of either a list or a JsonifiableArrayList, since that seems to be the type that common array properties are stored as.

I’ve been trying to access the data type like so:

from com.inductiveautomation.perspective.gateway.script import JsonifiableArrayList

However, when I try to access the JsonifiableArrayList class to compare the data’s type to, I get the following error:

ImportError: No module named perspective

I verified that this class is in my environment with the supplied package path, so I have no idea why this is happening.

How did you do this?

(Gateway modules are loaded into classloaders isolated from each other and inaccessible to scripts.)

I made a property binding to an empty array, then added a script transform that just returned the type of the array.

Right. Perspective itself has full access to its own classes. Scripts to not have access via classloader/import, only via objects Perspective hands to them.

I see, thank you.

Is there a typical standard for verifying that a value being received by a script transform is a list? Is the typical approach just to check if the value is iterable but not a string or a dictionary?

Yes. Check hasattr(obj, 'items') to catch dictionaries, then hasattr(obj, '__iter__') to identify iterables, then check for strings.

3 Likes

This is the way. Check if it's list like (but not a string or dictionary), don't try to guarantee which type you're actually receiving. Ignition scripting methods may choose to return a Java array, a Java list, a special Java object that acts as a distinct Python class, or anything else; any strict isinstance check is only going to lead to headaches down the road.

2 Likes