<?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: reusing a codeblock? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31862#M2514</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm not sure what you mean by "save it as a text file even inside a python script".&amp;nbsp; If I enclose everything in triple double quotes, then it won't execute, but it will still become a variable?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 23 Sep 2016 20:28:07 GMT</pubDate>
    <dc:creator>KeithD1</dc:creator>
    <dc:date>2016-09-23T20:28:07Z</dc:date>
    <item>
      <title>reusing a codeblock?</title>
      <link>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31859#M2511</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm using arcpy.CalculateField_management() with a large-ish codeblock. Is there a way to store the codeblock so it can be easily reused multiple times, and more importantly, easily edited? This is part of a larger script.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For instance (psuedo-code):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;codeblock = "&lt;/P&gt;&lt;P&gt;line 1 statements&lt;/P&gt;&lt;P&gt;line 2 statements&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;some indented statements&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # a comment&lt;/P&gt;&lt;P&gt;line&amp;nbsp;3 statements&amp;nbsp;"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;arcpy.CalculateField_management("1st_dataset_name","field_to_calculate","codeblock")&lt;/P&gt;&lt;P&gt;arcpy.CalculateField_management("2nd_dataset_name","field_to_calculate","codeblock")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What would be the proper way of doing this?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Sep 2016 19:55:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31859#M2511</guid>
      <dc:creator>KeithD1</dc:creator>
      <dc:date>2016-09-23T19:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: reusing a codeblock?</title>
      <link>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31860#M2512</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;just save it as a text file even inside a python script. &amp;nbsp;You can enclose it inside triple-single or triple double quotes so that it won't execute, but it willl maintain its python-esque shape and form.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="comment token"&gt;# my code blocks enclosed in triple-double quotes...&lt;/SPAN&gt;

&lt;SPAN class="string token"&gt;""&lt;/SPAN&gt;"

codeblock &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; "
line &lt;SPAN class="number token"&gt;1&lt;/SPAN&gt; statements
line &lt;SPAN class="number token"&gt;2&lt;/SPAN&gt; statements
&amp;nbsp;&amp;nbsp; some indented statements
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="comment token"&gt;# a comment&lt;/SPAN&gt;
line &lt;SPAN class="number token"&gt;3&lt;/SPAN&gt; statements "

&lt;SPAN class="string token"&gt;""&lt;/SPAN&gt;"&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:16:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31860#M2512</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-10T21:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: reusing a codeblock?</title>
      <link>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31861#M2513</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If your codeblock is stored in a variable named "codeblock", then access it using the &lt;EM&gt;variable&lt;/EM&gt; codeblock (no quotes - quotes means it's a &lt;EM&gt;string&lt;/EM&gt; that literally is "codeblock")&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CalculateField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"1st_dataset_name"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"field_to_calculate"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;codeblock&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Sep 2016 20:08:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31861#M2513</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2016-09-23T20:08:48Z</dc:date>
    </item>
    <item>
      <title>Re: reusing a codeblock?</title>
      <link>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31862#M2514</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm not sure what you mean by "save it as a text file even inside a python script".&amp;nbsp; If I enclose everything in triple double quotes, then it won't execute, but it will still become a variable?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Sep 2016 20:28:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31862#M2514</guid>
      <dc:creator>KeithD1</dc:creator>
      <dc:date>2016-09-23T20:28:07Z</dc:date>
    </item>
    <item>
      <title>Re: reusing a codeblock?</title>
      <link>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31863#M2515</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ok... if you can save your def as a script you can import the function from within the script and reuse the code block... I suspect this isn't the easy solution that you want short of getting into importing modules, namespace and the like. &amp;nbsp;but here goes.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&amp;gt;&amp;gt;&amp;gt; from angles_demo import _demo
&amp;gt;&amp;gt;&amp;gt; import inspect
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; codeblock = inspect.getsource(_demo)
&amp;gt;&amp;gt;&amp;gt; print(codeblock)
def _demo():
&amp;nbsp;&amp;nbsp;&amp;nbsp; """A sample run of compass returning compass notations for 20 deg
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; increments.&amp;nbsp; Change to suit
&amp;nbsp;&amp;nbsp;&amp;nbsp; """
&amp;nbsp;&amp;nbsp;&amp;nbsp; angles = np.arange(0, 360, 20) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in angles:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("{} =&amp;gt; {}".format(i, compass(i)))
&amp;nbsp;&amp;nbsp;&amp;nbsp; rose = c[np.digitize(angles, a)]
&amp;nbsp;&amp;nbsp;&amp;nbsp; comp_rose = list(zip(angles,rose))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("\nCompass rose examples\n{}".format(comp_rose))
&amp;nbsp;&amp;nbsp;&amp;nbsp; return comp_rose
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and codeblock is a string. &amp;nbsp;For you, the easiest thing would just to keep a script of your code blocks in the same folder as the script you are using... import the code block from the script and get the script. &amp;nbsp;Or better still, you could just copy and paste the 'stuff' into the code you are working on... whatever is easiest is the best... but I suspect you aren't creating packages as of yet that you need to maintain.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:16:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31863#M2515</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-10T21:16:12Z</dc:date>
    </item>
    <item>
      <title>Re: reusing a codeblock?</title>
      <link>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31864#M2516</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Dan, I appreciate the response. A little over my head at this point, but maybe for my next Python project I'll be able to incorporate what you described!&amp;nbsp; For now, I'll just redundantly copy-paste the codeblock where I need it to go.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Sep 2016 21:02:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31864#M2516</guid>
      <dc:creator>KeithD1</dc:creator>
      <dc:date>2016-09-23T21:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: reusing a codeblock?</title>
      <link>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31865#M2517</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;good luk Keith... this topic is on my blog list. &amp;nbsp;I use it all the time. &amp;nbsp;It is easy once you ge the hang of it.... but copy paste is just as good...as long as you can find it &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Sep 2016 21:31:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reusing-a-codeblock/m-p/31865#M2517</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-09-23T21:31:48Z</dc:date>
    </item>
  </channel>
</rss>

