Select to view content in your preferred language

Update Python script to 10.0

1038
5
05-03-2013 05:39 AM
JulieMcKay
New Contributor II
I have a script that was written and worked very well in version 9.3.1. I have updated the script to work in version 10.0 but am getting an error:ERROR: Executing: CopyFeatures locations_Layer. However I also get the message "Step 5: New Civic Addresses Loaded Successfully)" before the error message. Is there something that I missed updating the script? Below is the portion of code where it is failing.



    # Process: Copy Features...
    arcpy.CopyFeatures_management("locations_Layer", Civic_Addresses, "", "0", "0", "0")    
    arcpy.AddMessage("Step 5: New Civic Addresses Loaded Successfully")

    # Update ST_TYPE_CD NULL fields with 999
    cur = arcpy.UpdateCursor(db + "\\Civic_Addresses","ST_TYPE_CD IS NULL")
    row = cur.next()
    
    while row:
        row.ST_TYPE_CD = 999
        cur.UpdateRow(row)
        row = cur.next()
    del row
    del cur
    print "Street Types with NULLS defined"



Thank you.
Tags (2)
0 Kudos
5 Replies
SamCoggins1
New Contributor III


    # Update ST_TYPE_CD NULL fields with 999
    cur = arcpy.UpdateCursor(db + "\\Civic_Addresses","ST_TYPE_CD IS NULL")
    row = cur.next()
    
    





I think this section of code should be:


    # Update ST_TYPE_CD NULL fields with 999
    cur = arcpy.UpdateCursor(db + os.sep + "Civic_Addresses","ST_TYPE_CD IS NULL")
    row = cur.next()
    


Remember to import the os module at the beginning of the script... Hope that works for you.
0 Kudos
JulieMcKay
New Contributor II
Thanks, but I am still getting the same error.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I have a script that was written and worked very well in version 9.3.1. I have updated the script to work in version 10.0 but am getting an error:ERROR: Executing: CopyFeatures locations_Layer. However I also get the message "Step 5: New Civic Addresses Loaded Successfully)" before the error message. Is there something that I missed updating the script? Below is the portion of code where it is failing.



    # Process: Copy Features...
    arcpy.CopyFeatures_management("locations_Layer", Civic_Addresses, "", "0", "0", "0")    
    arcpy.AddMessage("Step 5: New Civic Addresses Loaded Successfully")

    # Update ST_TYPE_CD NULL fields with 999
    cur = arcpy.UpdateCursor(db + "\\Civic_Addresses","ST_TYPE_CD IS NULL")
    row = cur.next()
    
    while row:
        row.ST_TYPE_CD = 999
        cur.UpdateRow(row)
        row = cur.next()
    del row
    del cur
    print "Street Types with NULLS defined"



Thank you.


What is the Civic_Addresses variable set to?  It may help to post all of the code.
0 Kudos
JulieMcKay
New Contributor II
All of the code is below. Thank you.

# Import system modules
import sys, string, os, datetime, calendar
import arcpy

# Get start time
startTime = str(datetime.datetime.now())
today = datetime.date.today()
month = today.strftime('%m')
print month

# Get previous month for old Civic Address feature class naming
prev_month = int(month) - 1
if prev_month == 0:
    prev_month = 12
print prev_month
prev_month_2 = int(month)-2
if prev_month_2 == 0:
    prev_month_2=12
mon2 = calendar.month_name[prev_month_2]
mon = calendar.month_name[prev_month]
print mon
print mon2

# Input File
inFolder = r"path\\updateCADB"
outLocation = r"Database Connections\un@test.sde"

# Local variables...
db =  outLocation
dl = inFolder
Civic_Addresses = db + "\\Civic_Addresses"

