<?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: Best Way to Pass a Function into arcpy.CalculateField_management? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319293#M24818</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So, I posted this question after working on the solution for a couple days and as soon as I posted, I found a possible solution. Thought I'd share. I did a quick google search on how to print out a python function definition. It took to to this page:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://stackoverflow.com/questions/427453/how-can-i-get-the-source-code-of-a-python-function" title="http://stackoverflow.com/questions/427453/how-can-i-get-the-source-code-of-a-python-function" rel="nofollow noopener noreferrer" target="_blank"&gt;http://stackoverflow.com/questions/427453/how-can-i-get-the-source-code-of-a-python-function&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A few of the answers suggestion to use a module called 'inspect' which evaluates the contents of classes, methods, functions, etc. &lt;A href="https://docs.python.org/2/library/inspect.html" rel="nofollow noopener noreferrer" target="_blank"&gt;Python Inspect Help&lt;/A&gt;. One of the inspect functions allows you to find the source code of a function&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;import inspect&lt;/P&gt;&lt;P&gt;inspect.getsource(&lt;EM&gt;myfunction&lt;/EM&gt;)&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;so, I tried this (rememebering to pass a string (str) conversion of the inspection into the codeblock parameter):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.CalculateField_management(outClipFC, "PERCENTAGE_OF_OVERLAP_OF_LAYER", 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "getPercentOverlapValueOfAOI(!AREA_OF_OVERLAP_HA_OF_LAYER!, {0})".format(extentArea), 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "PYTHON", &lt;STRONG&gt;str(inspect.getsource(getPercentOverlapValueOfAOI&lt;/STRONG&gt;)))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And it works. Hopefully this is helpfully to others.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 15:10:39 GMT</pubDate>
    <dc:creator>MikeMacRae1</dc:creator>
    <dc:date>2021-12-11T15:10:39Z</dc:date>
    <item>
      <title>Best Way to Pass a Function into arcpy.CalculateField_management?</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319290#M24815</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a function that I use in a script in a couple different ways. One of the ways I would like to use it, is in the codeblock parameter of:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.CalculateField_management&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The function is here and basically does some math and returns a value:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def getPercentOverlapValueOfAOI(area_of_overlap, extentarea):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if round(area_of_overlap/extentarea * 100, 1) &amp;lt; 0.1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AOI_Percent_Value = str("&amp;lt;" +&amp;nbsp; "0.0")
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AOI_Percent_Value = str(round(area_of_overlap/extentarea * 100,1))
&amp;nbsp;&amp;nbsp;&amp;nbsp; return AOI_Percent_Value&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and then further down in my script, I use the calculate field function:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.CalculateField_management(outClipFC, "PERCENTAGE_OF_OVERLAP_OF_LAYER", 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "getPercentOverlapValueOfAOI(!AREA_OF_OVERLAP_HA_OF_LAYER!, {0})".format(extentArea), 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "PYTHON", """getPercentOverlapValueOfAOI""")&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Consistently, I am receiving the following error&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;ExecuteError: ERROR 000539: Runtime error &lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "&amp;lt;string&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;/P&gt;&lt;P&gt;NameError: name 'getPercentOverlapValueOfAOI' is not defined&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Now, I understand that in example #2 in the &lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00170000004m000000http://" rel="nofollow noopener noreferrer" target="_blank"&gt;help&lt;/A&gt;​ menu, They use a variable "codeblock" and wrap the function in triple quotes. I believe calculatefield evaluates the code block string and interprets it as a function. The issue I have with that, is that I have a function that I use inside and outsiide of the arcpy.CalculateField_management function. I initially declare it without the triple quotes becuase the native python interpter will obviously not undertand that the triple quoted string is, in fact, a python function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now, I've tried a number of things to get this to work. For example, I assign a variable to the triple quote wrapped function:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;codeblock = """def getPercentOverlapValueOfAOI(area_of_overlap):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if round(area_of_overlap/extentArea * 100, 1) &amp;lt; 0.1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AOI_Percent_Value = str("&amp;lt;" +&amp;nbsp; "0.0")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AOI_Percent_Value = str(round(area_of_overlap/extentArea * 100,1))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return AOI_Percent_Value"""&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and then pass the codeblock variable into the caluclate field function:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.CalculateField_management(outClipFC, "PERCENTAGE_OF_OVERLAP_OF_LAYER", 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "getPercentOverlapValueOfAOI(!AREA_OF_OVERLAP_HA_OF_LAYER!, {0})".format(extentArea), 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "PYTHON", codeblock)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now, that seems to work, but it completely defeats the purpose of creating a function. One of the primary purposes of a function is to make the code reusable. It seems like I have to define my variable twice. Once at the beginning of my script and then again so that I can get it wrapped in triple quotes and then pass it to a variable that in turn, gets passed into the caluclate field function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've also tried to pass the function name into the expression via .format with the same error popping up ( I think I was trying to get creative and silly with this one):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.CalculateField_management(outClipFC, "PERCENTAGE_OF_OVERLAP_OF_LAYER", 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "{0}(!AREA_OF_OVERLAP_HA_OF_LAYER!, {1})".format(getPercentOverlapValueOfAOI, extentArea), 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "PYTHON", """getPercentOverlapValueOfAOI""")&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My question is, what is the best way, to take a custom built function and pass it into the calculate field function as a piece of code block and then have the expression parameter recognize it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:10:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319290#M24815</guid>
      <dc:creator>MikeMacRae1</dc:creator>
      <dc:date>2021-12-11T15:10:34Z</dc:date>
    </item>
    <item>
      <title>Re: Best Way to Pass a Function into arcpy.CalculateField_management?</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319291#M24816</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'll give you the trick answer of "the best way to use your function is not to use the field calculator".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can get the same result by calling your function as it is from within an &lt;A href="http://resources.arcgis.com/en/help/main/10.1%20/index.html#/UpdateCursor/018w00000014000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;UpdateCursor&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There may be a way to format your function call within a formatted string in the field calculator expression, but it seems unnecessarily complicated (as you're finding).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;untested:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;with arcpy.da.UpdateCursor(outClipFC, [&lt;SPAN style="color: #0000ff; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;"PERCENTAGE_OF_OVERLAP_OF_LAYER","&lt;SPAN style="color: #0000ff; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #f6f6f6;"&gt;AREA_OF_OVERLAP_HA_OF_LAYER"&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #f6f6f6;"&gt;]) as cursor:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="background-color: #f6f6f6; color: #000000; font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif;"&gt;&amp;nbsp;&amp;nbsp; for row in cursor:&lt;/SPAN&gt;
