AnsweredAssumed Answered

Working with Python Functions/Methods and ArcPy

Question asked by geoffreywestgis on Oct 24, 2016
Latest reply on Oct 24, 2016 by geoffreywestgis

I have this block of code that I would like repeated 21 times.  It creates a feature layer, executes summary statistics, joins a field, executes update cursor twice, for a line and polygon feature class.  Lastly, it calculates a field and deletes two fields.


Can somebody please explain to me the logic of the Python method/function?  How does the program know which variable should be called within an ArcPy function?  


The block of code below has within the first function what I would like executed 21 times.  However, when the SummaryStats(DistrictConfig) is executed, I do not receive my expected output.  What is being done incorrectly?  



import arcpy
import DistrictConfig
import logging
import logging.handlers
import ConfigParser
LOGGER_NAME = 'korterra_district'
arcpy.env.workspace = r"C:\KorTerraTables\tables_db.gdb"
arcpy.env.overwriteOutput = True
from datetime import datetime
startTime =
logger = logging.getLogger(LOGGER_NAME)
distfc = DistrictConfig.distfc
linefc = DistrictConfig.linefc
polyfc = DistrictConfig.polyfc
totalexpression = DistrictConfig.TotalExpression
totalscline = DistrictConfig.TotalSCLine
totalscpoly = DistrictConfig.TotalSCPoly
statsfield = DistrictConfig.StatsField
fields = DistrictConfig.fields
totalCalc = DistrictConfig.TotalCalc
unique_name = arcpy.CreateUniqueName("featureLyr")
transPoly = DistrictConfig.Overdue_Expression_SC_Tran
overDueLine = DistrictConfig.OverdueTableSC_TransLine
overDueExpression = DistrictConfig.Overdue_Expression
statusCaseFields = DistrictConfig.CaseFields
TotalLong ="TOTAL_LONG"
OverDueTotal = "OVERDUE"

def DistrictFunction(DistrictConfig, distfc, totalexpression, totalscline, statsfield, fields,TotalLong):
print "Calculating Total Line Tickets per District"
arcpy.MakeFeatureLayer_management(linefc, unique_name, totalexpression)
arcpy.Statistics_analysis(unique_name, totalscline , statsfield, "DISTRICT")
arcpy.JoinField_management(distfc, "NAME", totalscline, "DISTRICT", fields)
with arcpy.da.UpdateCursor(distfc, ["FREQUENCY"]) as cursor:
for row in cursor:
if row[0] == None:
row[0] = 0
#Calculate and Join Totals per District Polys
arcpy.MakeFeatureLayer_management(polyfc, unique_name, totalexpression)
arcpy.Statistics_analysis(unique_name, totalscpoly , statsfield, "DISTRICT")
arcpy.JoinField_management(distfc, "NAME", totalscpoly, "DISTRICT", fields)
with arcpy.da.UpdateCursor(distfc, ["FREQUENCY_1"]) as cursor:
for row in cursor:
if row[0] == None:
row[0] = 0
arcpy.CalculateField_management(distfc, TotalLong, totalCalc)
arcpy.DeleteField_management(distfc, ["FREQUENCY", "FREQUENCY_1"])

def SummaryStats(DistrictConfig):
## DistrictFunction(DistrictConfig, distfc, totalexpression, totalscline, statsfield, fields, TotalLong )
## DistrictFunction(DistrictConfig, distfc, totalexpression, totalscpoly, statsfield, fields, TotalLong)
DistrictFunction(DistrictConfig, distfc, overDueExpression, overDueLine, statsfield, fields, OverDueTotal)
except Exception as e:, exc_info=True)
raise e