Using an if-else construct

3105
3
06-28-2015 06:20 AM
JayDee
by
New Contributor

I would like this script to accept a user input (a recordset) - if the user input is an an address, then geocode the address and return a parcel id based on the address, which is what script 1 does. On the other hand, if the user input is an XY, make an event layer from the XY fields and also return a parcel id based on the XY which is what script 2 does. My question: How do I combine these 2 scripts using an if-else statement to do this in one fell swoop? I have attached the scripts and would appreciate any help.

Thanks

0 Kudos
3 Replies
WesMiller
Regular Contributor III

How do intend to use it? If you want to use in a model you could:

  1. Create a list with choice 1 "Address" choice 2 "XY"
  2. then test the list in your script

if listoption == "Address":

     run script

else:

     run other script

0 Kudos
DanPatterson_Retired
MVP Emeritus

To answer on a more general level, you can use

  • raw_input to get a user choice
  • use parameters on the command line to control flow
  • create a toolbox and a tool to control the flow

The first option is demonstrated below

def do_this(input):
    print("do this function: {}".format(input))
def do_that(input):
    print("do that function: {}".format(input))
import sys
if __name__ == '__main__':
    """enter a choice"""
    input = raw_input("input a or b")
    if input == 'a':
        do_this(input)
    elif  input == 'b':
        do_that(input)
    else:
        print "like it said...input a or b"

The second allows you to enter command line parameters depending upon your IDE, this could be a popup window with an arguments entry line (ie PythonWin) or a separate entry section for arguments (ie Pyscripter)

def do_this(input):
    print("do this function: {}".format(input))
def do_that(input):
    print("do that function: {}".format(input))
import sys
if __name__ == '__main__':
    """enter a choice at the command line"""  
    if len(sys.argv) > 1:
        input = sys.argv[1]
        if input == 'a':
            do_this(input)
        elif input == 'b':
            do_that(input)
    else:
        print "You have to put something in like...input a or b"

I won't get into the third option now.  And I have excluded imports of the two initial scripts, since it is a bit more difficult and requires you to organize your code in function blocks etc.

0 Kudos
FreddieGibson
Occasional Contributor III

If you want to use a model I'd suggest looking at the following page.

Using If-Then-Else logic for branching

http://desktop.arcgis.com/en/desktop/latest/analyze/modelbuilder/using-if-then-else-logic-for-branch...

If you're running the tool from a GUI or Console outside of the geoprocessing framework then you could follow Dan's suggestion to utilize the raw_input or input function in python to prompt the user. If you want to utilize your tool within the GP framework this would become problematic as it would violate the intended interaction experience for our GP framework (i.e. all inputs are specified prior to clicking OK and the tool is not paused to wait for user interaction).

If you wanted to leverage your tool within the GP Framework you'd want to expose your choice as a parameter within your tool and use a tool validator class to handle the logic of the user's selection. For example, below is an image of how this tool would look. Within the logic behind the tool I could listen for the user to change the first parameter, which determines what task they'll need to accomplish. Once the user selects a task I could disable the unrelated tasks so that they cannot specify values for these parameters and input empty strings to satisfy the required condition.

pyt.png

0 Kudos