Sunday, June 7, 2015

GIS Programming: Error Handling and Debugging

Hello all,
     Last week we were concluding the big fundamentals of Python scripting. Yet this week feels like it could be even bigger still for really understanding and going in depth with Python. That is error handling and debugging. These are integral facets of scripting as errors will happen. Knowing how to find, troubleshoot, and potentially fix an error is key to successful scripting. Thankfully thats what this week was dedicated to. The overall objectives were to examine syntax errors and exceptions, implement debugging procedures available in PythonWin, and handle some exceptions. I was given 3 complete scripts of various functions, all with particular errors focused on one of the objectives above and then had to correct or handle the error accordingly.

The first scripts function was to print out a list of the field names in a specific shapefile. Its results can be seen below.

This script had a couple syntax errors, such as improper capitalization  or a set of dependant variables were reversed of each other etc.

The second script prints out a list of layers for a data frame in an example ArcMAP document.

This script had significantly more errors within it, beyond just syntax. There were variables missing definition, or improperly referenced, or added arguments where there shouldn't be.

The third script is likely the most interesting. This script has two parts, and the goal was to demonstrate the functionality of try/except statements within Python script. These scripts allow errors to be contained and let a script run despite having a segment flagged with an error. So the first part of the script contains an error, and the goal was to isolate it and allow the script to move on and run the second part.


You can see that Part A has an error that was flagged and described while running, but it successfully moved past it and provided the desired output for Part B.

Error handling and debugging can be time consuming but understanding it will pay dividends in the long run when working with these scripts. Reviewing syntax errors or employing the try except statement above are only the beginning of my understanding of how to best employ Python debugging. But it was a good start! 

No comments:

Post a Comment