help regarding different problems in python script

3441
5
Jump to solution
05-27-2015 11:57 AM
waqarahmad
New Contributor III
  • i am writing script which uses different Tools of arcgis and implementing different functionalities ..
  • My script consists in different Functions running in an order.
  • Each function is connected to each other , and works on layers and performs   functions like(update cursor, search cursor )
  • When function1 finishes execution funciton2 starts and works on the same layer on which first one was working.

The problem is here function2 generates wrong results or works wrong.

works wrong means if a loop is defined to 5 iterations it will runs more then 5 times. like 20, 30 times and generates different results of one value.

When i run each function separately it will works fine. but when i run function in one script it will create problems.

---------------------------

My findings of this are LOCKS which are creating the problem.

Sample Structure of my script:

import arcpy
import math
import sys  
import traceback  
from arcpy import env


# Class to Generate Report
class test:
    def __init__(self, workSpace):
      self.workSpace = workSpace
      
    # Add Fields
    def function1(self):
        # Script
                return;    
    # Generate Report
    def function2(self):
            # Script 
            rows = arcpy.SearchCursor(self.mapLayerFinal,"","","Shape_Area")
            row = rows.next()
            while row:
                    area = row.Shape_Area  
                    print "function2"      
                    destination_cursor = arcpy.UpdateCursor(self.censusLayer, '"DISTRICT_ID" = '+ str(district_id))
                    row1 = destination_cursor.next()
                    while row1:
                            row1.setValue("District_Area",area)
                            destination_cursor.updateRow(row1)
                                            
                            row1 = destination_cursor.next()
                                            
                    row = rows.next()
        return;    


t = test(workSpace)
t.function1()
t2 = test(workSpace)
t2.function2()

NOTE: code is tested and working correct if each function is executed separately

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Do not try to nest old-style cursors. They behave erratically, at best.

However, arcpy.da cursors nest properly.

View solution in original post

5 Replies
SepheFox
Frequent Contributor

I'm not clear on what you're asking. You say the code is tested and working correctly, so what is it that is not working correctly? You mention your theory that schema locks are causing an issue, but that is not consistent with the iteration errors you describe. I don't really see how we can help without seeing function 2.

0 Kudos
waqarahmad
New Contributor III

code is updated check please

0 Kudos
waqarahmad
New Contributor III

Corrected NOTE.

Working correctly if i run each function as a separate script....

adding function2 script

0 Kudos
DarrenWiens2
MVP Honored Contributor

Do not try to nest old-style cursors. They behave erratically, at best.

However, arcpy.da cursors nest properly.

DanPatterson_Retired
MVP Esteemed Contributor

Why are you using a class? Your functions look fine without being buried within a class.

I can't follow your indentation particularly with respect to your returns...and

Darren is absolutely correct about arcpy.da cursors