arcpy.AddMessage(startTime)
print inFolder
print outLocation
print db
print dl
try:
    # Process: Remove Domain From Field...
    if arcpy.Exists(db + "\\Civic_Addresses"):
        arcpy.RemoveDomainFromField_management(db + "\\Civic_Addresses", "COUNTY_CD", "")
        arcpy.RemoveDomainFromField_management(db + "\\Civic_Addresses", "PLN_CD", "")
        arcpy.RemoveDomainFromField_management(db + "\\Civic_Addresses", "COORDINATE_CLASS_CD", "")
        arcpy.RemoveDomainFromField_management(db + "\\Civic_Addresses", "COORDINATE_METHOD_CD", "")
        arcpy.RemoveDomainFromField_management(db + "\\Civic_Addresses", "COORDINATE_SOURCE_CD", "")
        arcpy.RemoveDomainFromField_management(db + "\\Civic_Addresses", "ST_TYPE_CD", "")
    arcpy.AddMessage("Step 1: Domains removed from fields")
    print "domains removed from fields"

    # Process: Delete Domain...
    desc = arcpy.Describe(db)
    print desc
    domains = desc.Domains
    print domains
    for domain in domains:
        print domain
        if (domain=="County"):
            arcpy.DeleteDomain_management(db, "County")
            print "County Domain Deleted"
        elif (domain=="Places"):
            arcpy.DeleteDomain_management(db, "Places")
            print "Places Domain Deleted"
        elif (domain=="Coord_Classes"):
            arcpy.DeleteDomain_management(db, "Coord_Classes")
            print "Coord Classes Domain Deleted"
        elif (domain=="Coord_Methods"):
            arcpy.DeleteDomain_management(db, "Coord_Methods")
            print "Coord Methods Domain Deleted"
        elif (domain=="Coord_Sources"):
            arcpy.DeleteDomain_management(db, "Coord_Sources")
            print "Coord Sources Domain Deleted"
        elif (domain=="Street_Dir"):
            arcpy.DeleteDomain_management(db, "Street_Dir")
            print "Street Dir Domain Deleted"
        elif (domain=="Street_Types"):
            arcpy.DeleteDomain_management(db, "Street_Types")
            print "Street Types Domain Deleted"
        elif (domain=="Unit_Types"):
            arcpy.DeleteDomain_management(db, "Unit_Types")
            print "Unit Types Domain Deleted"
    arcpy.AddMessage("Step 2: Domains deleted")
    print "table to domains starting"
    # Process: Table To Domain...
    arcpy.TableToDomain_management(inFolder + "\\counties.txt", "CD", "NAME",db, "County", "Individual County Names", "APPEND")
    arcpy.TableToDomain_management(inFolder + "\\place_names.txt", "CD", "NAME_E", db, "Places", "Place Names", "APPEND")
    arcpy.TableToDomain_management(inFolder + "\\coordinate_classes.txt", "CD", "NAME_E", db, "Coord_Classes", "Coordinate Classes", "APPEND")
    arcpy.TableToDomain_management(inFolder + "\\coordinate_methods.txt", "CD", "NAME_E", db, "Coord_Methods", "Coordinate Methods", "APPEND")
    arcpy.TableToDomain_management(inFolder + "\\coordinate_sources.txt", "CD", "NAME_E", db, "Coord_Sources", "Coordinate Sources", "APPEND")
    arcpy.TableToDomain_management(inFolder + "\\street_directions.txt", "CD", "NAME_E", db, "Street_Dir", "Street Directions", "APPEND")
    arcpy.TableToDomain_management(inFolder + "\\street_types.txt", "CD", "NAME", db, "Street_Types", "Street Types", "APPEND")
    arcpy.TableToDomain_management(inFolder + "\\unit_types.txt", "CD", "NAME_E", db, "Unit_Types", "Unit Types", "APPEND")
    print "Table to domains complete"
    arcpy.AddMessage("Step 3: New Domains Created")

    # Process: Feature Class to Feature Class...
    if arcpy.Exists(db + "\\Civic_Addresses_" + mon2):
        arcpy.Delete_management(db + "\\Civic_Addresses_" + mon2)
    if arcpy.Exists(db + "\\Civic_Addresses"):
        ## changed the next line
        arcpy.Rename_management(Civic_Addresses, "Civic_Addresses_" + mon)
    print "renaming of civic address feature class complete"
    arcpy.AddMessage("Step 4: Last Months Civic Addresses Backed Up")

    # Make XY Event Layer...
    arcpy.MakeXYEventLayer_management(inFolder + "\\locations.txt", "X_COORDINATE", "Y_COORDINATE", "locations_Layer", "PROJCS['NAD_1983_CSRS_New_Brunswick_Stereographic',GEOGCS['GCS_North_American_1983_CSRS',DATUM['D_North_American_1983_CSRS',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Double_Stereographic'],PARAMETER['False_Easting',2500000.0],PARAMETER['False_Northing',7500000.0],PARAMETER['Central_Meridian',-66.5],PARAMETER['Scale_Factor',0.999912],PARAMETER['Latitude_Of_Origin',46.5],UNIT['Meter',1.0]];-28216700 -23260100 10000;#;#;0.001;#;#;IsHighPrecision")
    print "make xy event layer completed"
    arcpy.AddMessage(" make XY event layer completed")


    # Process: Copy Features...
    arcpy.CopyFeatures_management("locations_Layer", db + "\\Civic_Addresses")
    print "Civic Addresses updated"
    arcpy.AddMessage("Step 5: New Civic Addresses Loaded Successfully")

    # Update ST_TYPE_CD NULL fields with 999
    print "Step 6 is getting started"
    cur = arcpy.UpdateCursor(db + "\\Civic_Addresses","ST_TYPE_CD IS NULL")
    row = cur.next()
         
    while row:
        row.ST_TYPE_CD = 999
        cur.UpdateRow(row)
        row = cur.next()
   
    del row
    del cur
    print "Street Types with NULLS defined"
    arcpy.AddMessage("Step 6: After del cur")
    # Process: Assign Domain To Field...
    arcpy.AssignDomainToField_management(Civic_Addresses, "COUNTY_CD", "County", "")
    arcpy.AssignDomainToField_management(Civic_Addresses, "PLN_CD", "Places", "")
    arcpy.AssignDomainToField_management(Civic_Addresses, "COORDINATE_CLASS_CD", "Coord_Classes", "")
    arcpy.AssignDomainToField_management(Civic_Addresses, "COORDINATE_METHOD_CD", "Coord_Methods", "")
    arcpy.AssignDomainToField_management(Civic_Addresses, "COORDINATE_SOURCE_CD", "Coord_Sources", "")
    arcpy.AssignDomainToField_management(Civic_Addresses, "ST_TYPE_CD", "Street_Types", "")
    ## Street Direction Codes are not assigned to any of the civic address records
    # arcpy.AssignDomainToField_management(Civic_Addresses, "STREET_DIR_CD", "Street_Dir", "")
    ## Unit Type Codes are not assigned to any of the civic address records
    #arcpy.AssignDomainToField_management(Civic_Addresses, "UNIT_TYPE_CD", "Unit_Types", "")

    arcpy.AddMessage("Step 6: Civic Address Update is Complete")
    print startTime
    endTime = str(datetime.datetime.now())
    print endTime
    ## email saying process succeeded

