If-then-else using Calculate Value in a Geoprocessing Service

1692
2
07-22-2011 10:17 AM
HeatherLizama
New Contributor
Hello all,

Could you please help me find out why and how to fix a Geoprocessing model (v10)? The current model calculates census data and needs to have an if-then-else using the Calculate Value tool. The code below is working wonderfully on desktop, but crashes in a service with the following error:

Error 000539: Error running expression: x(r"c:\arcgisserver\arcgisjobs\Population_tools\model3_gpserver\jb18558850e7e47799a\scratch\scratch.gdb\Census_join") <type 'exceptions.ImportError'>: No module named collections
Failed to execute

Python code within Calculate Value in Geoprocessing Service:

# First Calculate Value should count the number of rows in the feature class and continue with
# the calculations if it is = 1 row.
Expression:
x(r"%Census_join%")

Code Block:
def x(InputFeatureClass):
import arcpy
result = arcpy.GetCount_management(InputFeatureClass)
if int(result.getOutput(0)) == 1:
return "true"
else:
return "false"

# Second Calculate Value should count the number of rows in the feature class and continue with
# the calculations if there is > 1 row.
Expression:
x(r"%Census_join%")

Code Block:
def x(InputFeatureClass):
import arcpy
result = arcpy.GetCount_management(InputFeatureClass)
if int(result.getOutput(0)) == 1:
return "false"
else:
return "true"

Data type: Boolean

Any help to make this work as a service...would be hugely helpful!
Thank you.
0 Kudos
2 Replies
curtvprice
MVP Esteemed Contributor
I don't know if this will help with your problem, but the strings "false" and "true" are both true:

>>> bool("true")
True
>>> bool("false")
True
>>>


You probably want to return python built-ins False or True:

return True
0 Kudos
HeatherLizama
New Contributor
Thank you Curtis for your reply, but changing to python built-ins False or True did not allow the GP service to run.

Any other ideas??
0 Kudos