SWC/LC Workshop: Introduction to Python

King's College London

January 9-10, 2020

9:00 - 17:00

Instructors: Luca Di Stasio, George Graham

Helpers: Stefania Marcotti, Walter Muruez Gutierrez, Neil Jakeman, Alessia Visconti, Natasha Romanova, Fiona Wardle

General Information

Software Carpentry aims to help researchers get their work done in less time and with less pain by teaching them basic research computing skills. This hands-on workshop will cover basic concepts and tools, including program design, version control, data management, and task automation. Participants will be encouraged to help one another and to apply what they have learned to their own research problems.

For more information on what we teach and why, please see our paper "Best Practices for Scientific Computing".

Who: The course is aimed at postgraduates and staff in the Faculties of Arts & Humanities and Social Science & Public Policy at KCL. You don't need to have any previous knowledge of the tools that will be presented at the workshop.

Where: King's College London, Strand Campus, Bush House South East Wing, Room BH(SE) 1.01 (Jan. 9) and BH(SE) 6.04 (Jan. 10), 300, Strand, London WC2R 1AE. Get directions with OpenStreetMap or Google Maps.

When: January 9-10, 2020. Add to your Google Calendar.

Requirements: Participants must bring a laptop with a Mac, Linux, or Windows operating system (not a tablet, Chromebook, etc.) that they have administrative privileges on. They should have a few specific software packages installed (listed below).

Code of Conduct: Everyone who participates in Carpentries activities is required to conform to the Code of Conduct. This document also outlines how to report an incident if needed.

Accessibility: We are committed to making this workshop accessible to everybody. The workshop organizers have checked that:

Materials will be provided in advance of the workshop and large-print handouts are available if needed by notifying the organizers in advance. If we can help making learning easier for you (e.g. sign-language interpreters, lactation facilities) please get in touch (using contact details below) and we will attempt to provide them.

Contact: Please email fiona.wardle@kcl.ac.uk , luca.di.stasio@ltu.se or geogra@MBA.ac.uk for more information.


Surveys

Please be sure to complete these surveys before and after the workshop.

Pre-workshop Survey

Post-workshop Survey


Schedule (preliminary)

Day 1

Before Pre-workshop survey
09:00 Introduction to Working with Data
10:30 Morning break
10:45 OpenRefine
12:00 Lunch break
13:00 Automating Tasks with the Unix Shell
15:00 Afternoon break
15:15 Automating Tasks with the Unix Shell (Continued)
16:55 Wrap-up
17:00 END

Day 2

09:00 Building Programs with Python (Part I)
10:30 Morning break
10:45 Building Programs with Python (Part II)
12:00 Lunch break
13:00 Plotting in Python
15:00 Afternoon break
15:15 Advanced topics in Python
16:50 Post-workshop Survey
17:00 END

We will use this collaborative document for chatting, taking notes, and sharing URLs and bits of code.


Syllabus (preliminary)

The Unix Shell

  • Files and Directories
  • History and Tab Completion
  • Pipes and Redirection
  • Looping Over Files
  • Creating and Running Shell Scripts
  • Finding Things
  • Reference...

Programming in Python

  • Using Libraries
  • Working with Arrays
  • Reading and Plotting Data
  • Creating and Using Functions
  • Loops and Conditionals
  • Using Python from the Command Line
  • Statistics with Pandas
  • Defensive Programming
  • Plotting
  • Reference...
  • Reference...

Open Refine

  • Introduction to OpenRefine
  • Importing Data
  • Basic Functions
  • Advanced Functions
  • Reference...

Setup

To participate in a Software Carpentry workshop, you will need access to the software described below. In addition, you will need an up-to-date web browser.

We maintain a list of common issues that occur during installation as a reference for instructors that may be useful on the Configuration Problems and Solutions wiki page.

The Bash Shell

Bash is a commonly-used shell that gives you the power to do simple tasks more quickly.

