What is Parameter.name used for in python toolbox

399
5
Jump to solution
12-06-2017 11:42 AM
LizEidsness
New Contributor III

According to the python toolbox documentation, a Parameter's name attribute is: 

The parameter name as shown in the tool's syntax in Python.

Right.  That doesn't mean much.  What is it used for?  Can I reference a parameter with it in code?

So some tool dialog's get burdened with parameters.  Are we really supposed to refer to all by index and remember the order?  When we move things around, renumber our code?

There's got to be a better way, but I'm still looking.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RichardDaniels
Regular Contributor

If a name is set for a parameter the name is available for interrogation. You could do this. 

paramName = parm0.name

You could write a function to search for a given name and return the parameter index. See http://pro.arcgis.com/en/pro-app/arcpy/classes/parameter.htm and http://pro.arcgis.com/en/pro-app/arcpy/functions/getparameter.htm for more information.

along the lines of:

def getParamByName(pName):

     iArgCount=arcpy.GetArgumentCount()

  # Note, a pIndex of -1 indicates parameter not found

  pIndex= -1

  iCount=0

    while (iCount<iArgCount):

            param=arcpy.GetParameter(iCount)

            if param.name==pName:

               pIndex=iCount

            iCount=iCount+1

     return pIndex;

View solution in original post

5 Replies
RichardDaniels
Regular Contributor

Within a python program you can do this to get the values from the Model Process that called it.

#Check to see if we have the expected number of parameters, in this case 2

if arcpy.GetParameterCount !=2:
  print "Script requires two arguments (table file name and a field name)."
  raise Exception

# Gets the value from the Model Process
theFileName= arcpy.getparameterastext(0)

theFieldName= arcpy.getparameterastext(1)

Some coders also use

theFileName= sys.argv[1].lower()

theFieldName=sys.argv[2].lower()

In this case sys.argv[0] would be the name of the 'program' itself.

Rich

0 Kudos
LizEidsness
New Contributor III

Right, but that's not what I mean.  In a Python Toolbox specifically, when a parameter is defined, there is a name assigned.  

param0 = arcpy.Parameter(
    displayName="Input Features",
    name="in_features",
    datatype="GPFeatureLayer",
    parameterType="Required",
    direction="Input")

What is the "name" used for? 

If I have a toolbox with say 20 things in it (Perhaps exessive, but it's just an example), if I add a parameter in the middle let's say param8, or move things around, param9 becoms param10, and 10-19 increment.  All my 

value_name = parameters[9].value  calls also have to change, and I have to keep track.  By index.

It would be useful to do something like parameters["in_features"] as in the above example.  But it's not possible, so what is "name" used for?

0 Kudos
RichardDaniels
Regular Contributor

If a name is set for a parameter the name is available for interrogation. You could do this. 

paramName = parm0.name

You could write a function to search for a given name and return the parameter index. See http://pro.arcgis.com/en/pro-app/arcpy/classes/parameter.htm and http://pro.arcgis.com/en/pro-app/arcpy/functions/getparameter.htm for more information.

along the lines of:

def getParamByName(pName):

     iArgCount=arcpy.GetArgumentCount()

  # Note, a pIndex of -1 indicates parameter not found

  pIndex= -1

  iCount=0

    while (iCount<iArgCount):

            param=arcpy.GetParameter(iCount)

            if param.name==pName:

               pIndex=iCount

            iCount=iCount+1

     return pIndex;

LizEidsness
New Contributor III

So essentially, it's just an attribute of the object.  If I want to make it useful, I have to wrap my own code around it.  

Thanks,

0 Kudos
RichardDaniels
Regular Contributor

most likely, but in some pure python code I've seen code similar to 

getParameter(param0.Name

you might try arcpy.getParameter("in_features"), were in_feature is a name of a parameter, and see if it works.

If it does let us know :)

0 Kudos