Python Script tool templates

3643
3
09-13-2011 02:09 PM
curtvprice
MVP Esteemed Contributor
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()
Tags (2)
3 Replies
MarcNakleh
New Contributor III
Hi there,

The files within the Python Standard Library have some pretty good examples of how to lay out code (especially code metadata) in a module. If not, I've always enjoyed this page's take on structuring code.

I try and set up my own code as follows:
#!/usr/bin/env python
###############################################################################
'''TITLE
   ------
   Description'''
###############################################################################

import os
import sys
import arcpy

__author__       = "Marc Nakleh"
__copyright__   = "TrakMaps 2011"
__version__      = "1.0"

#----------#
def main():
#----------#
    # Code goes here
    pass

##################
if __name__ == '__main__':
    main()


I have an unhealthy fixation on formatting, though!
I like your idea of a local set-up. I'd just mention that there are a lot of reasons to be running a module locally, testing (which would require the parameters you are querying the user for) being a good one.
PhilMorefield
Occasional Contributor III
Hi there,

The files within the Python Standard Library have some pretty good examples of how to lay out code (especially code metadata) in a module. If not, I've always enjoyed this page's take on structuring code.



Python actually has an official style guide, called PEP8 for brevity: http://www.python.org/dev/peps/pep-0008/. You can use the PEP8 Python module to check your code for adherence to this standard: http://pypi.python.org/pypi/pep8#downloads.

In my opinion, what is needed is an official ArcGIS Python style guide. Even among the ESRI produced script tools there is some variation and considerable deviation from PEP8. With Python becoming more and more integral to ArcGIS, I'd like to see promulgation of coding standards and best practices that fit within PEP8, to the extent possible.

Just my two cents.
MarcNakleh
New Contributor III
grrr! I forgot to mention PEP8, which is absolutely excellent advice!

Thanks for the reminder!