2nd arg can't be coerced to org.python.core.PySequence[] for addRows function

Thought this might be a good topic since I couldn’t find it on the forum.

Here’s what I’m trying to do since the notation is a little confusing

  1. delete a row/rows from tableT1 (lines 1-5) (this code works)
  2. take the deleted row/rows from the tableT1 and add them to the data on tableT2 which is on a different popup window (rest of code)(error)
table = event.source.parent.getComponent("TableT1")
data = table.data

selected = table.selectedRows

newData = system.dataset.deleteRows(data, selected)
table.data = newData

table2 = system.gui.getWindow("T2T3T1 Popups/T2 Popup").rootContainer.getComponent("TableT2")
data2 = table2.data

newData2 = system.dataset.addRows(data2,selected)
table2.data = newData2

system.gui.getWindow("T2T3T1 Popups/T2 Popup").rootContainer.getComponent("TableT2").data = newData2

There are two problems with the second half of the code

  1. If I don’t have the pop-up folder expanded in designer it throws TypeError: Window : T2T3T1 Popups/T2 Popup is not open. I can get past this by just opening up the folder.

2: For line 12 or TypeError: addRows(): 2nd arg can’t be coerced to org.python.cogsre.PySequence[]
I don’t know what coerced means, but the way i understand the addRows function works is addRows(dataset, int (default bottom), PySequence). both tables have same number of columns with the same headers

Much Thanks in advance

AddRows requires a sequence of sequences - a Python list of lists, where each sublist has as many elements as there are columns in the dataset (the types also have to match, but that’s another problem).

If you just need to add a single row, then use system.dataset.addRow, not addRows; or, pack your values into another sequence:
newData2 = system.dataset.addRows(data2, [selected])
It’s very unlikely this is going to do what you want/work at all, though - if you’re allowing multi-selection on the source table, then your selected variable will have an inconsistent length, which is not compatible with the way you’re passing things into the addRows function. If you have a single-column destination dataset, then you could “transpose” the sequence with a list comprehension:
newData2 = system.dataset.addRows(data2, [[row] for row in selected])

That comprehension will take an input long list:
[0, 1, 2, 3]
and transpose it into a 2-d sequence of single-element lists:

[
	[0],
	[1],
	[2],
	[3]
]

I have the table set to select Single Interval. I figured since the deleteRows worked of both single or interval selection addRows would function in a similar fashion.

You were correct that what I'm doing is not compatible with the way I'm passing things into the addRows function. When I inputted your suggesting lines of code it threw a different error on the same line.
IndexError: Number of values (1) in row 1 doesn't match number of columns (12) in dataset.

Your selected variable contains the row numbers. For addRows(), you need complete row contents. You need to extract them into a 2d list of lists before you delete from the original dataset.

1 Like

I'm a little confused isn't [[row] for row in selected] putting it into a 2d list of lists? Do I just need to reorder the code?

It’s putting row numbers into a 2d list. You need row contents. Something like this:

deletedRows = [[data.getValueAt(r, c) for c in range(data.columnCount)] for r in selected]
1 Like

table.selectedRows is a list of row indexes. Your ultimate goal is to copy a list of row contents into your destination dataset. You will need to create a data structure (a list of lists suitable for addRows is easiest) at the source before trying to paste them into the destination.

@pturmel’s last snippet does what you need - go through each selected row, then copy the contents of each column from the source dataset into a python list (and each of those rows is then put into another list)

The output of that comprehension will be directly able to pass to system.dataset.addRows()

1 Like

Ah Awesome I get it!! Can’t possibly thank you guys enough :pray:

2 Likes

I am trying the same thing but getting error as long object not iterable …

vTab=self.getSibling("Table1").props.data
	selectedData=self.getSibling("Table1").props.selection.data
	selectedRow=self.getSibling("Table1").props.selection.selectedRow
	downSelectedRow=self.getSibling("Table2").props.selection.selectedRow
	downTable=self.getSibling("Table2").props.data
       deletedRows = [[vTab.getValueAt(r, c) for c in range(vTab.columnCount)] for r in selectedRow]
#	for row in range(len(selectedData)):
		


	newData=system.dataset.addRows(downTable, downSelectedRow+1, deletedRows)

	self.getSibling("Table2").props.data=newData

	vTab = system.dataset.deleteRows(vTab, selectedRow)
	self.getSibling("Table1").props.data=vTab

Plz help

