[Question] Looping through array parameter in script

I have a named query returning a dataset that I am using as a parameter in a popup window. I can pull values out of the dataset, and do a transform to get the individual values within the view, but I am having issues when I try and get the individual values out using my event script for when the popup is closing.

Here is my base script for now:

	data = self.getSibling("EmbeddedView").props.params.datasource
	newOrEdit = self.view.params.newRecipe
	PN = self.view.params.PN
	Rev = system.db.runNamedQuery('AeroDef', 'GetMaxRevision', {"PN":PN}).getValueAt(0,0)+1
	log=system.util.getLogger("Garrett Logger")
	log.warn("%s"%data)
	i=0
	ribNo1 = data[0][2]
	dispRate1 = data[0][3]
	dispSpeed1 = data[0][4]
	beadH1 = data[0][5]
	beadW1 = data[0][6]
	log.warn("%s"%ribNo1)

If I run it as it is there, it throws an error saying the following.

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 10, in runAction TypeError: 'com.inductiveautomation.perspective.gateway.script.PropertyTreeScriptWrapper$MapWrapper' object is unsubscriptable

The data type as an array, so I tried pop, which will give me the row, but it returns as an ‘com.inductiveautomation.perspective.common.Immutab’ and I can’t get any data out of that easily it seems.

Any ideas on what the best way to approach this is?

1 Like

This is what the data variable is returning when I print it to the logs, though I added some linebreaks to make it readable

array(java.lang.Object, [QV[{BeadHeight=[2, Good, Wed Dec 31 19:00:00 EST 1969 (191)], Revision=[4, Good, Wed Dec 31 19:00:00 EST 1969 (191)], PartNum=[PN5 , Good, Wed Dec 31 19:00:00 EST 1969 (191)], BeadWidth=[2.2, Good, Wed Dec 31 19:00:00 EST 1969 (191)], RibNum=[1, Good, Wed Dec 31 19:00:00 EST 1969 (191)], DispenseRate=[1.23, Good, Wed Dec 31 19:00:00 EST 1969 (191)], DispenseSpeed=[1.25, Good, Wed Dec 31 19:00:00 EST 1969 (191)]}, Good, Wed Dec 31 19:00:00 EST 1969], 
QV[{BeadHeight=[2.1, Good, Wed Dec 31 19:00:00 EST 1969 (192)], Revision=[4, Good, Wed Dec 31 19:00:00 EST 1969 (191)], PartNum=[PN5 , Good, Wed Dec 31 19:00:00 EST 1969 (191)], BeadWidth=[2.5, Good, Wed Dec 31 19:00:00 EST 1969 (192)], RibNum=[2, Good, Wed Dec 31 19:00:00 EST 1969 (191)], DispenseRate=[1.35, Good, Wed Dec 31 19:00:00 EST 1969 (192)], DispenseSpeed=[1.35, Good, Wed Dec 31 19:00:00 EST 1969 (192)]}, Good, Wed Dec 31 19:00:00 EST 1969], 
QV[{BeadHeight=[4.2, Good, Wed Dec 31 19:00:00 EST 1969 (192)], Revision=[4, Good, Wed Dec 31 19:00:00 EST 1969 (192)], PartNum=[PN5 , Good, Wed Dec 31 19:00:00 EST 1969 (192)], BeadWidth=[1.2, Good, Wed Dec 31 19:00:00 EST 1969 (192)], RibNum=[3, Good, Wed Dec 31 19:00:00 EST 1969 (192)], DispenseRate=[3.12, Good, Wed Dec 31 19:00:00 EST 1969 (192)], DispenseSpeed=[1.4, Good, Wed Dec 31 19:00:00 EST 1969 (192)]}, Good, Wed Dec 31 19:00:00 EST 1969], 
QV[{BeadHeight=[2, Good, Wed Dec 31 19:00:00 EST 1969 (192)], Revision=[4, Good, Wed Dec 31 19:00:00 EST 1969 (192)], PartNum=[PN5 , Good, Wed Dec 31 19:00:00 EST 1969 (192)], BeadWidth=[2.1, Good, Wed Dec 31 19:00:00 EST 1969 (192)], RibNum=[4, Good, Wed Dec 31 19:00:00 EST 1969 (192)], DispenseRate=[2.2, Good, Wed Dec 31 19:00:00 EST 1969 (192)], DispenseSpeed=[2.1, Good, Wed Dec 31 19:00:00 EST 1969 (192)]}, Good, Wed Dec 31 19:00:00 EST 1969], 
QV[{BeadHeight=[2.1, Good, Wed Dec 31 19:00:00 EST 1969 (192)], Revision=[4, Good, Wed Dec 31 19:00:00 EST 1969 (192)], PartNum=[PN5 , Good, Wed Dec 31 19:00:00 EST 1969 (192)], BeadWidth=[1.9, Good, Wed Dec 31 19:00:00 EST 1969 (192)], RibNum=[5, Good, Wed Dec 31 19:00:00 EST 1969 (192)], DispenseRate=[1.4, Good, Wed Dec 31 19:00:00 EST 1969 (192)], DispenseSpeed=[1.5, Good, Wed Dec 31 19:00:00 EST 1969 (192)]}, Good, Wed Dec 31 19:00:00 EST 1969]])

Can you try something like:

dataList = data.items()

and then print the dataList and see what you get?

Also, do you mean to be accessing the data variable rather than the results of your query?

in runAction AttributeError: 'array.array' object has no attribute 'items'

Yes data variable is what I am trying to access. I mentioned the name query since it is populated with the data from that when the window loads, but I guess it isn’t really important to troubleshooting.

Try calling data[0].items() instead.

It seems you’re getting an array of these property tree MapWrapper objects…

I think you’re meant to access it like JSON, it’s not a dataset.

Still not finding that attribute.

Is there an way to loop through the values in JSON? The parameter shows as dataset in the property editor, so that’s where I started, but as long as I can extract the data for the database, I’m fine with going another route.

I’ll have to let someone more familiar with Perspective come in and figure this out, I’m still not sure what that object is. The “QV” in it might mean it’s an array of QualifiedValues, and each of the values in the QV is a MapWrapper object.

Glad we aren’t the only ones a bit stumped. It seems to be returning a PyArray when I pull it into the data variable, but we can’t get much further down into the array to get to the actual values.

Figured it out, given the tree MapWrapper object, we were able to call the specific attributes as defined by the original query.

edit for clarification: image shows how datasource is displayed in property editor.
image

data = self.getSibling("EmbeddedView").props.params.datasource
length = len(data)
for i in range(len(data)):
	ds = data[i]
	rev= ds.Revision
	rib = ds.RibNum
	disRate = ds.DispenseRate
	disSpeed = ds.DispenseSpeed
	bh = ds.BeadHeight
	bw = ds.BeadWidth
2 Likes