I have created a Python script tool in ArcGIS Pro 1.4.1 which has tool validation.
The fourth parameter on my tool dialog, which is of type string with multiple values allowed, after choosing some values looks like this:
I would like this list of value to be sorted and contain only unique values.
I think the updateParameters
function should be able to achieve that. My updateParameters
function appears below.
The code has an if
statement to cater for the function being called before any values have been added. Then what I think it should be doing is:
- Read the values from the ValueTable object of parameter 3 into a string of values separated by semi-colons
- Create a list of those values by splitting on semi-colons
- Make a unique sorted list
- Join that list back into a string and load that string into the value table object.
At this point I would expect my five unordered values to have become four values sorted alphabetically.
My code gives no error, but when I change the value of a parameter, triggering the updateParameters
function, it leaves the list with the original five members still unordered.
def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
if self.params[3].value is not None:
valueString = self.params[3].value.exportToString()
# Sort and make values unique
valueList = valueString.split(";")
sortedList = sorted(set(valueList))
self.params[3].value.loadFromString(";".join(sortedList))
Can anyone see what I am missing or may be misunderstanding?
I have assumed that the string is being exported from and read by the value table using semi-colons but do not know where I can check that.
The answer by @AlexTereshenkov has all but solved this for me. The lingering issue which may or may not represent a new problem (that may need a new question) is an error that it seemed to bring in:
The value table appears to be updating perfectly but it is throwing ERROR 000800 and saying that one or both of the values in the picture are not in the choice list but they both are.
def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
if self.params[3].altered:
self.params[3].value = sorted(list(set(self.params[3].value.exportToString().split(";"))))
I suspect that I'm seeing the error when Alex is not because I have spaces in my strings when he does not. I'll experiment some more tomorrow.