This is my initial code before editing as per the suggestions here…

	vTab=self.getSibling("Table1").props.data
	selectedData=self.getSibling("Table1").props.selection.data
        selectedRow=self.getSibling("Table1").props.selection.selectedRow
	newRow=[vTab.getValueAt(selectedRow,'Id'),vTab.getValueAt(selectedRow,'Description'),vTab.getValueAt(selectedRow,'IsPrimary') ]
	system.perspective.print(selectedData)
	downSelectedRow=self.getSibling("Table2").props.selection.selectedRow
	downTable=self.getSibling("Table2").props.data
	system.perspective.print("downTable"+str(downTable))
	for row in range(len(selectedData)):
		
		newRow=[selectedData[row].SrNo,selectedData[row].Id,selectedData[row].Description,selectedData[row].IsPrimary]
		system.perspective.print(newRow)

		newData=system.dataset.addRow(downTable, downSelectedRow+row, newRow)
		system.perspective.print("_____________")
		system.perspective.print(newData)

#		downSelectedRow=downSelectedRow+1
		system.perspective.print(downSelectedRow)
		self.getSibling("Table2").props.data=newData
		system.perspective.print("newData"+str(newData))
		system.perspective.print("new row added successfully")
		vTab = system.dataset.deleteRow(vTab, selectedRow)
		self.getSibling("Table1").props.data = vTab
		system.perspective.print("row deleted")
	

above code deletes multi selected rows but it is adding only one single row …plz help

selectedRow is a long.

In the OP, they were using the vision table components selectedRows property which returned a list of selected row numbers.

I don’t understand exactly what your are trying to accomplish with this code (other than to delete rows). How are you expecting to determine how many rows need to be deleted?

If you are wanting to take all of the rows from Table 1 and remove them from Table 2 that would be done as follows:

deletedRows = [[vTab.getValueAt(r,c) for ci in range(vTab.columnCount)] for r in range(vTab.rowCount)]

I want the selected rows to be deleted and deleted rows to be copied in table 2 but my above code deletes deleted rows but only one from that deleted rows(last selected row) is getted added/copied to table 2..plz help

I am trying to help, but I am struggling to understand what you are after.

Are you saying that the user selects a number of rows from Table 1, and then performs some action, maybe clicks a button, and then those selected rows should be removed from Table 1 and added to Table 2?

yes right…the below-shared code deletes selected rows in table 1 but from that selected rows only one row gets added to table 2 but I want all the selected rows to be added in table 2 …consider if I selected 3 rows from table 1 then after clicking on the button it deletes all that selected 3 rows but from that deleted rows only one row gets added to table 2

vTab=self.getSibling("Table1").props.data
	selectedData=self.getSibling("Table1").props.selection.data
        selectedRow=self.getSibling("Table1").props.selection.selectedRow
	newRow=[vTab.getValueAt(selectedRow,'Id'),vTab.getValueAt(selectedRow,'Description'),vTab.getValueAt(selectedRow,'IsPrimary') ]
	system.perspective.print(selectedData)
	downSelectedRow=self.getSibling("Table2").props.selection.selectedRow
	downTable=self.getSibling("Table2").props.data
	system.perspective.print("downTable"+str(downTable))
	for row in range(len(selectedData)):
		
		newRow=[selectedData[row].SrNo,selectedData[row].Id,selectedData[row].Description,selectedData[row].IsPrimary]
		system.perspective.print(newRow)

		newData=system.dataset.addRow(downTable, downSelectedRow+row, newRow)
		system.perspective.print("_____________")
		system.perspective.print(newData)

#		downSelectedRow=downSelectedRow+1
		system.perspective.print(downSelectedRow)
		self.getSibling("Table2").props.data=newData
		system.perspective.print("newData"+str(newData))
		system.perspective.print("new row added successfully")
		vTab = system.dataset.deleteRow(vTab, selectedRow)
		self.getSibling("Table1").props.data = vTab
		system.perspective.print("row deleted")

Assuming that you have the tables bound to a dataset, the following will work:

vTab = self.getSibling("Table1").props.data
selectedData = self.getSibling("Table1").props.selection.data
downTable = self.getSibling("Table2").props.data

dataToRemove = [[row[col] for col in row.keys()] for row in selectedData]
self.getSibling("Table1").props.data = system.dataset.deleteRows(vTab,dataToRemove)
self.getSibling("Table2").props.data = system.dataset.addRows(downTable,dataToRemove)