I'm having an odd issue with a script that I use to update the "last inspected" field in a feature class based on the related inspection records. It uses a search cursor to populate a dictionary with the most recent inspection date (values) for a given unique ID (keys). Then, it uses an update cursor, uses the unique ID field to look up the inspection date in the dictionary, and updates the inspection date if applicable. What I've noticed happening is that the script will overwrite the unique ids in chunks so that there are sets of duplicates. I'd love to say it was working perfectly before, but there was an index error that I recently fixed. It was only after I fixed that error and the script was able to run that I noticed this problem. I do have several other scripts that use the same template on other FCs that don't have this issue. Fortunately, I have archiving enabled and I was able to restore the proper ids.
If I disable the line that overwrites the row and have it print out the existing Unique ID and what the script wants to write in place, I get output like this:
(u'STV033632', (u'STV033586', datetime.datetime(2021, 3, 24, 4, 0)))
(u'STV033633', (u'STV033586', datetime.datetime(2021, 3, 24, 4, 0)))
(u'STV033634', (u'STV033586', datetime.datetime(2021, 3, 24, 4, 0)))
(u'STV033635', (u'STV033586', datetime.datetime(2021, 3, 24, 4, 0)))
(u'STV033636', (u'STV033586', datetime.datetime(2021, 3, 24, 4, 0)))
(u'STV033637', (u'STV033586', datetime.datetime(2021, 3, 24, 4, 0)))
(u'STV033638', (u'STV033586', datetime.datetime(2021, 3, 24, 4, 0)))
(u'STV033639', (u'STV033586', datetime.datetime(2021, 3, 24, 4, 0)))
(u'STV033641', (u'STV033640', datetime.datetime(2021, 1, 6, 5, 0)))
(u'STV033642', (u'STV033640', datetime.datetime(2021, 1, 6, 5, 0)))
(u'STV033643', (u'STV033640', datetime.datetime(2021, 1, 6, 5, 0)))
(u'STV033644', (u'STV033640', datetime.datetime(2021, 1, 6, 5, 0)))
(u'STV033645', (u'STV033640', datetime.datetime(2021, 1, 6, 5, 0)))
(u'STV033646', (u'STV033640', datetime.datetime(2021, 1, 6, 5, 0)))
Here's the meat of the script. I've excised the except statements and all of the edit session formalities for the sake of brevity. Is there something obvious I'm missing here?
#specify fields from target dataset to be updated
oldFields = ("WAM_ID","LastServiceDate")
#specify fields from source dataset
newFields = ("wam_id","date")
#create empty dictionary to store values
valDict = {}
#use search cursor to collect values from source dataset
newValues = arcpy.da.SearchCursor(gateInspections,newFields,"wam_id is not Null")
for row in newValues:
try:
key = row[0]
if key not in valDict:
valDict[key] = row[1:]
elif key in valDict:
if valDict[key][0] < row[1]:
valDict[key] = row[1:]
else:
pass
else:
pass
except:
logFile.write('No date for valve {0}.\n'.format(row[0]))
fcRows = arcpy.da.UpdateCursor(inFC,oldFields, "WAM_ID is not Null")
try:
for row in fcRows:
try:
if valDict.has_key(row[0]):
Vals = valDict[row[0]]
if Vals[0] is None:
operDate = row[1]
else:
operDate = Vals[0]
newRow = (row[0],operDate)
if tuple(row) != newRow:
fcRows.updateRow(newRow)
else:
pass