Select to view content in your preferred language

trying to write a subroutine

598
2
10-26-2011 07:27 AM
AmyMeehan
Deactivated User
I'm trying to write a subroutine for the first time and I'm getting very confused.  I'm trying to create a tool where users would interactively add a point to a feature class (using a feature set and appending it to the original feature class).  The tool uses the interactive feature input control.  So far I've gotten that part to work and to append the new feature to the original feature class.  Now I'm trying to add in some functionality so that if the user forgets to enter a project id for the point, an error message will be printed and then a subroutine is called so they can then enter the project id. 

Here is my code:
# Import arcpy module
import arcpy
from arcpy import env

#Set workspace
#
env.workspace = "L:/Projects/HMAP/EnvironmentalReview.gdb"

# Script arguments
Feature_Set = arcpy.GetParameterAsText(0)
if Feature_Set == '#' or not Feature_Set:
    Feature_Set = "in_memory\\{C6359898-F410-49FF-96DD-0EB59C98516A}" # provide a default value if unspecified

# Local variables:
##ProjectPoints = Feature_Set
##ProjectPoints__2_ = "ProjectPoints"

#define subroutine if user forgot to enter a project identifier
#
def subroutine1():
    cur = arcpy.UpdateCursor(Feature_Set)
    for row in cur:
        newVal = arcpy.GetParameterAsText(1)
        idValue = row.setValue(newVal)
        row.updateRow(row)
    del cur, row
    return

#Check to make sure user entered a project identifier for point
#
cur = arcpy.SearchCursor(Feature_Set)
for row in cur:
    idValue = row.getValue("ProjectID")
    if idValue == None:
        arcpy.AddError("PLEASE ENTER A VALUE(S) FOR PROJECT ID")
        subroutine1
    else:
        arcpy.Append_management(Feature_Set, "ProjectPoints", "TEST", "", "")  # Process: Append


I know I'm doing something wrong as far as the subroutine is concerned.  When I run the tool, it allows me to click the mouse to add a point.  Then I hit "o.k" on the interactive input window to simulate someone forgetting to input the project id.  Then the rest of the script starts to run but I get an error stating that "the name Feature_Set is not defined". 

I've tried reading all kinds of documentation on defining functions but I apparently still don't quite get it.  Can anyone help?

Thanks much!

Amy Meehan
MDIFW
Bangor, Maine
Tags (2)
0 Kudos
2 Replies
PhilMorefield
Frequent Contributor
I'm trying to write a subroutine for the first time and I'm getting very confused.  I'm trying to create a tool where users would interactively add a point to a feature class (using a feature set and appending it to the original feature class).  The tool uses the interactive feature input control.  So far I've gotten that part to work and to append the new feature to the original feature class.  Now I'm trying to add in some functionality so that if the user forgets to enter a project id for the point, an error message will be printed and then a subroutine is called so they can then enter the project id. 

Here is my code:
# Import arcpy module
import arcpy
from arcpy import env

#Set workspace
#
env.workspace = "L:/Projects/HMAP/EnvironmentalReview.gdb"

# Script arguments
Feature_Set = arcpy.GetParameterAsText(0)
if Feature_Set == '#' or not Feature_Set:
    Feature_Set = "in_memory\\{C6359898-F410-49FF-96DD-0EB59C98516A}" # provide a default value if unspecified

# Local variables:
##ProjectPoints = Feature_Set
##ProjectPoints__2_ = "ProjectPoints"

#define subroutine if user forgot to enter a project identifier
#
def subroutine1():
    cur = arcpy.UpdateCursor(Feature_Set)
    for row in cur:
        newVal = arcpy.GetParameterAsText(1)
        idValue = row.setValue(newVal)
        row.updateRow(row)
    del cur, row
    return

#Check to make sure user entered a project identifier for point
#
cur = arcpy.SearchCursor(Feature_Set)
for row in cur:
    idValue = row.getValue("ProjectID")
    if idValue == None:
        arcpy.AddError("PLEASE ENTER A VALUE(S) FOR PROJECT ID")
        subroutine1
    else:
        arcpy.Append_management(Feature_Set, "ProjectPoints", "TEST", "", "")  # Process: Append


I know I'm doing something wrong as far as the subroutine is concerned.  When I run the tool, it allows me to click the mouse to add a point.  Then I hit "o.k" on the interactive input window to simulate someone forgetting to input the project id.  Then the rest of the script starts to run but I get an error stating that "the name Feature_Set is not defined". 

I've tried reading all kinds of documentation on defining functions but I apparently still don't quite get it.  Can anyone help?

Thanks much!

Amy Meehan
MDIFW
Bangor, Maine


Amy-

The subroutine (Python folks would just call this a 'function', which you have named 'subroutine1') looks okay to me. Except when you call a function, you need to use parentheses after it like this:
...
arcpy.AddError("PLEASE ENTER A VALUE(S) FOR PROJECT ID")
subroutine1()
...


To see what's happening, go into a Python shell and do this:

def my_function():
    print "yes"

# type this first and see what happens
my_function()

#type this next and see what happens
my_function
0 Kudos
AmyMeehan
Deactivated User
Thanks!!  That worked!!

Amy
0 Kudos