Wednesday, May 29, 2024

Module 3- GIS Programming- Debugging and Error Handling

 This week’s assignment focused on diagnosing errors and debugging scripts. We learned several different types of errors and techniques for identifying errors and debugging, notably using try-except statements.

We were given 3 error-filled scripts to fix in IDLE. For the first script we had to identify and correct syntax errors. I fixed the first one easily but got stuck on one for quite a bit.  Without giving away the answer, I was stuck trying to figure out what was wrong with the variable for quite awhile before realizing I was misunderstanding how the words in a loop worked.

I was able to work through most of the errors in the second script methodically but ultimately, I ended up with the script running and giving me the appropriate output, but still giving an error. This took a while for me to resolve and after trying several things that only made the script not run, I realized that it was because I still had the script open in ArcGIS Pro. Once I closed the program the script was able to run and give me the requested output.

The third script required us not to directly fix the error but to add a try-except statement that allowed the script to run but to print the error instead of terminating the script. I was able to quickly identify where the issue in the script was and where to put the “try” statement but had a more difficult time figuring out where to put the “except” statement. I ended up adjusting my statement multiple times before I was able to get the desired result. The following flowchart shows the steps I took to debug this particular code.


Here are copies of the output of my debugged scripts:






Wednesday, May 22, 2024

Module 2- GIS Programming- Python Fundamentals

 This week we read about python fundamentals including: if, elif and else; lists, dictionaries and tuples; commonly used methods and function; loops and Booleans. We then practiced implementing these fundamentals both in the practice exercises and module 2 lab.

We were assigned 4 tasks: to print our last name from a created list, to fix errors within a prewritten code set, to populate and empty list with 20 random integers using a while loop with a counter variable. Then to use an “if” statement and a while loop to remove an “unlucky” number of our choice from the previously created list and then reprint the list with this integer removed.

 The first two tasks went smoothly. I created a string of my name and used the split function to create a list of individual names. Then I was able to use the index function to print only my last name. The second task was to correct errors in a provided “for” loop.

The third task was more complicated, but I feel like I learned a lot through this process. I began by creating my empty list and then made sure to import the random module, and began to put together my "while" loop. For my initial code I utilized the “random” function too soon in the script. This resulted in my output simply making a list of 20 duplicate integers. The second script I ran worked perfectly and gave the desired output, but it did not contain a counter variable which was a requirement of the assignment. I wasn’t quite grasping the concept of a counter variable and breaking a loop, so my next script ended up being an infinite loop and crashed the program. Finally, after a bit of re-reading I created a working script that contained all the desired components, however, my output was printing each addition to the list individually as a separate list, so I ended up with a giant triangle of 20 lists each with a single additional number. This was because I accidentally put the “print” function indented under the while loop. By moving it to the left where it belonged and not including it in the loop, I was finally able to print a single list of 20 randomly generated integers.

The final task built upon task 3. We were to remove an “unlucky” number (integer of our choice) from the list generated in the last task. We were to include an “if” statement to print one of two statements. If the integer was absent, we were to print that it was not in the list. If it was present we were to print a statement saying that the integer would be removed from the list for the total count of times it was included in the list. We were then to use a “while” loop to remove the integer from the list for the “count” of times it was present. I initially typed my print statements in my “if/else” statement with the integer itself in the string but I quickly realized I should reference the variable (unlucky is what I designated it) as a string within the code in order to create a code that was more efficient and could be changed by simply changing the variable if I desired, so I made sure that instead of typing “9 is not in the list” I would make sure the “9” was actually str(unlucky). I tested this by changing the variable to another integer and the code ran smoothly and changed easily along with the newly generated list and newly chosen integer. I finally was able to print the list with the number removed and completed the assignment, here is an image depicting my final output followed by a flowchart showing the steps I took to complete the task.








Tuesday, May 14, 2024

Module 1- GIS Programming- Python Environments and Flowcharts

 The first week of GIS Programming has commenced. I was nervous going into this class as I have no coding experience, but I am excited to learn something new. This week we learned the very basics of python and practiced making flowcharts. Our first task was to run a pre-scripted code that created all the folders we would need for the course. As I looked at the code, I noticed how easy it would be to slightly alter this code to use for other courses, assuming one was following a consistent folder pattern for each module. I find the functional aspect of coding interesting because I haven’t understood or considered all the different tasks it can be used to streamline before now.

One aspect of coding I think is less intuitive to me at this point is knowing what values to define. I could easily see myself forgetting to define a value when setting up my codes, especially as the codes get progressively more complex. I presume that it will become more instinctive with time/practice. In the case of this week’s assignment, which was to create a flowchart for a simple math calculation, it was such a different way of looking at a simple math problem- having to input each piece separately before putting it all together. When I began putting my flowchart together, I was unsure if “pi” would be a known or unknown variable in python so decided to test it by running the code to determine degrees based on my flowchart. It told me that there was an “error” with variable the variable “pi”. I tried again but added that pi=3.14159 and it gave me to output so I made sure to define that variable to my flowchart. I had run a google search before “testing” my equation and found that there is some way to use a math module or library that would allow me to simply use “pi” without declaring it but for the purposes of this lab didn’t investigate it further since we weren’t tasked with running the code at this time. However, I do want to follow up and figure out how that works at a later point if it is not covered in future labs.

A flowchart depicting the calculation of degrees when radians=3

Our last task was to access and read “The Zen of Python” and share what we think it means. I read “The Zen of Python” as a set of guidelines to consider when developing scripts using this program. It makes clear that the developers of python value clarity (with statements such as “explicit is better than implicit”), and simplicity (by defining a simple>complex>complicated hierarchy, and a preference for sparse over dense and flat over nested). This work makes clear that the purpose of python is not flashy/overcomplicated scripts but rather the beauty of python lies in ease of use. Fortunately for me, this challenges my previously contrived ideas of what coding would look like as I imagined it to be a very difficult, complicated process.


GIS Portfolio

 We were tasked to create a GIS portfolio for our internship program. It was a great opportunity to put organize the work I have been doing....