Skip to content

Instantly share code, notes, and snippets.

@M-Bryant
Last active May 17, 2019 15:25
Show Gist options
  • Save M-Bryant/7147254 to your computer and use it in GitHub Desktop.
Save M-Bryant/7147254 to your computer and use it in GitHub Desktop.
arcpy: Sample Template
"""----------------------------------------------------------------------------
Name: $[ActiveDoc-Name]
Description:
Requirements: ArcGIS Desktop Standard (10.2)
Python Version: 2.7
Inputs:
Outputs:
Author: $[UserName]
Created: $[DateTime-'DD/MM/YYYY'-DateFormat]
Copyright: (c) $[UserName] $[DateTime-'YYYY'-DateFormat]
Licence: <licence here>
-------------------------------------------------------------------------------
"""
# import modules
import arcpy
def trace():
""" Trace finds the line, the filename and error message
and returns it to the user.
"""
import inspect
import traceback
import sys
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
# script name + line number
line = tbinfo.split(", ")[1]
filename = inspect.getfile(inspect.currentframe())
# Get Python syntax error
#
synerror = traceback.format_exc().splitlines()[-1]
return line, filename, synerror
def main(*argv):
"""TODO: Add documentation about this function here"""
try:
#TODO: Add code here
pass
except arcpy.ExecuteError:
line, filename, err = trace()
errMessage = ('Geoprocessing error on %s of %s :') % (line, filename)
print errMessage
arcpy.AddError(errMessage)
arcpy.AddError(arcpy.GetMessages(2))
except:
line, filename, err = trace()
errMessage = ('Python error on %s of %s : with error - %s') % (
line, filename, err)
print errMessage
arcpy.AddError(errMessage)
finally:
# Final cleanup goes here
pass
if __name__ == "__main__":
env.overwriteOutput = True
argv = tuple(arcpy.GetParameterAsText(i)
for i in xrange(arcpy.GetArgumentCount()))
main(*argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment