Python/programming

Lecture 1 of 3

Mathijs de Bruin (mathijs@mathijsfietst.nl),  January 7, 2014
The WSL Institute for Snow and Avalanche Research SLF

Available online

Feel free to click along!

Lecture URL QRCode

http://tinyurl.com/slf-python-day1

Python/programming workshop

Three days, three lectures
  1. SciPy, Shell/CLI and Python debugging
  2. Best practises and modular programming
  3. Version control, project structure

Advanced programming

Required prior to workshop

  • One year of general programming experience
  • Basic understanding of Python
  • Ability to start a terminal, execute commands
  • Installed Python distribution — preferably Enthought or Anaconda

Daily structure

  1. Introductionary lecture ~ 1,5 hour
  2. Hands-on with lecture material ~ 2-3 hours
  3. Support for individual projects ~ 2-3 hours
  4. Reserved moments for invidual questsions (see schedule)

Program - day 1

  1. Introduction and overview
  2. Inventarisation of common problems
  3. Scientific Python distributions
  4. Bash shell/Command Line Interface
  5. Python shell, IPython, debugging
  6. Installing and managing packages, PyPI
  7. Taming your text editor

Program - day 2

  1. Typical gotchas; references, immutables
  2. Best practises
    • Classes, methods and functions; use them!
    • Code formatting; PEP8
    • Don’t repeat yourself; DRY
    • Organizing your code; minimize globals, cleanout imports
    • Comments, Docstrings: documentation
  3. Structuring code, working with modules
  4. Packaging, creating reusable code

program - day 3

  1. Version control; what, how and why?
  2. GIT concepts
    • Branches
    • Commits
    • Forks
    • Merges
    • Hashes
  3. Setting up an example project
  4. And hosting it too (Bitbucket, GitHub)
  5. Having your code and forking it too
 

Program - day 1

  1. Introduction and overview
  2. Inventarisation of common problems
  3. Scientific Python distributions
  4. Bash shell/Command Line Interface
  5. Python shell, IPython, debugging
  6. Installing and managing packages, PyPI
  7. Taming your text editor

Inventarisation

Common problems

Documentation? Revision control? Packaging? Code structuring? Text editor WTF? Syntax errors? Testing? Weird bugs? Compilation hell? Whatsitmean? Reinventing the wheel? Whodunnit? Wheresitlive? (...)

Program - day 1

  1. Introduction and overview
  2. Inventarisation of common problems
  3. Scientific Python distributions
  4. Bash shell/Command Line Interface
  5. Python shell, IPython, debugging
  6. Installing and managing packages, PyPI
  7. Taming your text editor

Installing Python

A recipe for failure

  • Different versions of Python (2.6, 2.7, 3.3, ...)
  • Different operating systems (Mac, Linux, Windows)
  • Different versions of distributions (OS, official, other)
  • Different versions of dependencies (Numpy etc.)
  • Compilation problems (binary dependencies)
  • More compilation problems

Installing Python

Available options

  1. System Python (Mac, Linux)
  2. Official distribtion (python.org)
  3. Scientific distributions (Anaconda, Enthought)

System Python

Available on Mac and Linux

  • Often outdated
  • Installing/compiling Python packages difficult
  • No support for SciPy, Numpy etc.
  • Differences across OS'es
  • Requires no administrator privileges
  • Not available on Windows

Official Python

Available from python.org

  • Fully up to date
  • Installing/compiling Python packages difficult
  • Installation of SciPy, Numpy etc. supported
  • Requires administrator privileges

Scientific Python distributions

Preferred and suggested

  • Two tastes: Anaconda, Enthought
  • Almost fully up to date
  • Requires adminstrator privileges, only initially
  • SciPy, Numpy etc. installed by default
  • Same versions of packages across platforms
  • Commercial support available
  • Allows multiple (legacy) versions of Python to coexist

Program - day 1

  1. Introduction and overview
  2. Inventarisation of common problems
  3. Scientific Python distributions
  4. Bash shell/Command Line Interface
  5. Python shell, IPython, debugging
  6. Installing and managing packages, PyPI
  7. Taming your text editor

The Terminal

More or less equivalent:

  • Shell
  • Command Line Interface (CLI)
  • (ba)sh

The Terminal

UNIX origins

Keyboard + printer

UNIX shell

Basic principles

  • Command ➞ Response
  • Input ➞ Output
  • Streams and pipes
  • Environment variables

UNIX shell

Cygwin: UNIX for Windows

UNIX shell

Common concepts

  • Execution search path — $PATH
  • Home directory — $HOME, ~
  • Current working directory (CWD) — $PWD
  • Standard input, error and output (stdin, stderr, stdout)
  • Shell scripts (.sh files)

UNIX shell

Common commands

For later reference.

  • ls — list directory contents
  • cd <dir> — change directory
  • pwd — print (show) current working directory
  • man <command> — manual for command
  • echo "Text $VARIABLE" — print to stdout
  • apropos <topic> — search man pages
  • export PATH=~/bin:$PATH — set PATH variable
  • less <filename> — read and scroll trough file

UNIX shell

Handy tricks

  • Hit <TAB> for automatic completion
  • Use the ↑ and ↓ keys for command history
  • Use <CONTROL-D> to quit a shell (works for Python as well)
  • HELP!?
    <command> --help
    e.g.: ls --help

Scripts

“One damn thing after another”


#!/bin/sh
# This first line tells the system it's a shell script.
# Similarly, #!/bin/usr/python would execute Python code.

# Print hello world
echo "Hello world"

# Print $PATH variable
echo "My PATH variable is $PATH"

