2

Does anyone know if and how you can remove a value from a parameter filter list?

In my case, I am using a python toolbox in ArcGIS 10.2.2 and want a drop down list of all the versions for the person reconciling to choose from. But I do not want to include dbo.DEFAULT and DS.Draft (our QAQC version, child of Draft).

param1.filter.type = "ValueList"

param1.filter.list = [v.name for v in arcpy.da.ListVersions(r"\\gis01\DatabaseConnectionFiles\DataStewardsOnly\GIS_Vector_DS.sde")]

I tried using -=("dbo.DEFAULT", "DS.Draft") and treating it like a list, but am just stuck on how to do this, since I read elsewhere that these act like tuples, and not like lists. And not sure if this must be done in one line of code or can be altered or if I need to use UpdateParams, but seems like that is for updating based on user input.

I am very new to python and scripting.

5
  • Perhaps in the validate on the tool you could remove them, but I would think it would be far simpler to put in an exit clause if the wrong version is chosen like arcpy.AddError("You can't pick that version") followed by sys.exit(0). Commented Feb 24, 2015 at 0:50
  • 1
    @MichaelMiles-Stimson, sorry cannot agree on that. Why would one allow user to choose a version which will not be possible to use? To me, this is almost like showing food but prohibiting to eat it up :D it is much more user friendly to show only those versions that user can interact with. Including sys.exit(0) for that is a poor practice - sys.exit() raises a SystemExit exception which you are probably assuming as some error; choosing a "wrong" version is not an error. Commented Feb 24, 2015 at 7:00
  • I was fairly sure that sys.exit(0) doesn't raise an exception, the exit code 0 is 'normal', arcpy.AddError does raise an exception.. What I was trying to say is that if it was too hard to take them out of the list then there's a way to prevent the script from continuing on input that isn't appropriate @AlexTereshenkov. +1 for your answer, you've taught me something today. Commented Feb 24, 2015 at 21:34
  • 1
    @MichaelMiles-Stimson, no problem at all :) os._exit(1) provides a sledgehammer approach exiting without raising any exception. And your approach would be absolutely valid unless there were not any other way to filter the input. Another approach is to use updateMessages, and make a parameter invalid (red cross) parameters[0].setErrorMessage("Cannot have this version!") if user has selected the "wrong" one (resources.arcgis.com/en/help/main/10.1/index.html#/…). Commented Feb 25, 2015 at 7:27
  • I just posted a follow-up question about how to refresh this parameter in another post at gis.stackexchange.com/questions/137684/…
    – Vanessa
    Commented Mar 4, 2015 at 18:41

1 Answer 1

4

You are almost there. You just need to test in the list comprehension you get that the v.name is not equal to certain strings.

def getParameterInfo(self):
        """Define parameter definitions"""
        param0 = arcpy.Parameter(
            displayName="Input Features",
            name="in_features",
            datatype="GPString",
            parameterType="Required",
            direction="Input",
            multiValue=True)
        param0.filter.type = "ValueList"
        param0.filter.list = [v.name for v in arcpy.da.ListVersions(r"C:\ArcGIS\sde db.sde")
                              if v.name not in ["sde.DEFAULT","DBO.ProdQA"]]

        params = [param0]
        return params

enter image description here

EDIT: Alternate approach: if you want to let your users see all versions, but let them run the script only when having a correct version(s) selected, you might do:

def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        versionsDisabled = ("DBO.DraftQA","sde.DEFAULT")
        if any(v in str(parameters[0].value) for v in versionsDisabled):
            parameters[0].setErrorMessage("Cannot use the " + str(parameters[0].value))  
        return

When user will click away from the version parameter, a red cross will be shown. If she will try to click OK, an error message will appear: enter image description here

Read more about tool validation at Customizing tool behavior in a Python toolbox

If you do not want to run a tool if a user has chosen multiple valid versions, then:

def updateMessages(self, parameters):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    versionsDisabled = ("DBO.DraftQA","sde.DEFAULT")
    if any(v in str(parameters[0].value) for v in versionsDisabled):
        parameters[0].setErrorMessage("Cannot use the " + str(parameters[0].value))

    if ";" in str(parameters[0].value): #because the multivalue has ; between values
        parameters[0].setErrorMessage("Cannot use multiple versions")
    return
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.