<?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 Create Custom Function in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220281#M16996</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Please bare with me. I'm very new to python and programming in general. I have a challenge for a class and just need help getting started. I have attached the streets.shp file. Any help would be greatly appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Challenge 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Create a custom function called countstringfields that determines the&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;number of fields of type string in an input feature class. Create this&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;function in a separate script ( for example, mycount.py ) that you call&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from another script ( for example, callingscript.py ). You can use the&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;streets.shp feature class in the Exercise12 folder.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 07 May 2014 01:54:55 GMT</pubDate>
    <dc:creator>RossRomero</dc:creator>
    <dc:date>2014-05-07T01:54:55Z</dc:date>
    <item>
      <title>Create Custom Function</title>
      <link>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220281#M16996</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Please bare with me. I'm very new to python and programming in general. I have a challenge for a class and just need help getting started. I have attached the streets.shp file. Any help would be greatly appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Challenge 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Create a custom function called countstringfields that determines the&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;number of fields of type string in an input feature class. Create this&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;function in a separate script ( for example, mycount.py ) that you call&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from another script ( for example, callingscript.py ). You can use the&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;streets.shp feature class in the Exercise12 folder.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 May 2014 01:54:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220281#M16996</guid>
      <dc:creator>RossRomero</dc:creator>
      <dc:date>2014-05-07T01:54:55Z</dc:date>
    </item>
    <item>
      <title>Re: Create Custom Function</title>
      <link>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220282#M16997</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;When you export a shapefile, there should have several other types of files(for example .cpg, .dbf,.prj...), without these files others cannot open your shapefile. You need to zip all these files in the zip file.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 May 2014 02:19:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220282#M16997</guid>
      <dc:creator>LiYao</dc:creator>
      <dc:date>2014-05-07T02:19:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create Custom Function</title>
      <link>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220283#M16998</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ahh sorry. Here it is.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 May 2014 15:08:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220283#M16998</guid>
      <dc:creator>RossRomero</dc:creator>
      <dc:date>2014-05-07T15:08:43Z</dc:date>
    </item>
    <item>
      <title>Re: Create Custom Function</title>
      <link>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220284#M16999</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;See here for getting string fields:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//018v00000012000000"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//018v00000012000000&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's a very simple way to call another script (there are many other ways):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;os.system("joinfiles.py")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;or if it's in another directory:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;os.system("c:/temp/joinfiles.py")&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 May 2014 16:50:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220284#M16999</guid>
      <dc:creator>RDHarles</dc:creator>
      <dc:date>2014-05-07T16:50:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create Custom Function</title>
      <link>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220285#M17000</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;&lt;BR /&gt;Here's a very simple way to call another script (there are many other ways):&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Or just import it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 May 2014 18:48:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220285#M17000</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2014-05-07T18:48:47Z</dc:date>
    </item>
    <item>
      <title>Re: Create Custom Function</title>
      <link>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220286#M17001</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you're looking to create a custom function that gives you field information, you will want to look at these two pages:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000012000000" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;BR /&gt;ListFields Method&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018z0000004n000000" rel="nofollow noopener noreferrer" target="_blank"&gt;FieldObject&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The list fields method will give you a list of all the fields in your Feature Class.&amp;nbsp; You can then loop through that list, getting information about each field using attributes of the field object.&amp;nbsp; If this case, you'd want the "type" attribute to see if that field is a string or not.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code Example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

def numberStringFieldsWithLayer(layer = None):

&amp;nbsp;&amp;nbsp;&amp;nbsp; # If no layer is passed in, stop the function
&amp;nbsp;&amp;nbsp;&amp;nbsp; if layer == None:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return None

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get the count for each field with type "String"
&amp;nbsp;&amp;nbsp;&amp;nbsp; n = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in arcpy.ListFields(layer):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if field.type == "String":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n += 1

&amp;nbsp;&amp;nbsp;&amp;nbsp; return n&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As far as calling this from another script, I think the best way to call your function would be to import that custom script.&amp;nbsp; What you're essentially doing is creating your our custom module.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Save the above code in a file called &lt;/SPAN&gt;&lt;STRONG&gt;customModule.py&lt;/STRONG&gt;&lt;SPAN&gt;.&amp;nbsp; Before you can import that module, you need to add that folder to the list of paths Python looks in to import things from.&amp;nbsp; Then you can import the module and call your custom function.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Add your custom module folder to the system paths
import sys
sys.path.append(r"Path\to\your\custom\module\folder")

# import your custom moudle
import customModule

# Make a layer object from your feature layer
streetsLayer = arcpy.mapping.Layer("streets")

# Call you custom function from the module
numberOfFieldsTypeString = customModule.numberStringFieldsWithLayer(streetsLayer)

# Remove your layer reference
del streetsLayer

# Print your results
print numberOfFieldsTypeString&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is pretty cool because it gives you a good introduction how to make code in other files to import to any project!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Matt&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:44:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-custom-function/m-p/220286#M17001</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2021-12-11T10:44:56Z</dc:date>
    </item>
  </channel>
</rss>