&lt;SPAN style="background-color: #f6f6f6; color: #000000; font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[0] = &lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;getPercentOverlapValueOfAOI(row[1],extentArea)&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="background-color: #f6f6f6; color: #000000; font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;CODE&gt;&lt;SPAN class="n" style="font-size: 11.44px;"&gt;cursor&lt;/SPAN&gt;&lt;SPAN class="o" style="font-size: 11.44px;"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n" style="font-size: 11.44px;"&gt;updateRow&lt;/SPAN&gt;&lt;SPAN class="p" style="font-size: 11.44px;"&gt;(&lt;/SPAN&gt;&lt;SPAN class="n" style="font-size: 11.44px;"&gt;row&lt;/SPAN&gt;&lt;SPAN class="p" style="font-size: 11.44px;"&gt;)&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:10:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319291#M24816</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T15:10:36Z</dc:date>
    </item>
    <item>
      <title>Re: Best Way to Pass a Function into arcpy.CalculateField_management?</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319292#M24817</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey Darren, thanks for the tip. I did come across a few suggestions to use UpdateCursor. This exposes my stubborness. I'm convinced to find a way, only because by using UpdateCUrsor, it would mean I'd have to change a little bit of my other code. If this doesn't work out, so be it. I'll use the Cursor &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 03 Dec 2015 22:11:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319292#M24817</guid>
      <dc:creator>MikeMacRae1</dc:creator>
      <dc:date>2015-12-03T22:11:29Z</dc:date>
    </item>
    <item>
      <title>Re: Best Way to Pass a Function into arcpy.CalculateField_management?</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319293#M24818</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So, I posted this question after working on the solution for a couple days and as soon as I posted, I found a possible solution. Thought I'd share. I did a quick google search on how to print out a python function definition. It took to to this page:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://stackoverflow.com/questions/427453/how-can-i-get-the-source-code-of-a-python-function" title="http://stackoverflow.com/questions/427453/how-can-i-get-the-source-code-of-a-python-function" rel="nofollow noopener noreferrer" target="_blank"&gt;http://stackoverflow.com/questions/427453/how-can-i-get-the-source-code-of-a-python-function&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A few of the answers suggestion to use a module called 'inspect' which evaluates the contents of classes, methods, functions, etc. &lt;A href="https://docs.python.org/2/library/inspect.html" rel="nofollow noopener noreferrer" target="_blank"&gt;Python Inspect Help&lt;/A&gt;. One of the inspect functions allows you to find the source code of a function&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;import inspect&lt;/P&gt;&lt;P&gt;inspect.getsource(&lt;EM&gt;myfunction&lt;/EM&gt;)&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;so, I tried this (rememebering to pass a string (str) conversion of the inspection into the codeblock parameter):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.CalculateField_management(outClipFC, "PERCENTAGE_OF_OVERLAP_OF_LAYER", 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "getPercentOverlapValueOfAOI(!AREA_OF_OVERLAP_HA_OF_LAYER!, {0})".format(extentArea), 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "PYTHON", &lt;STRONG&gt;str(inspect.getsource(getPercentOverlapValueOfAOI&lt;/STRONG&gt;)))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And it works. Hopefully this is helpfully to others.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:10:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319293#M24818</guid>
      <dc:creator>MikeMacRae1</dc:creator>
      <dc:date>2021-12-11T15:10:39Z</dc:date>
    </item>
    <item>
      <title>Re: Best Way to Pass a Function into arcpy.CalculateField_management?</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319294#M24819</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can import a function from within the script itself.&amp;nbsp; This can be useful for timing purposes for example, where the timeit.timeit variables need to be strings as in your case with the field calculator.&amp;nbsp; I wonder if that might work in your case.&amp;nbsp; I can't try since I am on an iThingy&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import numpy as np
