Python version: 3.9.18
I am trying to identify drainage structures that failed two inspections in a row. To do so, I am making a copy of the related inspections table, then sort it by "NewGUID" (id field that ties inspections to parent drainage structures" and "date_" descending. I have tested this workflow manually, and am now looking to automate it, but I'm having trouble. Sorting by two fields is only available with the Advanced license, which I do not have.
After doing some research, I found what appears to be a similar situation here:
https://community.esri.com/t5/python-questions/increment-field-in-sorted-table/td-p/514221
I took the draft code provided by @RichardFairhurst and adapted it to my features and fields, and it now looks like this:
import arcpy
sourceFC = r"Z:\Projects\StormwaterInspectionsSelection\StormwaterInspectionsSelection.gdb\InspectionsCustomSorted20240130"
sourceFieldsList = ['NewGUID', 'date_', 'OBJECTID']
# Use list comprehension to build a dictionary from a da SearchCursor
valueDict = {tuple(r[0:]):0 for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList, sql_clause=(None, "ORDER BY NEWGuid, date_ DESC"))}
counter = 0
for item in sorted(valueDict.keys):
counter += 1
valueDict[item] = counter
sourceFieldsList.append("SortID")
with arcpy.da.UpdateCursor(sourceFC, sourceFieldsList, sql_clause=(None, "ORDER BY NEWGuid, date_ DESC")) as updateRows:
for updateRow in updateRows:
keyValue = tuple(updateRow[0:-1])
# verify that the keyValue is in the Dictionary
if keyValue in valueDict:
updateRow[-1] = valueDict[keyValue]
updateRows.updateRow(updateRow)
del valueDict
When I run that, I receive this error message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
In [38]:
Line 10: for item in sorted(valueDict.keys):
TypeError: 'builtin_function_or_method' object is not iterable
---------------------------------------------------------------------------
I searched online for that error and it seems that sorted is a built-in function and apparently can't be iterated. Is there a way around this?
Please let me know if there's any more information needed. Any help would be greatly appreciated!