Wednesday, June 5, 2024

Debugging and Error Handling - GIS Programming Module 3

 Hello everyone!

In this week's lab, we explored debugging and error handling in Python. I was shocked by how easy this came to me as a beginner, and I was able to look for the errors and implement the solutions easily. As a result, I was able to achieve the learning outcomes for the lab. The first objective we needed to reach was to demonstrate our ability to fix syntax errors and exceptions. We explored this within the scripts we needed to edit. The next outcome we were expected to learn was how to implement debugging procedures and modify scripts to run with exceptions. The third script we edited required us to add an exception to our code by using a try-except statement. Correctly using a try-except statement was one of the objectives we were expected to learn about. We also needed to know how to properly interpret script error messages, because if we don't know how to do that then how can we fix the errors we encounter. Finally, we need to be able to design a logical model of a script using a flowchart.

For the first script, we encountered two errors that needed to be fixed. The first error was a result of case sensitivity as a part of the script was properly capitalized. The next error was an issue with a for-in statement being flip-flopped. The variable had identified fields = arcpy.ListFields(FC), but the for-in statement said for fields in field. This wouldn't work because field had not been identified, so the correct statement would be for field in fields. The resulting code would print this:















The second script included server more errors, mainly in the variable section. The first error was the file path variable being wrong, so I had to ensure that the file path properly matched my project folder. The lab script originally had the file path as S:/GISProgramming/Module3/Results/Austin_TX.aprx when in actuality it is supposed to be S:\GISProgramming\Module3\Data\TravisCountyAustinTX.aprx. The next issue was a simple case-sensitive issue that had to be changed from upper case to lower case. After this issue, another issue that needed to be corrected was a file path using both forward slash and backslash. To fix it you need to be consistent with your slash choice so I went with a backslash. The last three errors were just spelling mistakes that missed a character or two that prevented the code from running properly. Once all of these errors were corrected the resulting code should look like this:

The third and final script required us to NOT fix the errors that occurred when running the script. The objective of this script was to add a try-except statement. I at first didn't read the instructions of the lab and fixed the errors but then realized I had done it wrong. It was pretty straightforward as I had to add the try: statement at the beginning but not before the import statement. After you type the try: statement you let the code for Part A run and once it reaches the end when the error occurs you add the except statement by typing it as Except TypeError:. TypeError is the error that occurred not allowing the code to run. Here is the steps taken in the flowchart on how the code is supposed to properly run:

Now that you know how the code is supposed to run, here is the expected results to know that the code ran properly: 


I really enjoyed this lab and I am very proud of how easily I can recognize the errors we faced. I am now more confident in my ability to be successful in this class and I can't wait to continue learning how to properly code in python.

Thanks for reading!







No comments:

Post a Comment

Final Project and other maps created

 After eight weeks this class has come to its conclusion. Our last task was to come up with a project idea that is geographic and meaningful...