Scripting with Python

ENV 859 - Geospatial Data Analytics   |   Fall 2024   |   Instructor: John Fay  

Introduction

So far, we’ve only examined the very basic concepts and building blocks of Python. Python’s true power, of course, comes when we combine these elements to accomplish a specific task. This is the process of crafting a Python script, and it requires an additional layer of skills and understanding within the coding landscape. That’s the topic of this lesson.

While a Python script is basically just a string of Python commands written in a simple text file, writing a script involves planning, logic, patience, persistence, often a good degree of trial and error. The process can be equal parts frustration (why isn’t my code working?!?) and exhilaration (my code is working!!!), and individuals can take very different approaches, different styles to the same scripting task.

Here we review a number of techniques, tools, and best practices to ease the process of writing Python scripts. We do so in the context of a particular scripting project, a common scripting task involving cleaning up and querying data. We’ll introduce some new technologies (integrated development environments and versioning software) that facilitate the coding process. We’ll also examine ways to approach a scripting task that should make the process less daunting and improve overall success. And throughout this process, we’ll continue gaining experience learning Python, introducing a few new Python objects and their uses, and also discussing how to format Python scripts that make them more “Pythonic” (as well as what that term means).

Section organization and central task: Tidying ARGOS tracking data files

This section is organized as a set of tasks which incrementally build up to a working tool that accomplishes a typical workflow. We want to develop a tool that allows a user to identify a specific ARGOS observation and display information about it given a raw ARGOS satellite tracking data file. Each task in the sequence introduces or reviews new scripting concepts. However, the exercise as a whole will expose you to a general approach to writing scripts, namely breaking the overall script objective into logical workflow of intermediate steps, each of which is relatively easy to implement – and to evaluate for errors.

As mentioned above, we’ll also take some time to learn technologies that make this process easier. First, we’ll look at Visual Studio Code (or just VSCode), an integrated development environment, or IDE, that provides an alternative to Jupyter Notebooks for writing Python code. And second, we’ll look at Git and GitHub, two parts of a version control system that are instrumental both in writing and updating code and in many cloud-based technologies that we’ll examine later in the course.

NOTE:

Prior to running the steps in this section, you will need to install VSCode and Git for Windows on your Virtual Machine. To do this, download the installer files (linked below), and run them, accepting all default values in the installation dialog (except the one for Git, noted below). We will examine how to configure these applications later in this section.


Learning outcomes

Section On completion of the section, you will be able to…
1. Introduction to IDEs: VS Code • Describe the benefits of using an IDE
Set up VS Code for use with Python
Open VS, setting the workspace to a specific folder
• Identify and use VS Code various panes
• Explore and set various Spyder user settings and preferences
Create, modify, execute and save a script within Spyder
2. Approaching a Scripting Task • Articulate an example task where Python is helpful
• Develop pseudocode for an example task
• Dissect your pseudocode into a coding workplan
• Prepare a coding workspace
3. Version Control: GitHub and Git • Explain the utility of version control in coding projects
Create a new project repository on GitHub
Clone the repository to a local machine using Git
Add, stage, and commit changes to the local repository
Fetch, Pull, and Push changes between local and remote repositories
4. Writing & Debugging Python Scripts Initialize a script file with header comments
Split strings into a list and access items in this list
Read and write text files with Python’s file object
• Create a for loop and use it to repeat a section of code
• Create a while loop and use it to repeat a section of code
• Apply conditional statements & comparative operators
• Identify and debug scripting errors (syntax and logical)
Tidy and share Python scripts