Select to view content in your preferred language

gp.calculatefield_management -- seemingly erratic behaviour

1067
4
04-20-2010 01:08 PM
justinperez
New Contributor II
I am using a few search cursors and deleting them as I go.  gp.calculatefield_management is behaving erratic from what I see.  The gp.calculatefield_management is working but is not hitting my print statements and then not hitting the code further downstream.

I first had the gp.calculatefield_management working against a shapefile that was clipped during the initial search cursor.  So the calculate method was being called against a newly created shapefile.  I thought that maybe the searchcursor didn't like the calculate method.  When I commented out the calculate calls everything worked fine.

Then I would make sure to clean up any objects and then it would work once.  Then nothing.  It wasn't hitting my print statements.  I use the gp.calculatefield_management in many other geoprocessing python scripts and never this behaviour.
0 Kudos
4 Replies
KimOllivier
Regular Contributor II
I think I can guess what is happening.
You will get into trouble deleting records while looping through the same set.
The only way to make this safe to delete records is to make a copy of the keys or the full set and use that in the loop.
0 Kudos
justinperez
New Contributor II
To start I will say I used the update cursor and some dicts to hold values to be used in the update cursor to solve the problem... Problem solved with different approach!

I would, however, like to understand what went wrong in this code below. 

I did a select by location on a polygon layer to clip a polyline layer.  I wanted to move attributes from the polygon layer to the subsequent clipped polyline layers.  I figured that a search cursor on the polygon layer for each row as I do the clip would let me call a calculatefield on the polyline clipped layers for each loop of the search cursor of the polygon layer.  It would work sometimes and sometimes not but would never hit my print statements (maybe once it did).  When I commented out the calculatefield calls and let the overall code run all my print statements would get hit.

My thought was that calling a calculatefield inside of a search cursor loop (even if the calculate field was not being done against the layer that the search cursor was working on but instead against the newly created clipped layer).  As I said, it would give me results sometimes and sometimes not.  Maybe this will help some else.

Here is the broken code...let me know if anyone wants to see the update cursor and dicts code that worked..

gp.makefeaturelayer(r"path\to\school.shp", "schl")
gp.selectlayerbylocation("schl", "intersect", r"path\to\layer to clip")

scur = gp.searchcursor("schl")
row1 = scur.reset()
row1 = scur.next()
while row1: 
    v = str(random.randint(1, 100000))
    name1 = row1.name
    print name1
    feat1 = row1.shape
    print feat1.area
    gp.clip_analysis(twline, feat1, wkschool + "\\" + v)
    gp.addfield_management(wkschool + "\\" + v + ".shp", "ISDName", "text", "#", "#", "100")
    gp.calculatefield_management(wkschool + "\\" + v + ".shp", "ISDName", "str(name1)" , "Python_9.3")
    row1 = scur.next()
del scur
0 Kudos
KentBurley
New Contributor
Hi,
Did you get this to work eventually?
If so, could you post the code? 

Thanks very much
Kent
0 Kudos
justinperez
New Contributor II
I think I once I applied sp1 for 9.3.1 it worked.
0 Kudos