Select to view content in your preferred language

script works randomly when used in a toolbox

917
4
04-28-2010 01:59 AM
RuiLopes
Emerging Contributor
Hello

I have a very simple script that tests the number of objects selected in a layer to use as a precondition for other processes in the model. The script has 3 parameters - number to test, minimum and maximum - and 1 if condition. It works fine when executed in python but it gives random results when used in a toolbox.

I'm using ArcGIS 9.2 in Windows XP Professional. I've attached script and toolbox (as txt files)

Any one else with similar problems?

Thanks in advance

Rui
0 Kudos
4 Replies
by Anonymous User
Not applicable
Original User: fperks

Don't use sys.exit, in the past its given random results. Instead you should raise an exception, then ArcGIS will note the error.

I modified your code block:

#test number
if myCountOfObjects < myMinimum:
    #gp.AddError("The input should be more than " + str(myMinimum) +" objects and is currently " + str(myCountOfObjects))
    #print "The input should be " + str(myMinimum) +" or more objects and is currently " + str(myCountOfObjects)
    raise RuntimeError("The input should be " + str(myMinimum) +" or more objects and is currently " + str(myCountOfObjects))
    #sys.exit(0)
elif myCountOfObjects > myMaximum:
    #gp.AddError("The input should be less than " + str(myMaximum) +" objects and is currently " + str(myCountOfObjects))
    #print "The input should be " + str(myMaximum) + " or less objects and is currently " + str(myCountOfObjects)
    raise RuntimeError("The input should be " + str(myMaximum) + " or less objects and is currently " + str(myCountOfObjects))
    #sys.exit(0)
else:
    gp.AddMessage("Processing " + str(myCountOfObjects) + " objects")
    print "Processing " + str(myCountOfObjects) + " objects"


This would give you the proper effect you are looking for.
0 Kudos
RuiLopes
Emerging Contributor
Thanks for the code Frank but it makes no difference in the result. For instance if you try inputing the values 23 / 1 / 5  the result is still wrong (when using the toolbox)

Any more suggestions?

Rui

Don't use sys.exit, in the past its given random results. Instead you should raise an exception, then ArcGIS will note the error.

I modified your code block:

#test number
if myCountOfObjects < myMinimum:
    #gp.AddError("The input should be more than " + str(myMinimum) +" objects and is currently " + str(myCountOfObjects))
    #print "The input should be " + str(myMinimum) +" or more objects and is currently " + str(myCountOfObjects)
    raise RuntimeError("The input should be " + str(myMinimum) +" or more objects and is currently " + str(myCountOfObjects))
    #sys.exit(0)
elif myCountOfObjects > myMaximum:
    #gp.AddError("The input should be less than " + str(myMaximum) +" objects and is currently " + str(myCountOfObjects))
    #print "The input should be " + str(myMaximum) + " or less objects and is currently " + str(myCountOfObjects)
    raise RuntimeError("The input should be " + str(myMaximum) + " or less objects and is currently " + str(myCountOfObjects))
    #sys.exit(0)
else:
    gp.AddMessage("Processing " + str(myCountOfObjects) + " objects")
    print "Processing " + str(myCountOfObjects) + " objects"


This would give you the proper effect you are looking for.
0 Kudos
by Anonymous User
Not applicable
Original User: fperks

Oops, i missed the big error. When ever you pass in arguments to a script, they are always as strings.
Lets say you pass in 1 / 23 / 5...
So right now your myCountOfObjects is actually the string "1"

Now saying the string "1" > integer 23? (It will be true, since "1" string converted gives you ascii value of 49)

Well you can't compare an string with an integer directly since it will give you not the correct behavouor, so you need to convert it to an integer. E.g.:

myCountOfObjects = int(gp.GetParameterAsText(0))
myMinimum = int(gp.GetParameterAsText(1))
myMaximum = int(gp.GetParameterAsText(2))
0 Kudos
RuiLopes
Emerging Contributor
Thanks a lot
0 Kudos