Passing python function into arcpy module for code maintenance

699
2
Jump to solution
11-13-2012 04:14 AM
Jay_Gregory
Occasional Contributor III
To create some custom scripts, I first create what I can in Model Builder, then export to Python and add what custom code I need.  One problem I've ran into is when passing other python code into a function.  In Model Builder, it is easy to maintain the code block, but once exported to Python, any significant amount of code becomes difficult to read or document.
I was curious if arcpy functions accept function names as parameters instead of the actual code.  Then I could refer to a separate function in my script instead of stuffing all my code into a single line. 

This is what an exported function looks like:

arcpy.CalculateField_management(ModelAirports_shp__12_, "Zoom", "calcZoom(!FlDaily!, !UseType!, !Part139!, !OEP35!, !Core30!, !Type!)", "PYTHON_9.3", "def calcZoom(flights, use, part139, oep35, core30, type):\\n  if (oep35 == 1) or (core30 == 1):\\n    return 10000000\\n  elif ((oep35 == 0) and (core30 == 0) and (part139 == 1) and (type == \"AIRPORT\") and (use == \"Public\")):\\n    return 262144\\n  elif ((oep35 == 0) and (part139 == 1) and (type == \"AIRPORT\") and (use == \"Private\")):\\n    return 262144\\n  elif ((part139 == 1) and (oep35 == 0) and (type == \"AIRPORT\") and (use == \"Military\")):\\n    return 262144\\n  elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Public\") and (flights >= 250)):\\n    return 131072\\n  elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Military\") and (flights >= 100)):\\n    return 131072\\n  elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Public\") and (flights < 250) and (flights >= 100)) :\\n    return 65536\\n  elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Private\") and (flights >= 100)):\\n    return 65536\\n  elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Military\") and (flights < 100)):\\n    return 65536\\n  elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Public\") and (flights < 100)):\\n    return 32768\\n  elif (((type == \"SEAPLANE BASE\") or (type == \"GLIDERPORT\") or (type == \"ULTRALIGHT\") or (type == \"BALLOONPORT\")) and ((use == \"Public\") or (use == \"Private\"))):\\n    return 32768\\n  elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Private\") and (flights < 100)):\\n    return 32768\\n  elif ((type == \"HELIPORT\") and ((use == \"Public\") or (use == \"Military\"))):\\n    return 16384\\n  elif ((type == \"HELIPORT\") and (use == \"Private\")):\\n    return 8192\\n  else:\\n    return 0")


Easy to maintain in Model Builder
[ATTACH=CONFIG]19251[/ATTACH]

Was wondering if I could have something like this:
arcpy.CalculateField_management(ModelAirports_shp__12_, "Zoom", "calcZoom(!FlDaily!, !UseType!, !Part139!, !OEP35!, !Core30!, !Type!)", "PYTHON_9.3", calcZoom(!Airports!, !Use!)


and then define calcZoom in a different part of the script. 

Thanks!

Jay
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
0 Kudos
2 Replies
MathewCoyle
Frequent Contributor
You can read this on how to pass variables to the tools.

http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004m000000
0 Kudos
MattSayler__Work_
New Contributor III
As long as the function returns a value in the string format and/or datatype the tool is looking for for the parameter, it should work.

You could also set up variables for the parameters and then pass those into the tool:

param1 = function1("some text")
param2 = function2("some other text", somevariable)
saveLoc = r"o:\some\savelocation\path"

arcpy.SomeTool(param1, param2, saveLoc)


This is particulary nice if the variables are used in multiple locations and something changes. For example, if that saveLoc  variable is used in multiple tools and you decide to save to a different location. You only have to change the path once and you're good to go.
0 Kudos