dictionary of all ArcPy toolbox commands

3387
9
04-25-2012 01:56 AM
HugoAhlenius
Occasional Contributor
Hi,
I frequently do my coding in the excellent text-editor Vim. It has a feature to do text-completion from a dictionary, which I would like to use - is there a plain list of all the toolbox commands + Spatial Analyst functions that one could use?

When I type in Vim I am often not sure about the capitalization etc of the commands.
Tags (2)
0 Kudos
9 Replies
RDHarles
Occasional Contributor
When you say "toolbox commands", are you talking about the exact name of the tool (with proper capitalization)?
0 Kudos
HugoAhlenius
Occasional Contributor
When you say "toolbox commands", are you talking about the exact name of the tool (with proper capitalization)?


Yes, I am primarily thinking of all the arcpy.something tools (not the arguments etc though). Maybe constants and classes would be useful as well.
0 Kudos
HugoAhlenius
Occasional Contributor
Hmm, I think I got it - in my iPython console, I just type arcpy. and then pressed <tab> - and I got it all! Another way is to do a dir(arcpy) and dir(arcpy.sa).
0 Kudos
RDHarles
Occasional Contributor
The tool ListTools will give them to you too...

for newToolName in arcpy.ListTools():
    print newToolName
0 Kudos
HugoAhlenius
Occasional Contributor
Now, how would I get all constants... ?
0 Kudos
HugoAhlenius
Occasional Contributor
...as well as functions and classes.

I guess one can always copy/paste them from the help file, if nothing else...
0 Kudos
RDHarles
Occasional Contributor
When Arc 10 was first released, I attempted to write a script to convert everything in a 9.x script to a 10.x script.
I never used it and I know there were some issues with it but you can use the ideas/lists/dictionaries from it:

#
# Updates Tools, Functions, Environments & a few Methods from 9.x to "case-sensitive" 10.x
#
# Note: Rewrites script(s) to hardcoded "ARC10" directory.
#
# Thoughts:
#   Can I get a list of all Methods & Properties?
#
# R.D. Harles
# 11-21-10
#

import arcpy, os, sys, string

# Output directory for updated scripts
if not os.path.exists("ARC10"):
    os.mkdir("ARC10")

# Create the master dictionary
masterDict={}

# Add tools to the masterDict (with and without the toolbox)
# Note: The "(" is required because some tools start with the same word as others (e.g. gp.select & gp.selectlayerbylocation)
for newToolName in arcpy.ListTools():
    # Without toolbox (e.g. clip)
    masterDict["gp."+newToolName.split("_")[0].lower()+"("] = "arcpy."+newToolName+"("  
    # With toolbox (e.g. clip_management)
    masterDict["gp."+newToolName.lower()+"("] = "arcpy."+newToolName+"("   

# Add environments to the masterDict
for env in arcpy.ListEnvironments():    
    masterDict["gp."+env.lower()] = "arcpy.env."+env
# These don't get listed for some reason?
masterDict["gp.overwriteoutput"] = "arcpy.env.overwriteOutput"
masterDict["gp.GetParameterAsText"] = "arcpy.env.GetParameterAsText"
    
