Monday, May 25, 2020

Python Debugging

This week's assignment was all about debugging.  Errors in programming are inevitable so it's important to learn how to begin to approach them while learning how to code.  We were provided with three different scripts and had to fix them so that they would all run correctly.
For this class most of our coding is done in Spyder.  When you run a script in Spyder, like all IDEs if it gets hung up on an error it will tell you with some kind of error message, like a traceback or syntax error.  Sometimes the easiest way to find a problem in your code is simply to run it and then see where the problem is.
Once mistakes such as misspellings or incorrect capitalization are cleared up then the program can run correctly. 
Another way to approach debugging is to use Spyder's debugger.  With this, you can work through the code line by line and see what is happening at each point.  This is a good way to identify an error where the code is running but not giving you the sort of output that you are expecting.

This script had eight bugs that needed to be teased out before it would properly run.

Sometimes, you might want to sidestep a bug entirely.  You can do this by adding in a "try" and except clause.  For this section of the assignment we had to identify that part of the code with an error and put it into a "try" clause.  This means that the code will be attempted, but if it cannot run it will turn to the except clause instead of simply breaking.  For the "except" clause I added in an error message that would identify what the problem was and print that out.  Afterwards, the code would continue as normal.
As you can see in Part A an error message is printed out from the "except" clause.  However, Part B runs without issue.


A Flowchart showing the try/except process


Bug fixing can be tedious but it's important to have different approaches to problem solving.  The more errors you fix, the easier it become to figure out other bugs in the future.

Monday, May 18, 2020

Python Fundamentals


The focus this week was getting down the basics of Python programming.  That means understanding variables and lists, using methods and functions, if-elif-else statements, for and while loops.

The first task was to make a string of my full name, then put each name into a list using the split function, then use string indexing to isolate just the last name.  Fairly straightforward, but it's a good way to make sure you have the basics down.

The next task was to fix two errors within a basic dice game.  I first tried to run the game to see where the code got hung up.  I then identified the problems (you can't concatenate an int to a string without first changing the type of the int, and a variable capitalization issue) and fixed them so that the code would properly run.

The final task this week was mastering loops.  First, I used a while loop to append 20 integers to an empty list.  When the list was complete the program printed the list.  Then I chose an integer that I wanted to remove from the list and assigned it to a variable.  I used the count method to check for the presence of this variable within the list in an if-else statement.  The statement would print out if the integer was in the list and how many times it was there.  Then I used a while that ran while the count of the chosen integer was greater than zero.  For each pass through the loop it removed one instance of the chosen integer.  Then the adjusted list was printed again.

Showing the results of the various basic processes covered in this lesson.
Flowchart for the luckyList section of the code.




Monday, May 11, 2020

Python Programming for GIS

This week is the start of another class - GIS programming.  Currently, the predominate scripting language for ArcGIS is Python which we will be using for this class.  Learning to program is useful as it helps to automate tasks and make routine functions much faster.

So far, we have been introduced to the concept of pseudocode which is the line-by-line process of thinking about what we want the code to do without writing it in standard Python syntax.  We also ran a Python script in the IDE Spyder which automatically generated a set of folders for out projects in the coming weeks.

The script-generated folders for the class.