Video Tutorial
  1. Download the Git for Windows installer.
  2. Run the installer and follow the steps below:
    1. Click on "Next" four times (two times if you've previously installed Git). You don't need to change anything in the Information, location, components, and start menu screens.
    2. From the dropdown menu select "Use the nano editor by default" and click on "Next".
    3. Ensure that "Git from the command line and also from 3rd-party software" is selected and click on "Next". (If you don't do this Git Bash will not work properly, requiring you to remove the Git Bash installation, re-run the installer and to select the "Git from the command line and also from 3rd-party software" option.)
    4. Ensure that "Use the native Windows Secure Channel library" is selected and click on "Next".
    5. Ensure that "Checkout Windows-style, commit Unix-style line endings" is selected and click on "Next".
    6. Ensure that "Use Windows' default console window" is selected and click on "Next".
    7. Ensure that "Enable file system caching" and "Enable Git Credential Manager" are selected and click on "Next".
    8. Click on "Install".
    9. Click on "Finish".
  3. If your "HOME" environment variable is not set (or you don't know what this is):
    1. Open command prompt (Open Start Menu then type cmd and press [Enter])
    2. Type the following line into the command prompt window exactly as shown:

      setx HOME "%USERPROFILE%"

    3. Press [Enter], you should see SUCCESS: Specified value was saved.
    4. Quit command prompt by typing exit then pressing [Enter]

This will provide you with both Git and Bash in the Git Bash program.

The default shell in all versions of macOS is Bash, so no need to install anything. You access Bash from the Terminal (found in /Applications/Utilities). See the Git installation video tutorial for an example on how to open the Terminal. You may want to keep Terminal in your dock for this workshop.

The default shell is usually Bash, but if your machine is set up differently you can run it by opening a terminal and typing bash. There is no need to install anything.

Git

Git is a version control system that lets you track who made changes to what when and has options for easily updating a shared or public version of your code on github.com. You will need a supported web browser.

You will need an account at github.com for parts of the Git lesson. Basic GitHub accounts are free. We encourage you to create a GitHub account if you don't have one already. Please consider what personal information you'd like to reveal. For example, you may want to review these instructions for keeping your email address private provided at GitHub.

Git should be installed on your computer as part of your Bash install (described above).

Video Tutorial

For OS X 10.9 and higher, install Git for Mac by downloading and running the most recent "mavericks" installer from this list. Because this installer is not signed by the developer, you may have to right click (control click) on the .pkg file, click Open, and click Open on the pop up window. After installing Git, there will not be anything in your /Applications folder, as Git is a command line program. For older versions of OS X (10.5-10.8) use the most recent available installer labelled "snow-leopard" available here.

If Git is not already available on your machine you can try to install it via your distro's package manager. For Debian/Ubuntu run sudo apt-get install git and for Fedora run sudo dnf install git.

Text Editor

When you're writing code, it's nice to have a text editor that is optimized for writing code, with features like automatic color-coding of key words. The default text editor on macOS and Linux is usually set to Vim, which is not famous for being intuitive. If you accidentally find yourself stuck in it, hit the Esc key, followed by :+Q+! (colon, lower-case 'q', exclamation mark), then hitting Return to return to the shell.

nano is a basic editor and the default that instructors use in the workshop. It is installed along with Git.

Others editors that you can use are Notepad++ or Sublime Text. Be aware that you must add its installation directory to your system path. Please ask your instructor to help you do this.

nano is a basic editor and the default that instructors use in the workshop. See the Git installation video tutorial for an example on how to open nano. It should be pre-installed.

Others editors that you can use are BBEdit or Sublime Text.

nano is a basic editor and the default that instructors use in the workshop. It should be pre-installed.

Others editors that you can use are Gedit, Kate or Sublime Text.

Python

Python is a popular language for research computing, and great for general-purpose programming as well. Installing all of its research packages individually can be a bit difficult, so we recommend Anaconda, an all-in-one installer.

Regardless of how you choose to install it, please make sure you install Python version 3.x (e.g., 3.6 is fine).

We will teach Python using the Jupyter notebook, a programming environment that runs in a web browser. For this to work you will need a reasonably up-to-date browser. The current versions of the Chrome, Safari and Firefox browsers are all supported (some older browsers, including Internet Explorer version 9 and below, are not).

  1. Open https://www.anaconda.com/download/#linux with your web browser.
  2. Download the Python 3 installer for Linux.
    (The installation requires using the shell. If you aren't comfortable doing the installation yourself stop here and request help at the workshop.)
  3. Open a terminal window.
  4. Type
    bash Anaconda3-
    and then press Tab. The name of the file you just downloaded should appear. If it does not, navigate to the folder where you downloaded the file, for example with:
    cd Downloads
    Then, try again.
  5. Press Return. You will follow the text-only prompts. To move through the text, press Spacebar. Type yes and press enter to approve the license. Press enter to approve the default location for the files. Type yes and press enter to prepend Anaconda to your PATH (this makes the Anaconda distribution the default Python).
  6. Close the terminal window.

OpenRefine (formerly Google Refine)

For this lesson you will need OpenRefine (formerly Google Refine) and a web browser.

Note: this is a Java program that runs on your machine (not in the cloud). It runs inside your browser, but no web connection is needed.

  1. If you have Internet Explorer (or Edge) set as your default web browser, check that you have Firefox or Chrome installed and set either of them as your default browser. OpenRefine runs in your default browser, but will not run correctly in Internet Explorer.
  2. Learn how to set your browser as default by clicking on this link for Google Chrome and this link for Firefox.
  3. Download software from OpenRefine
  4. Select the most recent version of OpenRefine (do not select beta versions or the release candidates). The version that you should download will be at the top of the page and named OpenRefine 3.1 for example.
  5. Unzip the downloaded file into a directory by right-clicking and selecting “Extract…”. Name that directory something like OpenRefine.
  6. Go to your newly created OpenRefine directory.
  7. Launch OpenRefine by clicking on openrefine.exe (this will launch a command prompt window first; ignore that, and wait for OpenRefine to launch in the web browser, which is where you will interact with the program).
  8. If you are using a different browser, or OpenRefine does not automatically open for you, point your browser at http://127.0.0.1:3333/ or http://localhost:3333 to launch the program.
  1. Check that you have Firefox or Chrome browsers installed and set as your default browser. OpenRefine runs in your default browser. It will not run correctly in Internet Explorer.
  2. Download software from OpenRefine
  3. Select the most recent version of OpenRefine (do not select beta versions or the release candidates). The version that you should download will be at the top of the page and named OpenRefine 3.1 for example.
  4. Unzip the downloaded file into a directory by double-clicking it. Name that directory something like OpenRefine.
  5. Go to your newly created OpenRefine directory.
  6. Launch OpenRefine.
  7. Drag icon into Applications folder, and Ctrl-click/Open… it.
  8. If you are using a different browser, or OpenRefine does not automatically open for you, point your browser at http://127.0.0.1:3333/ or http://localhost:3333 to launch the program.
  1. Check that you have Firefox or Chrome browsers installed and set as your default browser. OpenRefine runs in your default browser. It will not run correctly in Internet Explorer.
  2. Download software from OpenRefine
  3. Select the most recent version of OpenRefine (do not select beta versions or the release candidates). The version that you should download will be at the top of the page and named OpenRefine 3.1 for example.
  4. Unzip the downloaded file into a directory by double-clicking it. Name that directory something like OpenRefine.
  5. Go to your newly created OpenRefine directory.
  6. Launch OpenRefine.
  7. Type ./refine into the terminal within the OpenRefine directory.
  8. If you are using a different browser, or OpenRefine does not automatically open for you, point your browser at http://127.0.0.1:3333/ or http://localhost:3333 to launch the program.