# Add functions to the masterDict
functions = ["AddError","AddFieldDelimiters","AddIDMessage","AddMessage","AddReturnMessage","AddToolbox","AddWarning",
"AsShape","CheckExtension","CheckInExtension","CheckOutExtension","CheckProduct","ClearEnvironment",
"Command","CopyParameter","CreateObject","CreateRandomValueGenerator","CreateScratchName","CreateUniqueName",
"Describe","Exists","GetArgumentCount","GetIDMessage","GetInstallIInfo","GetMaxSeverity","GetMessage",
"GetMessageCount","GetMessages","GetParameter","GetParameterAsText","GetParameterCount","GetParameterInfo",
"GetParameterValue","GetReturnCode","GetSeverity","GetSeverityLevel","GetSystemEnvironment","ImportToolbox",
"InsertCursor","IsSynchronous","ListDatasets","ListEnvironments","ListFeatureClasses","ListFields","ListFiles",
"ListIndexes","ListInstallations","ListPrinterNames","ListRasters","ListTables","ListToolboxes","ListTools",
"ListVersions","ListWorkspaces","LoadSettings","NumpyArrayToRaster","ParseFieldName","ParseTableName",
"ProductInfo","RasterToNumPyArray","RefreshActiveView","RefreshCatalog","RefreshTOC","RemoveToolbox",
"ResetEnvironments","ResetProgressor","SaveSettings","SearchCursor","SetParameter","SetParameterAsText",
"SetProduct","SetProgressor","SetProgressorLabel","SetProgressorPosition","SetSeverityLevel","TestSchemaLock",
"UpdateCursor","Usage","ValidateFieldName","ValidateTableName"]
for func in functions:
    masterDict["gp."+func.lower()] = "arcpy."+func    

# Add cursor methods to masterDict
methods = ["next","reset","newRow","insertRow","updateRow","deleteRow","getValue","isNull","setNull","setValue","getPart","getObject","removeAll","partCount"]
for meth in methods:
    masterDict[meth.lower()] = meth

# This isn't going to work.  There could be up to 3 replaceable items on one line and only item item per line can be replace the way the script is written.
### Codes and return values
##codesReturnValues = ["ArcView","ArcEditor","ArcInfo","Engine","EngineGeoDB","ArcServer","AlreadyInitalized","Available","Unavailable","NotLicensed","Failed"]
##for crv in codesReturnValues:
##    masterDict[crv.lower()] = crv

# Add misc to masterDict
masterDict["gp = arcgisscripting.create()"] = ""
masterDict["gp = arcgisscripting.create(9.3)"] = ""
    
# Loop through python scripts
for script in os.listdir(''):
    if script.endswith('.py'):
        print "\nProcessing "+script+"..."

        # Open new file (output to hardcoded "ARC10" directory)
        wfile = open("ARC10/"+script, "w")

        count=0
        # Read existing script (line by line)
        for line in open(script).readlines():            
            # Make the line lower case to ensure match with lower case item
            lower = line.lower()
            count=count+1           
            
            # Loop through master dictionary
            for item in masterDict:
                # Get the key (2nd part of the pair)
                key = masterDict[item]                
                # Check for a match in the line
                x = lower.find(item)
                
                # Continue if there is a match
                if x != -1:                    
                    # Do the search & replace
                    fix = lower.replace(item, key)
                    # Write it (changed line)
                    wfile.write(fix)
                    print " "+script+": (line"+str(count)+") Replacing '"+item+"' with '"+key+"'"
                    break
            if x == -1:
                # Write it (unchanged line)
                wfile.write(line)
                print " "+script+": (line"+str(count)+") Writing original line: "+line 

        # Close file     
        wfile.close()       

        ## Replace arcgisscripting with arcpy        
        # Open file in read mode
        file = open("ARC10/"+script, "r")
        # Read entire file
        text = file.read()
        # Closes the read session
        file.close()        
        file = open("ARC10/"+script, "w")
        print "\n ...Replacing arcgisscripting with arcpy"
        file.write(text.replace("arcgisscripting", "arcpy"))
        file.close()        

print "\nDone.\n"
0 Kudos
HugoAhlenius
Occasional Contributor
ok cool, thanks - I have to look at that more closely at some point to see if I understand what your script does. I snooped around in the folder C:\ProgramX86\gis\ArcGIS\Desktop10.0\arcpy\arcpy, and I saw that one can grab a lot of things from those files as well.
0 Kudos
BenHolmes
New Contributor
Pyscripter is quite nice to use. It was mentioned in one of the geoprocessing blogs on here
http://code.google.com/p/pyscripter/
0 Kudos