Iterating multiple conditional statements, for loop

Hey,
So I’m still trying to improve my knowledge on how ignition datasets work. What i’m practicing with now is taking one table and distributing it into 5 different tables through a series of conditional statements.

the goal of the code is when I press a button the code,

  1. reads data from Sampler column.
  2. compares row value in column 6 to conditional values.
  3. appends entire row to respective data table.
  4. changes to the new window
sourceTable = event.source.parent.getComponent('Table')
dataOut = sourceTable.data
pythonDataset = system.dataset.toPyDataSet(dataOut)


header = ["col1", "col2", "col3", "col4", "col5",
 "col6", "Sampler", "col7", "col8"]

NewData = []
for row in dataOut:
		if row[6]== "Sampler 1":
			NewData.append(row)
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT1").data = SamData
		elif row[6]== "Sampler 2":
			NewData.append(row)
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT2").data = SamData
		elif row[6]== "Sampler 3":
			NewData.append(row)
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT3").data = SamData
		elif row[6]== "Sampler 4":
			NewData.append(row)
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT4").data = SamData
		elif row[6]== "Sampler 5":
			NewData.append(row)
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT5").data = SamData
			
window = system.nav.openWindow('advance')
system.nav.centerWindow(window)
system.nav.closeParentWindow(event)

This is the Error I get from the Code:

Traceback (most recent call last):
File “event:actionPerformed”, line 15, in
TypeError: ‘com.inductiveautomation.ignition.common.BasicDataset’ object is not iterable

You should be iterating over your newly-created pythonDataset, not the original one:

for row in pythonDataset:

1 Like

@Kevin.Herron Oh Thank you! I forgot I made that change from my original code. Unfortunately its still throwing the same error.

I believe the problem is I am trying to iterate a dataset and datasets are not iterable but I’m unsure how to get around this with the same result.

I think the issue here is that system.dataset.toDataset wants two lists: one of string and the other of strings/numbers. You are passing a list of string ( Header ) but for the other argument, you are passing a list of PyRowObject instead of strings/numbers.

If you want to solve your problem I think you have to get the value in every row and append it inside a list. Something like this:

NewData = []
for row in pythonDataset:
	
		if row[6]== "Sampler 1":
			for Value in row:
				val = pythonDataset.getValueAt(Value,6)
				NewData.append(val)
			
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT1").data = SamData

I did not test the code, but it should be something similar.
Hope this helps

1 Like

I don’t know what the original table contains, but you are also writing an entirely new dataset at every pass of the loop. This would give the net effect of having a single line with the latest value from the original table. You would actually need five different data lists. Also to expand on @c.bertulli’s mention, you would need to make a list out of the row:

sourceTable = event.source.parent.getComponent('Table')
dataOut = sourceTable.data
pythonDataset = system.dataset.toPyDataSet(dataOut)

header = ["col1", "col2", "col3", "col4", "col5", "col6", "Sampler", "col7", "col8"]

NewData1 = []
NewData2 = []
NewData3 = []
NewData4 = []
NewData5 = []

for row in dataOut:

	#convert row to list
	rowData = list(row)

	if row[6]== "Sampler 1":
		NewData1.append(rowData)
	elif row[6]== "Sampler 2":
		NewData2.append(rowData)
	elif row[6]== "Sampler 3":
		NewData3.append(rowData)
	elif row[6]== "Sampler 4":
		NewData4.append(rowData)
	elif row[6]== "Sampler 5":
		NewData5.append(rowData)

SamData1 = system.dataset.toDataSet(header, NewData1)
system.gui.getWindow("advance").rootContainer.getComponent("TableT1").data = SamData1

SamData2 = system.dataset.toDataSet(header, NewData2)
system.gui.getWindow("advance").rootContainer.getComponent("TableT2").data = SamData2

SamData3 = system.dataset.toDataSet(header, NewData3)
system.gui.getWindow("advance").rootContainer.getComponent("TableT3").data = SamData3

SamData4 = system.dataset.toDataSet(header, NewData4)
system.gui.getWindow("advance").rootContainer.getComponent("TableT4").data = SamData4

SamData5 = system.dataset.toDataSet(header, NewData5)
system.gui.getWindow("advance").rootContainer.getComponent("TableT5").data = SamData5

window = system.nav.openWindow('advance')
system.nav.centerWindow(window)
system.nav.closeParentWindow(event)

And, for those that like to make their code as compact as possible :smirk::

sourceTable = event.source.parent.getComponent('Table')
dataOut = sourceTable.data
pythonDataset = system.dataset.toPyDataSet(dataOut)

samplerList = ['Sampler 1','Sampler 2','Sampler 3','Sampler 4','Sampler 5']
tableList = ['TableT1','TableT2','TableT3','TableT4','TableT5']

header = ["col1", "col2", "col3", "col4", "col5", "col6", "Sampler", "col7", "col8"]
NewData = [[]] * 5  #create list of five empty lists

for row in dataOut:
	try:
		#convert row to list
		rowData = list(row)
		NewData1[samplerList(row[6])].append(rowData)
	except:
		print str(row[6]) + ' is not in samplerList'

for table, data in zip(tableList, NewData)
	system.gui.getWindow("advance").rootContainer.getComponent(table).data = system.dataset.toDataSet(header, data)

window = system.nav.openWindow('advance')
system.nav.centerWindow(window)
system.nav.closeParentWindow(event)
2 Likes