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