except:
    error = arcpy.GetMessages()
    arcpy.AddError(error)
    print "Process Failed"
    ## email saying process failed
0 Kudos
T__WayneWhitley
Frequent Contributor
Something I noticed about returning messages with GetMessages (which is what you are feeding AddError) is that you tend to get the last successfully ran gp feedback msg (if there are any)....which explains why you got the next print message that 'Step 5' is starting.  So look for the error just after that - is the query on your update cursor valid?  I'd check that your cursor 'is happy' with that 1st.

Hope that helps.

Enjoy,
Wayne


Minor note:
Although the next() method is supported at 10, you don't need it and you'll get an error (unless you trap it) if you try to fetch a row for applying a query returning no records - see this:
>>> db = r"Database Connections\MCPA.sde"

>>> # opening a cursor on the dataset, no query:
>>> cur = arcpy.UpdateCursor(db + "\\MCPA.DBO.PARCEL_PUBLIC")
>>> row = cur.next()

>>> # no problem with next() -- a row object was returned, so it makes sense to retrieve a field val:
>>> row.RECHAR
u'00078600-001100'

>>> # as a test, now applying the 'is null' query for a field I know there are no null vals:
>>> cur = arcpy.UpdateCursor(db + "\\MCPA.DBO.PARCEL_PUBLIC", "ID IS NULL")
>>> row = cur.next()

>>> # ah, the error is due to no row object (NoneType):
>>> row.RECHAR

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    row.RECHAR
AttributeError: 'NoneType' object has no attribute 'RECHAR'

>>> # in other words, this simple test reports there are no rows:
>>> if row:
               print 'okay, here we go'
else:
               print 'oops, no rows...'

oops, no rows...

>>> # to shorten your code and avoid the error, you can do something like this:
>>> cur = arcpy.UpdateCursor(db + "\\MCPA.DBO.PARCEL_PUBLIC", "ID IS NULL")
>>> for row in cur:
               print 'okay, here we go'
               
>>> 
0 Kudos