I'm having issues with what seems to be a straightforward task. Here's what I need:
System is setup with a pre-populated dataset tag
- User selects a column name from a dropdown (which is assembled based on the column names of the dataset tag)
- My script takes the selection and tries to remove it from the list of dataset column names
- Then the scrip applies the modified list of column names to the
system.dataset.filterColumns(dataset, columns)
function
- Finally the new dataset is written back to the tag
Here's where my issues starts. It appears that the column name list has some additional formatting ([u'Item 1'...u'Item N'
]) characters and the .remove() method does not work.
This is the piece of the code that's giving me trouble:
item_to_remove = 'Bob'
existing_dataset = system.tag.readBlocking([dataset_tag])[0].value
name_list = existing_dataset.getColumnNames()
name_list.remove(item_to_remove)
And this is the error message:
Error running action 'component.onActionPerformed' on Docks/_Common/Recipe Delete@D/root/rflexButtons/btnOk: Traceback (most recent call last): File "<function:runAction>", line 15, in runAction File "<function:runAction>", line 15, in runAction at java.base/java.util.Collections$UnmodifiableCollection.remove(Unknown Source) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) java.lang.UnsupportedOperationException: java.lang.UnsupportedOperationException
Any throughs and/or workaround suggestions?
Should that be,
item_to_remove = 'Bob'
existing_dataset = system.tag.readBlocking([dataset_tag])[0].value
name_list = system.dataset.getColumnHeaders(existing_dataset)
name_list = [item for item in name_list if item not in [item_to_remove ]]
I'm not a Python expert,
but the remove() function expects an index so to use that you would need to know what position 'Bob' was in first. It will also, as far as I know, error if 'Bob' doesn't show up.
I am incorrect. See Python List remove() Method.
1 Like
That's not your problem.
Tangent, explaining what's going on:
The u
artifacts you see when you print out a list's contents are a formatting artifact of the list itself - by default, Python prints the items inside the list using their __repr__
implementation. In Python 2, there are two fundamental types of strings - unicode and ASCII, and the u
prefix distinguishes them. It's indicating that to create an equivalent literal in your own code, you would have to prefix the string, just as it shows up in the debug output - but as long as the characters are the same, you can search for an ASCII string literal inside a unicode string just fine.
Your actual problem is this:
java.util.Collections$UnmodifiableCollection.remove(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
java.lang.UnsupportedOperationException: java.lang.UnsupportedOperationException
Jython wraps all Java list types in a list-like type, so you have the remove
function. But the list instance you are getting is an UnmodifiableCollection
, which does not allow you to remove elements - it's unmodifiable. Because you're dealing with a list of strings, you can get away with a "shallow copy" to a standard Python/Jython list:
item_to_remove = 'Bob'
existing_dataset = system.tag.readBlocking([dataset_tag])[0].value
name_list = list(existing_dataset.getColumnNames())
name_list.remove(item_to_remove)
And the rest of your code should work.
2 Likes