ArcGIS Python Toolbox – Reordering Parameters and Referencing by Name

arcgis-proarcpyparameterspython-toolbox

Is there a way to reference parameters in an ArcGIS Pro Python Toolbox by name, instead of by numeric index / positional argument?

I built a data entry tool in a Python Toolbox that has over 100 parameters. If I need to add, remove, or reorder any of these, it becomes a massive undertaking to edit every entry in updateParameters(), updateMessages(), and execute().

Update: I've deleted my attempt, because it's bad, and only user2856's accepted answer should be on record.

Best Answer

Don't use a global. Just create a dictionary whenever you need to access parameters, e.g. in updateParameters, updateMessages, execute, etc:

parameters = {p.name: p for p in parameters}

e.g.

def updateParameters(self, parameters):
    parameters = {p.name: p for p in parameters}
    etc...

def updateMessages(self, parameters):
    parameters = {p.name: p for p in parameters}
    etc...

def execute(self, parameters, messages):
    parameters = {p.name: p for p in parameters}
    etc...

Another option is to create a namedtuple (could be a good ArcGIS Pro Idea) so you can access by index (backwards compatibility) as well as name.

from collections import namedtuple

etc...

    def execute(self, parameters, messages):  
        parameters = namedtuple('Parameters', (p.name for p in parameters))(*parameters)
        # do something with parameters[0] or parameters.some_name
        etc...

There's an ArcGIS Idea to change the parameters type from a list to a NamedTuple that you can support.

Related Question