curtvprice

Python Script tool templates

Discussion created by curtvprice Champion on Sep 13, 2011
Latest reply on Sep 22, 2011 by mnakleh
Many thanks to Jason Pardy for posting a helpful script tool template on the GP blog.

Someone in the comments suggested the approach used by the Statistics Toolbox mavens using a function at the top of the tool to put the arguments up to for easy access, see:

\Desktop10.0\ArcToolbox\Scripts\LocalMoran.py.

What's really neat about both Jason's and the stats team's approach is that you can either (a) call it as a script tool or from the windows cmd line, passing it arguments though arcpy (if __name__ == "__main__":\) or (b) import it and run it as LocalMoran.localI(args) from another script, bypassing the toolbox interface altogether (which in my experience can be darn slow, especially in arc 10 when you need to run arcpy.ImportToolbox() first.)

Any thoughts on this?

"""
Tool Name:  Cluster/Outlier Analysis (Anselin Local Morans I)
Source Name: LocalMoran.py
Version: ArcGIS 10.0
Author: ESRI
...
import arcpy as ARCPY
import ...
...
def setupLocalI():
    """Retrieves the parameters from the User Interface and executes the
    appropriate commands."""

    inputFC = ARCPY.GetParameterAsText(0)                    
    varName = ARCPY.GetParameterAsText(1).upper()              
...
            li = LocalI(inputFC, varName, outputFC, wType, 
                        weightsFile = weightsFile,
                        rowStandard = rowStandard)
...
def ...
def ...
class localI(object) # actually does the work
...
if __name__ == "__main__":
  setupLocalI()

Outcomes