jeudi 24 janvier 2019

Speeding up multiple ifs python

I have a script that needs to check for multiple conditions while looping through a cursor. It is very slow. Does anyone know if there's a different way to write this code so it performs better? There is a total of 15 ifs. I just didn't want to clutter the code here.

with arcpy.da.UpdateCursor(centerlines,("StreetName","OBJECTID","Shape_length")) as cursor:
    for row in cursor:
       if row[0]=="" or row[2]==None:
           row[0]="Unidentified Rd"
           cursor.updateRow(row)
       if row[0].startswith(" "): #find and fix leading spaces
           whereclause='"OBJECTID" = '+str(row[1])
           selected=arcpy.SelectLayerByAttribute_management ("centerlines", "NEW_SELECTION",whereclause)
           arcpy.Append_management (selected, "Spaces")
           row[0]=row[0].strip()
           cursor.updateRow(row)
       if row[0].endswith(" "): #find and fix trailing spaces
           whereclause='"OBJECTID" = '+str(row[1])
           selected=arcpy.SelectLayerByAttribute_management ("centerlines", "NEW_SELECTION",whereclause)
           arcpy.Append_management (selected, "Spaces")
           row[0]=row[0].strip()
           cursor.updateRow(row) 
      if "," in row[0]:
          whereclause='"OBJECTID" = '+str(row[1])
          selected=arcpy.SelectLayerByAttribute_management ("centerlines", "NEW_SELECTION",whereclause)
          arcpy.Append_management (selected, "Punctuation")
          row[0]=row[0].replace(",","")
          cursor.updateRow(row)

Aucun commentaire:

Enregistrer un commentaire