Create Custom Function

1835
5
05-06-2014 06:54 PM
RossRomero
New Contributor
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.

Challenge 1
Create a custom function called countstringfields that determines the
number of fields of type string in an input feature class. Create this
function in a separate script ( for example, mycount.py ) that you call
from another script ( for example, callingscript.py ). You can use the
streets.shp feature class in the Exercise12 folder.
Tags (2)
0 Kudos
5 Replies
LiYao
by
New Contributor III
Hi,
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.
0 Kudos
RossRomero
New Contributor
Ahh sorry. Here it is.
0 Kudos
RDHarles
Occasional Contributor
See here for getting string fields:
http://resources.arcgis.com/en/help/main/10.2/index.html#//018v00000012000000

Here's a very simple way to call another script (there are many other ways):
os.system("joinfiles.py")
or if it's in another directory:
os.system("c:/temp/joinfiles.py")
0 Kudos
Zeke
by
Regular Contributor III


Here's a very simple way to call another script (there are many other ways):


Or just import it.
0 Kudos
MattEiben
Occasional Contributor
If you're looking to create a custom function that gives you field information, you will want to look at these two pages:

ListFields Method

FieldObject

The list fields method will give you a list of all the fields in your Feature Class.  You can then loop through that list, getting information about each field using attributes of the field object.  If this case, you'd want the "type" attribute to see if that field is a string or not.

Code Example:

import arcpy

def numberStringFieldsWithLayer(layer = None):

    # If no layer is passed in, stop the function
    if layer == None:
        return None

    # Get the count for each field with type "String"
    n = 0
    for field in arcpy.ListFields(layer):
        if field.type == "String":
            n += 1

    return n


As far as calling this from another script, I think the best way to call your function would be to import that custom script.  What you're essentially doing is creating your our custom module.

Save the above code in a file called customModule.py.  Before you can import that module, you need to add that folder to the list of paths Python looks in to import things from.  Then you can import the module and call your custom function. 

Code example:

# 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


This is pretty cool because it gives you a good introduction how to make code in other files to import to any project!

Hope this helps,
Matt
0 Kudos