Select to view content in your preferred language

How do you write a function that includes code completion like arcpy functions?

706
2
Jump to solution
12-12-2012 08:04 AM
KennethPierce
Emerging Contributor
I have a few functions for sets of tasks I do routinely. I'd like to have the dropdown of all feature classes in my document pop-up in the python command-line window like when I use a built in arcpy function. Is there a way to set up the parameters to do that? Here's a sample function.

def classClip(myInput, myClip, outFeature, outTable):     arcpy.Clip_analysis(myInput, myClip, outFeature)     arcpy.AddField_management(outFeature, "ClipAcres", "Double", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")     arcpy.CalculateField_management(outFeature, "ClipAcres", "float(!SHAPE.AREA@ACRES!)", "PYTHON")     arcpy.Statistics_analysis(outFeature, outTable, [["ClipAcres", "SUM"]], "AAClass")
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
If you put the function into a python module "mystuff.py" and import it, then it will be available (with its documentation string, if any) from the python command window.

But why not make yourself a .pyt or .tbx toolbox and import it with arcpy.ImportToolbox? This will show both tool and parameter usage documentation while you're using it and allow you to access picklists for the parameters on the command line.

View solution in original post

0 Kudos
2 Replies
curtvprice
MVP Esteemed Contributor
If you put the function into a python module "mystuff.py" and import it, then it will be available (with its documentation string, if any) from the python command window.

But why not make yourself a .pyt or .tbx toolbox and import it with arcpy.ImportToolbox? This will show both tool and parameter usage documentation while you're using it and allow you to access picklists for the parameters on the command line.
0 Kudos
ChrisSnyder
Honored Contributor
Making a .tbx is probably the best, but I have used something like this in the past. I have a whole series of these little helper functions in a script. Some functions use arcpy some don't.

#to import a custom module from a network
import sys
sys.path.append(r"\\snarf\am\div_lm\ds\gis\tools\scripts_toolboxes")
import helper #script is helper.py that is in the scripts_toolboxes folder


def searchFiles(rootDir, wildcard = "*"):
    """Returns a list of files under rootDir that contain the wildcard search string"""
    fileList = []
    if os.path.exists(rootDir) == False:
        print "ERROR: " + rootDir + " does not exist!"
    else:
        for dirPath, dirNames, fileNames in os.walk(rootDir, topdown=True):
            for file in fileNames:
                if fnmatch.fnmatch(file, wildcard) == True:
                    fileList.append(dirPath + "\\" + file)
    return fileList
0 Kudos