Converting PyDataSet to DataSet

Hello, i am trying to implement some filters on a table component, and the way I am trying to do it is by having the data(to be filtered) on a view property and then binding this to the table data and using a script transform to actually do the filtering. However my script cannot even be compiled, and i cant seem to be able to figure out why:

def transform(self, value, quality, timestamp):
	"""
	Transform the incoming value and return a result.

	Arguments:
		self: A reference to the component this binding is configured on.
		value: The incoming value from the binding or the previous transform.
		quality: The quality code of the incoming value.
		timestamp: The timestamp of the incoming value as a java.util.Date
	"""
	
	data=system.dataset.toPyDataSet(value)


	header=[]
	for i in range(data.getColumnCount()):
		header.append(data.getColumnName(i))


	newData=[]
	for row in data:
		if (self.checkFilters(row)): #Custom method to apply the filters
			newData.append(row)
	
	ret=system.dataset.toDataSet(header,newData)
	return ret

I am getting a scriptEval error at ret=system.dataset.toDataSet(header,newData)
(I cant seem to be able to copy-paste the error.)
newData is a PyList object (389x325)
header is also a PyList object(325)
Any help would be greatly appreciated!

Without a little more information on the error, Iā€™m not sure how much we can help.

If getColumnNames() returns a list of the column names, then why build your own list with a for loop? This seems like unneeded code.

Sorry about that, that was uncessary code, getColumnNames() returns an UnmodifiableRandomAccessList which causes an error in the conversion again, as the 1st Argument cannot be coerced into a PySequence.

I took a screenshot of the error message:

Try changing line 13 to this:

newData.append(list(row))
1 Like

The type cast to list as @code_skin suggests may do the trick. Barring that, I would shy away from the conversion to a PyDataSet. I just feel like it adds complexity without much reward in most cases. Here is how I would refactor your script:

def transform(self, value, quality, timestamp):
   """
   Transform the incoming value and return a result.

   Arguments:
   	self: A reference to the component this binding is configured on.
   	value: The incoming value from the binding or the previous transform.
   	quality: The quality code of the incoming value.
   	timestamp: The timestamp of the incoming value as a java.util.Date
   """
   
   headers = system.dataset.getColumnHeaders(value)

   newData=[]
   for rowIndex in range(value.rowCount):
       row = [value.getValueAt(rowIndex,col) for col in range(value.columnCount)]
       if self.checkFilters(row): #Custom method to apply the filters
           newData.append(row)

   return system.dataset.toDataSet(headers,newData)

The type casting to list, worked! Thank you very much all for your help and suggestions.

1 Like