I would like to understand how to loop the following set of data access search and update routines.
How do I take the chunk of code below and just loop through it?
I need to iterate through the "Buffer Recipient Claims" feature class and :
- Select the first row in the table if the field "Allocation Credit Value" (AllocFrcstdCreVal) is greater than 15000
- Subtract the "Allocation Credit Value" from the "Recipient Forecasted Credit Value" (RecFrcstdCredVal) for that row.
- Identify this record as having had a credit allocation.
- Move on to the next row in the "Buffer Recipient Claims" feature class and repeat the process.
Straightforward enough so long as we have one Allocating Claim Number for one Recipient Claim Number.
The trick is that there are several Allocation Claim Numbers (which provide the Allocation Credit Value) for one Recipient Claim number. This is important because as I apply the credits from the Allocation Claim its value drops by the amount required by the Recipient Claim. If that Allocation Claim credit value drops below 15000 I need to be able to select the next available Allocating Claim in the table. I have already sorted the Allocating Claims in descending order of Credit value so that the next availble Allocating Claim value is always the highest avaialable (and I want to run the Allocating Claims down as close to 15000 before having to switch.).
As it stands now the code does pretty much what I want it to do albeit just on the first records selected by Recipient Claim Number.
I'm guessing I may need to create a "def" module or set a count to step through the records (neither of which I have yet done).
# ======================== # immediate mode # prepare basic settings/parameters import arcpy from arcpy import env env.workspace = "CURRENT" # ===================================== # this works on one row and stops. It needs to loop. # these are the field values used for the two data sets. # "RecipientBufferClaims" fields: s_fieldList = ["RecClmNum","RecFrcstdCredVal","CorrRecCreVal","AllocClmNum","AllocFrcstdCreVal","CorrAllocCreVal","Sequence","AllocationStatus"] # "TableAllocationClaims" fields: u_field_list = ["AllocClmNum","CorrAllocCreVal","AllocFrcstdCreVal"] # Search the first row of the "RecipientBufferClaims" FC for the required values # and put those values into memory. Order the "RecipientBufferClaims" FC by their sequence number (sequence field). # note : oddly enough I had to use "DESC" for the table to sort from row 1 upwadrds. with arcpy.da.SearchCursor("RecipientBufferClaims",s_fieldList,sql_clause= (None,"ORDER BY Sequence DESC")) as srows: for srow in srows: # I think this is the way to place these fields in memory as variables assigned to the correlating "srow"s. V_RecClmNum = srow V_RecCreVal = srow V_CorrRecipientCreVal = srow V_AllocatingClmNum = srow V_AllocatingCreVal = srow V_CorrAllocCreVal = srow V_Sequence = srow V_AllocationStat = srow # set the "where clause" to search in the "TableAllocationClaims" LUT for the right Allocating Claim number. whereClause = ' "AllocClmNum" ' + " = '" + str(V_AllocatingClmNum) + " ' " # set the "where clause" to search in the "TRecipientBufferClaims" feature class for the right Recipient Claim number. whereClause2 = ' "RecClmNum" ' + " = '" + str(V_RecClmNum) + " ' " # Search through the "TableAllocationClaims" as per the "whereClause" and if the value is > than 1500 # subtract the value from the Recipient Credit field from the Allocating Credit field. # Use the RecipientCreditValue returned from the search routine above. with arcpy.da.SearchCursor("TableAllocationClaims",u_field_list, whereClause) as s2rows: for s2row in s2rows: V2_CorrAllocClm = s2row V2_CorrAllocCreVal = s2row V2_FrcstdCreVal = s2row if V2_CorrAllocCreVal >= 15000: with arcpy.da.UpdateCursor("TableAllocationClaims",u_field_list,whereClause) as u3rows: for u3row in u3rows: u3row = V2_CorrAllocCreVal - V_CorrRecipientCreVal u3rows.updateRow(u3row) # updates the CorrAllocCreVal field in "RecipientBufferClaims" with the # value returned above filtered as per the "whereClause". with arcpy.da.UpdateCursor("RecipientBufferClaims",s_fieldList,whereClause) as u4rows: for u4row in u4rows: u4row = u3row u4rows.updateRow(u4row) # marks how the credit allocation was distributed. with arcpy.da.UpdateCursor("RecipientBufferClaims",s_fieldList,whereClause2) as u5rows: for u5row in u5rows: if u5row == V_AllocatingClmNum: u5row = 0 u5row = "R_" + str(V_RecClmNum) + "_A_" + str(V_AllocatingClmNum) + "_Allocated" u5rows.updateRow(u5row)