I am attempting to write a script that loops through the selectionData property of a Tree and take the Value property of each item and adds it to a list. This works great when the value is a simple string. However if the value is a list( the user selects the folder in the tree, whose data property is a list of all the data values of its children) I need to loop through that list to pull out the individual string values, so that my final list can be a simple list of strings. I tried using the type() function on the list it returns âclass com.inductiveautomation.perspective.gateway.script.JsonifiableArrayListâ but then if I do an if statement using that condition it doesnât work. Any advice?
You can try duck-typing (try/catch around iterating items in the value
, e.g.)
try:
for item in value:
values.append(item)
except:
values.append(value)
Or you can use the Python builtin hasattr
to check for the __iter__
âdunderâ method, e.g.:
if hasattr(value, "__iter__"):
for item in value:
values.append(item)
else:
values.append(value)
I tried the âduck-typingâ method however it appears that a string is iterable and so it returned each letter of the string as a separate list item. I will try your second suggestion
1 Like
Blockquote Or you can use the Python builtin
hasattr
to check for the__iter__
âdunderâ method, e.g.:
if hasattr(value, "__iter__"):
for item in value:
values.append(item)
else:
values.append(value)
This worked
1 Like