import timeit

def X_bench(num):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """make X coordinates as a benchmark for making all the other values"""
&amp;nbsp;&amp;nbsp;&amp;nbsp; Xs = np.arange(10,num+10)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return Xs

setup = "from __main__ import X_bench"
t = timeit.timeit("X_bench(5)",setup=setup,number=int(3))
print(t)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:10:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319294#M24819</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T15:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: Best Way to Pass a Function into arcpy.CalculateField_management?</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319295#M24820</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This thread was helpful in helping me get the field calculator to work with a function.&amp;nbsp; I am using an update cursor later in my script for something else, but in this case, the field calculator was a better option.&amp;nbsp; Thanks to some of the blog post by &lt;A href="https://community.esri.com/migrated-users/3116" target="_blank"&gt;Dan Patterson&lt;/A&gt;​ and some other threads, I was able to modify and simplify the results I needed from the my function...just need the orientation/angle of the line segment (it's a straight line).&amp;nbsp; I could never get the function call to work directly. even with the "import inspect", so I just calc'd it once manually, copied the snippet, then pulled the expression and code_block arguments out....just to make it cleaner...and was able to get it to work.&amp;nbsp; just sharing as another option.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;fishnetFC =&amp;nbsp; r"C:\Prep.gdb\FlatTrans"
ptFields = [["X1", "!SHAPE.firstPoint.X!"], ["Y1", "!SHAPE.firstPoint.Y!"], ["X2", "!SHAPE.lastPoint.X!"], ["Y2", "!SHAPE.lastPoint.Y!"], ["angle", "999" ]]

for field in ptFields:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("adding field {0}...".format(field[0]))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(fishnetFC, field[0], "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("&amp;nbsp; calcing field {0} to be {1}".format(field[0], field[1]))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(fishnetFC, field[0], field[1], "PYTHON_9.3" )
&amp;nbsp;&amp;nbsp;&amp;nbsp; if field[0] == "angle":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression="calcOrientation( !X1!, !Y1!, !X2!, !Y2!)"

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; code_block="def calcOrientation(x1, y1, x2, y2):&amp;nbsp; \n&amp;nbsp;&amp;nbsp;&amp;nbsp; radian = math.atan2((y2 - y1),(x2 - x1))&amp;nbsp; \n&amp;nbsp;&amp;nbsp;&amp;nbsp; angle = math.degrees(radian)&amp;nbsp; \n&amp;nbsp;&amp;nbsp;&amp;nbsp; return angle&amp;nbsp; \n"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("&amp;nbsp; recalculating {0} to correct value".format(field[0]))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(fishnetFC, field[0], expression, "PYTHON_9.3", code_block)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And just for clarity, my function in a more readable format:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def calcOrientation(x1, y1, x2, y2):&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; radian = math.atan2((y2 - y1),(x2 - x1))&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; angle = math.degrees(radian)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; return angle&amp;nbsp; 
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:10:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-pass-a-function-into-arcpy/m-p/319295#M24820</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2021-12-11T15:10:45Z</dc:date>
    </item>
  </channel>
</rss>

