AnsweredAssumed Answered

Writing better Python Code

Question asked by matthewrusso on Feb 11, 2015
Latest reply on Feb 12, 2015 by matthewrusso

Hey everyone,

 

Wanted to ask a generic but help question of the community.  Currently I have been learning Python on my own and have just completed what I call my first "real world use" script for my company.  It is a pretty basic script but am trying to get better.  This script could be used by multiple people who don't know anything about python.  I wanted to see for them, and myself if there are improvements that I could make to make this script more robust and "user friendly".  I am using all this as a learning process.

 

Any help, ideas, opinions, comments would be fantastic.

-Matt

 

import time, sys, platform, imp, arcpy
start = (time.strftime('%x %X')) # Start time

##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS #####

arcpy.env.workspace = 'insert path' # Location of output files
newInput = 'insert path' # Input File (New Year)
clipFeat = 'insert path' # Clip Feature
eraseFeat = 'insert path' # Previous years merge
x = '2014' # Change for current year
year = str(x) # Input file year (Change only the year)
outClip = 'r' + x + 'C' # Clip Output
outLayer = 'Query_Layer' # Layer output (Used to run the query)
outFc = 'Query_' + outClip # Shapefile output from the Layer file
eraseOut = 'r' + x + 'E' # Erase output

arcpy.env.overwriteOutput = 1 # Allows for output file overwriting
messageList = [] # Message list for the log file

log = 'insert path' # Creates a .txt doc with a log of the geoprocessing tools run (Edit path)

##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS #####

# Clip file to specified parameter
arcpy.Clip_analysis(newInput, clipFeat, outClip)
messageList.append(arcpy.GetMessages())

# Create a layer to add to Table of Contents
newLyr = arcpy.MakeFeatureLayer_management(outClip, outLayer)[0]
messageList.append(arcpy.GetMessages())

# Add year field and Calculate Field
arcpy.AddField_management(outClip, "Year", "TEXT", 0, "", 10, "","NULLABLE", "NON_REQUIRED","")
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, 'Year', year, "PYTHON_9.3", "")
messageList.append(arcpy.GetMessages())

# Add Fields and run USDA QUERY on the Layer
arcpy.AddField_management(outLayer, "BType", "TEXT", 0, "", 10, "","NULLABLE", "NON_REQUIRED","")
messageList.append(arcpy.GetMessages())
arcpy.AddField_management(outLayer, "MPB_Only", "TEXT", 0, "", 10, "","NULLABLE", "NON_REQUIRED","")
messageList.append(arcpy.GetMessages())
arcpy.AddField_management(outLayer, "Aspen_Dec", "TEXT", 0, "", 10, "","NULLABLE", "NON_REQUIRED","")
messageList.append(arcpy.GetMessages())   

# Query the data, based on the provided information from the USDA (Forest Service)                
# MPB        
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' "DCA1" = 80003 OR "DCA2" = 80003 OR "DCA3" = 80003 OR "DCA1" = 11006 OR "DCA2" = 11006 OR "DCA3" = 11006 ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "MPB_Only", 'str("MPB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# LP
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 11006 AND "HOST1" = 108) OR ("DCA2" = 11006 AND "HOST2" = 108) OR ("DCA3" = 11006 AND "HOST3" = 108) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("LP")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# PP
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 11006 AND "HOST1" = 122) OR ("DCA2" = 11006 AND "HOST2" = 122) OR ("DCA3" = 11006 AND "HOST3" = 122) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("PP")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# 5N
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 80003) OR ("DCA2" = 80003) OR ("DCA3" = 80003) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("SN")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# SB
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 11009) OR ("DCA2" = 11009) OR ("DCA3" = 11009) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("SB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# DFB
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 11007) OR ("DCA2" = 11007) OR ("DCA3" = 11007) ')    
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("DFB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# WBBB
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 80002) OR ("DCA2" = 80002) OR ("DCA3" = 80002) ')    
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("WBBB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# WSB
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 12040) OR ("DCA2" = 12040) OR ("DCA3" = 12040) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("WSB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# WPB
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 11002) OR ("DCA2" = 11002) OR ("DCA3" = 11002) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("WPB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# Aspen Decline
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 80001) OR ("DCA2" = 80001) OR ("DCA3" = 80001) OR ("DCA1" = 24032) OR ("DCA2" = 24032) OR ("DCA3" = 24032) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "Aspen_Dec", 'str("AD")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")

# Creates a Shape file from the layer
arcpy.CopyFeatures_management(outLayer, outFc)
messageList.append(arcpy.GetMessages())

# Erase
arcpy.Erase_analysis(outFc, eraseFeat, eraseOut)
messageList.append(arcpy.GetMessages())

# Writes information to log file
f = open(log, 'w') # Log file location
f.write('Created by: Matt Russo' + '\n') 
f.write('Email: matthewrusso1986@gmail.com' + '\n')
f.write('Architecture : ' + platform.architecture()[0] + '\n')
f.write('Python EXE : ' + sys.executable + '\n')
f.write('Path to arcpy : ' + imp.find_module('arcpy')[1] + '\n' +'\n')
f.write('Start time was: ' + start + '\n')
end = (time.strftime('%x %X')) # End time
f.write('End time was: ' + end + '\n' + '\n')
for message in messageList:
    f.write(message)
    f.write('\n' + '\n')
f.close()

Outcomes