Bugs! This week in GISProgramming we worked through scripting errors and debugging in Python. After this introduction I think decided I prefer syntax errors to exceptions and logic errors because they seem easier to fix, or at least the computer is more likely to catch them as an error.
For the lab I tackled errors in three scripts supplied by our professor. In the first script the fixes were simple, just a couple of changes due to spelling and capitalization. The Check Tool in PythonWin did a lot of the work here. The output of the script is written in blue in the graphic below and the error that prevented the script from running is in red at the top.
|
Script 1 Output |
There were more errors, 8 total, in the second script and they were a little more difficult to identify. The errors this time around included spelling issues, incorrect symbols, incorrect file paths, and missing/incorrect arguments for methods, I used the Check Tool again but it did not identify all of the errors so I also tried the Debugging Tool on this script. The debugging tool helped but one thing I learned this week is that it is hard to recognize an error for a method or function you know nothing about. How do you know to correct an argument if you do not know the requirements for a particular method or function? That is why I am a big fan of the Help Tool and the Python Manuals, they make weird codes make sense. Once everything was corrected and I ran the script, I got the results in the graphic below. Also make note of the last error I received- if the script is trying to save something to an open map document you may have to close the .mxd file first.
|
Script 2 Output |
The final script seemed like it would be the most difficult to get through but it was actually much easier than the second. I guess the reason it was easier is because I used a try-exception statement to trap (and skip) and error instead of having to fix it. There was an error with one line of code that affected several others and caused the whole script to stop running. By adding a try-except statement around the whole section of affected code the rest of the script was able to run. You can see in the graphic of the script output that there was an error in Part A but that Part B was still able to run. The nice thing about a try-except statement is that you can add a print statement the describes the error that was trapped.
|
Script 3 Output |
**Since I mentioned it in my post last week I feel obligated to follow up with my progress on my final project idea. So far I am still working on a good idea but this week I am thinking I may be able to write a code for this class that will help me with my final project in GIS Applications. I will ponder it this weekend and provide another update next week.
No comments:
Post a Comment