# Change into some-directory
cd some-directory

# List directory contents
ls
					

Scripts

Having your script and executing it too


# Set executable permissions
chmod +x myscript.sh

# Executing command outside of $PATH requires explicit path
./myscript.sh

Shell initialization

~/.bash_profile (and ~/.bashrc)

  • Executed whenever a terminal is started
  • Configures environment variables such as:
    • $PATH
    • $PYTHONPATH
    • $EDITOR
  • Sets personal program settings
  • Executes arbitrary shell code

Program - day 1

  1. Introduction and overview
  2. Inventarisation of common problems
  3. Scientific Python distributions
  4. Bash shell/Command Line Interface
  5. Python shell, IPython, debugging
  6. Installing and managing packages, PyPI
  7. Taming your text editor

Interactive Python shell

Similar to bash shell: execute command, print output


$ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world'
hello world
>>> quit()
					

IPython

Python shell on steroids

Similar to Mathematica


$ ipython
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:
					

IPython features

Similar to UNIX shell

  • Command line completion
  • History available
  • Syntax coloring
  • Much more, read the manual!

ipdb

Interactive debugging

Debugging with IPython-like interface; stop your code anywhere and query variables or execute arbitrary code.

One line to rule them all:

import ipdb; ipdb.set_trace()

ipdb

Important commands

  • n — Execute next line of code
  • s — Step info function call
  • c — Continue execution as normal
  • q — Quit debugger

ipdb example

Code:


def addition(one, other):
    import ipdb; ipdb.set_trace()

    print 'Calculating now'

    return one + other

print 'Starting calculation'
addition(2, 5)

print 'Calculation done'
					

ipdb example

Result:


$ python ipdb-example.py
Starting calculation
> ipdb-example.py(4)addition()
      3
----> 4     print 'Calculating now'
      5

ipdb> n
Calculating now
> ipdb-example.py(6)addition()
      5
----> 6     return one + other
      7

ipdb> n
--Return--
7
> ipdb-example.py(6)addition()
      5
----> 6     return one + other
      7

ipdb> n
> ipdb-example.py(11)<module>()
      9 addition(2, 5)
     10
---> 11 print 'Calculation done'

ipdb> n
Calculation done
--Return--
None
> ipdb-example.py(11)<module>()
      9 addition(2, 5)
     10
---> 11 print 'Calculation done'

ipdb> quit
Exiting Debugger.
					

Program - day 1

  1. Introduction and overview
  2. Inventarisation of common problems
  3. Scientific Python distributions
  4. Bash shell/Command Line Interface
  5. Python shell, IPython, debugging
  6. Installing and managing packages, PyPI
  7. Taming your text editor

Python packages a.k.a. Eggs

  • Whenever you see setup.py, it's an Egg.
  • Eggs are self-contained software packages (e.g. libraries, commands)
  • Install it through PIP (next slide) or with:
    $ python setup.py install

PyPI a.k.a. The Cheese Shop

https://pypi.python.org/

Lot's and lot's of Eggs. Probably someone just solved your problem. In Iran. Or down the hallway.

PIP; Python package manager

Search, download, install and upgrade packages

$ pip search <package>
$ pip install <package>
$ pip install --upgrade <package>
$ pip uninstall <package>

requirements.txt

Managing versions of requirements

Textfile with required packages and versions, e.g.:


pkg1
pkg2
pkg3>=1.0,<=2.0
					

Reference: PIP cookbook

requirements.txt

Installing

pip install -r requirements.txt

Creating

pip freeze > requirements.txt

Warning: use only with VirtualEnv (next slide)

VirtualEnv

Keeping things clean

Sets up a 'local' Python distribution allowing you to:

  • Install different versions of packages
  • Exclude system packages (unless explicitly included)
  • Install packages without administrator privileges

VirtualEnv

Usage

Install VirtualEnv (only once):

$ pip install virtualenv

Create an isolated environment:

$ virtualenv [--system-site-packages] <dirname>

Activate the environment:

$ source <dirname>/bin/activate

Deactivate the environment:

$ deactivate

VirtualEnv

Example (1/4)


$ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xmodem
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named xmodem
					

VirtualEnv

Example (2/4)


$ virtualenv test
New python executable in test/bin/python
Installing Setuptools..............................................................................................................................................................................................................................done.
Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
$ source test/bin/activate
(test) $ pip install xmodem
Downloading/unpacking xmodem
  Downloading xmodem-0.3.2.tar.gz
  Storing download in cache at ./.pip/download_cache/http%3A%2F%2Fpackages.crate-cdn.com%2F4%2Fe%2F5%2F7%2F4e57f3fadd1f3f043a23f1a87e9d36f944a21b552aabebcdb802154667396567%2Fxmodem-0.3.2.tar.gz
  Running setup.py egg_info for package xmodem

Installing collected packages: xmodem
  Running setup.py install for xmodem

Successfully installed xmodem
Cleaning up...
					

VirtualEnv

Example (3/4)


(test) $ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xmodem
>>>
					

VirtualEnv

Example (4/4)


(test) $ deactivate
$ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xmodem
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named xmodem
					

Program - day 1

  1. Introduction and overview
  2. Inventarisation of common problems
  3. Scientific Python distributions
  4. Bash shell/Command Line Interface
  5. Python shell, IPython, debugging
  6. Installing and managing packages, PyPI
  7. Taming your text editor

Watch out for:

  • Trailing whitespace
  • Tabs; use 4 spaces instead
  • Trailing newlines

Example

Syntax errors

Use a Linter

Find syntax, style and other errors before running your code: less bugs, more productivity and much more beautiful code.

Thanks!

Any questions?