Sunday, May 29, 2016

GIS5103 - Module 3 - Python Fundamentals II

In Module 3 we were tasked with editing and completing a provided script.  After reading the required texts for this module I was initially weary of the assignment.   Many moons ago, programming did not come naturally to me.   In various work experiences I usually edited existing code instead of creating it.  So, I was happy to start the assignment by fixing/repairing some syntax issues in the provided code in Parts 1 and 2.   Part 1 emphasized the fact that a module has to be imported at the beginning of the script in order to use the functions of that module in the script.  Part 2 of the script was a dice game based on the length of the player's name.  The two syntax error corrections pointed out that case and type are important to producing an error free script.

Part 3 of the lab required us to generate a list of 20 numbers between 0 and 10 and it told us to use the "while" loop structure.   This made sense based on our reading of loop structures and the "while" loop requiring an exit condition; in our case the 20th number being generated.  At first I was stuck on how to put the generated numbers in a list but the assignment hint said to create an empty list first.   The Zandbergen text helped me figure out my next steps and the modules to use.

Flowchart for Step 3
Part 4 had us create a branching block and a loop structure using the list created in Part 3.  Again, the clues provided in the assignment steered me in the right direction.   A sketched flow chart helped me start the branching block and I also used the Zandbergen text to select the appropriate modules for both the branching and loop structures.  It was awesome seeing the results print on the interactive window.   I ran the script several times to make sure the correct result printed for each condition depicted in the script.   

I initially thought about receiving input from the script user to assign the "unlucky" number but quickly realized this was a little more complicated because I would have to take into account that the number received could only be between 0 and 10.   I would also have to create code to check for the length of the input and then truncate it to a value less than or equal to 2 in case a user typed in a value greater than 10.  I may still try to figure this out.

Flowchart for Step 4


Python code results for dice game and list creation and removal of unlucky number

Wednesday, May 25, 2016

GIS5103 - Module 2 - Python Fundamentals Part 1

In this week's module I found the lecture assignments and exercises very valuable in assisting with the lab assignment.  We were tasked with creating a script that given a string full name would then split the full name and print only the last name.  The script then had to calculate the length of the last name assigned in the original string and then multiply that length by three and print the value.   Because variable names were provided in the assignment, I had a better understanding of the script code.  My first script contained seven lines and produced the desired results.

Results of Mod2_MThrush.py running script in PythonWin

After creating the script and running it successfully, I created a flow chart.  I know this process should be reversed; create the flowchart and then create the script.  This is a habit I will need to form as scripts will become more complex.

Flow Chart of Mod2_MThrush.py which prints the last name of an assigned full name string and the value of three times the length of the last name.
While I was completing the Process Summary, I realized there was a way to reduce at least one line of code in my script.  After successfully running my second script, I found an additional line that I could remove.  My final script contained 5 lines of code and produced the same results.  There is  always more than one way to solve a problem or "skin the cat", especially with scripting/programming.   Editing and modifying my original script enabled me to get some experience using Python Win and to better understand Python syntax.

Saturday, May 14, 2016

GIS5103 - Module 1 - Introduction to Python

In the first module of GIS Programming we were introduced to programming with Python.   We first learned about scripting versus programming and the layout of our Python textbook.   Next we were introduced to different Python editors.   Specifically we used Python's Integrated Development Environment (IDLE).   We learned about an interactive interpreter and its use to interpret and immediately carry out commands.   You cannot save scripts in an interactive interpreter.   In a script window you must first save the script before it can be run.   You can simultaneously show the interactive interpreter and the script window when creating a script.  Syntax highlighting is the use of different colors to highlight statements, strings, etc. as a means to error check as you write code.  We also were introduced to the PythonWin editor which we will use in this class along with the Python window within ArcMap.  We learned how to create pseudocode for given problems/objectives and to then translate into Python syntax.   We were provided a Python script that when Run created 12 module folders within a Programming folder on our S: drive.

CreateModuleFolders Python Script
I am a little nervous about this class as it has been quite some time since I took a programming course.   Programming does not come naturally for everyone and a quarter of a century ago, I found it difficult.  Hopefully, with some years and experiences behind me it will be easier this go around.