[GIS] ArcGIS Python Toolbox Parameter Filter.List ValueList Delete/Remove for Reconciling Versions

arcgis-10.2arcgis-desktoparcpypython-toolboxversioning

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.

Best Answer

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
Related Question