<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Passing python function into arcpy module for code maintenance in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/passing-python-function-into-arcpy-module-for-code/m-p/101666#M7880</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You could also set up variables for the parameters and then pass those into the tool:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;param1 = function1("some text")
param2 = function2("some other text", somevariable)
saveLoc = r"o:\some\savelocation\path"

arcpy.SomeTool(param1, param2, saveLoc)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is particulary nice if the variables are used in multiple locations and something changes. For example, if that saveLoc&amp;nbsp; 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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 06:17:15 GMT</pubDate>
    <dc:creator>MattSayler__Work_</dc:creator>
    <dc:date>2021-12-11T06:17:15Z</dc:date>
    <item>
      <title>Passing python function into arcpy module for code maintenance</title>
      <link>https://community.esri.com/t5/python-questions/passing-python-function-into-arcpy-module-for-code/m-p/101664#M7878</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&amp;nbsp; One problem I've ran into is when passing other python code into a function.&amp;nbsp; 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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I was curious if arcpy functions accept function names as parameters instead of the actual code.&amp;nbsp; Then I could refer to a separate function in my script instead of stuffing all my code into a single line.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is what an exported function looks like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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&amp;nbsp; if (oep35 == 1) or (core30 == 1):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 10000000\\n&amp;nbsp; elif ((oep35 == 0) and (core30 == 0) and (part139 == 1) and (type == \"AIRPORT\") and (use == \"Public\")):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 262144\\n&amp;nbsp; elif ((oep35 == 0) and (part139 == 1) and (type == \"AIRPORT\") and (use == \"Private\")):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 262144\\n&amp;nbsp; elif ((part139 == 1) and (oep35 == 0) and (type == \"AIRPORT\") and (use == \"Military\")):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 262144\\n&amp;nbsp; elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Public\") and (flights &amp;gt;= 250)):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 131072\\n&amp;nbsp; elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Military\") and (flights &amp;gt;= 100)):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 131072\\n&amp;nbsp; elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Public\") and (flights &amp;lt; 250) and (flights &amp;gt;= 100)) :\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 65536\\n&amp;nbsp; elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Private\") and (flights &amp;gt;= 100)):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 65536\\n&amp;nbsp; elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Military\") and (flights &amp;lt; 100)):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 65536\\n&amp;nbsp; elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Public\") and (flights &amp;lt; 100)):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 32768\\n&amp;nbsp; elif (((type == \"SEAPLANE BASE\") or (type == \"GLIDERPORT\") or (type == \"ULTRALIGHT\") or (type == \"BALLOONPORT\")) and ((use == \"Public\") or (use == \"Private\"))):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 32768\\n&amp;nbsp; elif ((part139 == 0) and (type == \"AIRPORT\") and (use == \"Private\") and (flights &amp;lt; 100)):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 32768\\n&amp;nbsp; elif ((type == \"HELIPORT\") and ((use == \"Public\") or (use == \"Military\"))):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 16384\\n&amp;nbsp; elif ((type == \"HELIPORT\") and (use == \"Private\")):\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 8192\\n&amp;nbsp; else:\\n&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Easy to maintain in Model Builder&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]19251[/ATTACH]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Was wondering if I could have something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;arcpy.CalculateField_management(ModelAirports_shp__12_, "Zoom", "calcZoom(!FlDaily!, !UseType!, !Part139!, !OEP35!, !Core30!, !Type!)", "PYTHON_9.3", calcZoom(!Airports!, !Use!)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and then define calcZoom in a different part of the script.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jay&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Nov 2012 12:14:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/passing-python-function-into-arcpy-module-for-code/m-p/101664#M7878</guid>
      <dc:creator>Jay_Gregory</dc:creator>
      <dc:date>2012-11-13T12:14:52Z</dc:date>
    </item>
    <item>
      <title>Re: Passing python function into arcpy module for code maintenance</title>
      <link>https://community.esri.com/t5/python-questions/passing-python-function-into-arcpy-module-for-code/m-p/101665#M7879</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can read this on how to pass variables to the tools.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A class="jive-link-external-small" href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004m000000" rel="nofollow" target="_blank"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004m000000&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Nov 2012 13:27:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/passing-python-function-into-arcpy-module-for-code/m-p/101665#M7879</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-11-13T13:27:22Z</dc:date>
    </item>
    <item>
      <title>Re: Passing python function into arcpy module for code maintenance</title>
      <link>https://community.esri.com/t5/python-questions/passing-python-function-into-arcpy-module-for-code/m-p/101666#M7880</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You could also set up variables for the parameters and then pass those into the tool:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;param1 = function1("some text")
param2 = function2("some other text", somevariable)
saveLoc = r"o:\some\savelocation\path"

arcpy.SomeTool(param1, param2, saveLoc)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is particulary nice if the variables are used in multiple locations and something changes. For example, if that saveLoc&amp;nbsp; 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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:17:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/passing-python-function-into-arcpy-module-for-code/m-p/101666#M7880</guid>
      <dc:creator>MattSayler__Work_</dc:creator>
      <dc:date>2021-12-11T06:17:15Z</dc:date>
    </item>
  </channel>
</rss>

