Why is this code producing an infinite loop

1483
9
Jump to solution
09-11-2014 11:17 AM
RichardFairhurst
MVP Honored Contributor

I have tried this code and it is producing an infinite loop that repeatedly prints "0 Records Selected".  I know that the query is resulting in 0 records being selected.  Why is the value returned from the GetCount_management function that reports as 0 considered be greater than or equal to 1?

import arcpy

import os

from time import strftime

updateFC = r"C:\Users\RFAIRHUR\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\Trans Connection to SQL Server.sde\GDB_TRANS.TRANS.TRANSPORTATION_MAINT\CENTERLINE"

print "Make Feature Layer: " + strftime("%Y-%m-%d %H:%M:%S")

arcpy.MakeFeatureLayer_management(updateFC, "CENTERLINE_Layer")

arcpy.SelectLayerByAttribute_management("CENTERLINE_Layer", "NEW_SELECTION", "OBJECTID = -1")

records = arcpy.GetCount_management("CENTERLINE_Layer")

while (records >= 1):

    print str(arcpy.GetCount_management("CENTERLINE_Layer")) + " Records Selected: " + strftime("%Y-%m-%d %H:%M:%S")

    arcpy.SelectLayerByAttribute_management("CENTERLINE_Layer", "NEW_SELECTION", "OBJECTID = -1")

    records = arcpy.GetCount_management("CENTERLINE_Layer")

print "Finished All Records: " + strftime("%Y-%m-%d %H:%M:%S")

This alteration to the code works without producing an infinite loop.

import arcpy

import os

from time import strftime

records = 0

while (records >= 1):

   print str(records) + " Records Selected: " + strftime("%Y-%m-%d %H:%M:%S")

##    print str(arcpy.GetCount_management("CENTERLINE_Layer")) + " Records Selected: " + strftime("%Y-%m-%d %H:%M:%S")

##    arcpy.SelectLayerByAttribute_management("CENTERLINE_Layer", "NEW_SELECTION", "OBJECTID = -1")

##    records = arcpy.GetCount_management("CENTERLINE_Layer")

print "Finished All Records: " + strftime("%Y-%m-%d %H:%M:%S")

0 Kudos
1 Solution

Accepted Solutions
AnthonyGiles
Frequent Contributor

arcpy.GetCount_management returns a Result object and you have to cast it as int to check against it:

int(arcpy.GetCount_management("CENTERLINE_Layer").getOutput(0))

View solution in original post

9 Replies
AnthonyGiles
Frequent Contributor

arcpy.GetCount_management returns a Result object and you have to cast it as int to check against it:

int(arcpy.GetCount_management("CENTERLINE_Layer").getOutput(0))

curtvprice
MVP Esteemed Contributor

Let me amplify a bit because this is an important thread, and a FAQ for arcpy users!

This was a change made with the geoprocessor at 9.3. Before then, tools always returned strings. Now, you get a result object out from tools. This result object can contain one or several results, usually as unicode strings. Most tools have only one result (say, a path or a count) so you can easily get the string representation if you need it using the str() function.

>>> arcpy.GetCount_management("centroid_points.shp")

<Result '231'>

>>> arcpy.GetCount_management("centroid_points.shp").getOutput(0)

u'231'

>>> str(arcpy.GetCount_management("centroid_points.shp"))

'231'

Here's a function that makes getting that count a little easier:

def GetCount(input):

    """GetCount in one step (like 9.2 GetCount tool - except returns int)"""

    return int(arcpy.GetCount(input).GetOutput(0))

AnthonyGiles
Frequent Contributor

Richard did this post not answer your question?

RichardFairhurst
MVP Honored Contributor

I switched the correct answer to yours, although I would say all of the answers I have seen are correct.  It only lets me mark one, but since yours included the code I can paste into my script and was posted first, I switched the answer to yours.  (I gave all of them Like points).

AnthonyGiles
Frequent Contributor

Thanks Richard

0 Kudos
curtvprice
MVP Esteemed Contributor

It only lets me mark one [as correct]

Richard Fairhurst‌ I realize this is is adding some meta to the thread -- but you can provide credit to others that add to the content of the thread by marking responses helpful and this will further up-rank the thread in searches. Just a thought!

RichardFairhurst
MVP Honored Contributor

The Mark as Helpful option is hidden in the Actions drop down menu.  That is even less obvious than the old forum's buttons on the right side of each post that all new users overlooked.  This option should be more obviously exposed if it is the new basis of MVP rankings.

Are Likes considered?  Likes are the only thing I see anyone do consistently here and the only thing I could easily see that I could apply to each post.

0 Kudos
ThomasTri
New Contributor II

GetCount returns a result object, not an integer count of the number of features selected.

see python - Cast arcpy result as an integer instead (arcpy.GetCount_management) - Geographic Informatio...

RichardFairhurst
MVP Honored Contributor

Thanks. I had read that many months ago, but forgotten.  I did consult the help for Get Count when I wrote the code, but obviously I only glanced through it and overlooked that it was returning an object and not a number.

0 Kudos