Chicago Python Workshop/Projects

Welcome to the Saturday afternoon projects section of the Chicago Python Workshop! After lunch, we'll break out into groups to practice Python through using the Twitter API to write the basic parts of a Twitter client.

Setup
See the Friday setup instructions.

Twitter goals

 * practice for loops
 * practice using functions
 * practice implementing functions
 * see what it's like to use an API
 * have fun collecting data from Twitter

Indentation reminder
In Python, indentation matters. Everything is indented by a multiple of some number of spaces, often 4.

In  statements, you indent everything you want to be run if the if conditional is. For example:

>>> James = 35 >>> Alice = 30 >>> if James > Alice: ...    print "James is older than Alice." ... James is older than Alice. >>>

Because James really is older than Alice, the  conditional is  , so Python does execute the code indented under the if line. In this case we print "James is older than Alice."

>>> James = 35 >>> Alice = 30 >>> if James < Alice: ...    print "James is younger than Alice." ... >>>

Because James is not older than Alice, the  conditional is , so Python does not execute the code indented under the if line.

In for loops, you indent everything you want to be run each loop For example:

>>> names = ["Jessica", "Adam", "Liz"] >>> for name in names: ...    print "Hello " + name ... Hello Jessica Hello Adam Hello Liz

The  line is indented 4 spaces under the. That's how Python knows to execute the print line for every name in names.

Functions
Let's review what we know about functions:


 * They do some useful bit of work.
 * They let us re-use code without having to type it out each time.
 * They take input and possibly produce output (we say they return a value). You can assign a variable to this output.
 * You define a function using the def keyword.
 * You call a function by using its name followed by its arguments in parenthesis.

Here's an example:

>>> def add(x, y): ...    return x + y ... >>> add(1, 2) 3 >>> add(-1, 1) 0 >>> add(.5, .75) 1.25

pass
is a keyword that just means "do nothing". It most often shows up as a place-holder for code that doesn't exist yet. For example:

>>> def testFunction: ...    pass ... >>>

Imports
Imports look like this:

>>> import random >>> import time

In the above example,  and   are both Python modules. Modules are Python files outside of the current Python file that contain Python code, like functions and variables. You can use code from modules by first importing the module. Here's an example from the module:

>>> import random >>> random.randint(0, 10) 7 >>> random.randint(0, 10) 6 >>> random.randint(0, 10) 1 >>> random.randint(0, 10) 3 >>> random.randint(0, 10) 4 >>> random.randint(0, 10) 9

is a function in the  module. It takes a lower bound as the first argument and an upper bound as the second argument and returns a random integer between those bounds.

Python Twitter library functions we will use
A library provides a collection of functions for you, and defines a contract for using those functions. Here are the functions in the python-twitter library that will be useful for the project:

Given a string to search for, this function will return a list of tweets matching that search string.

Given a username, this function will return a list of tweets belonging to that username.

Python Twitter Project Instructions
Here are the Twitter Project Instructions.

Download the Wordplay project
We've written some skeleton code for the Wordplay project already. Download this code so you're ready to start working with it tomorrow:


 * 1) Right click the following file, click "Save Target as..." or "Save link as...", and save it to your Desktop directory:
 * 2) * http://web.mit.edu/jesstess/www/BostonPythonWorkshop5/Wordplay/Wordplay.zip
 * 3) Windows:
 * 4) Find Wordplay.zip on your Desktop and double-click on it to "unzip" it. That will create a folder called Wordplay containing several files.
 * 5) Mac OSX
 * 6) Find Wordplay.zip on your Desktop and double-click on it. This will create a directory for Wordplay, containing the source code for the dependency.
 * 7) Linux
 * 8) Find Wordplay.tar.gz on your Desktop and double-click on it to "extract" it. That will create a folder called Wordplay containing several files.

Test the Wordplay code
Start a command prompt and navigate to the Desktop\Wordplay directory where the Wordplay code lives. For example, if the Wordplay project is at ,

cd C:\Users\jesstess\Desktop\Wordplay

will change you into that directory, and

dir

will show you the source code files in that directory. One of the files is "words1.py", which has a ".py" extension indicating that it is a Python script. Type:

python words1.py

at the command prompt to execute the words1.py Python script. You should see a column of English words printed to the screen. If you don't, let a staff member know.

Success!
You've completed setup for the Wordplay project.

Wordplay goals

 * practice for loops
 * practice using lists
 * practice manipulating strings
 * get experience with regular expressions
 * have fun cheating at crosswords and Words with Friends

Indentation reminder
In Python, indentation matters. Everything is indented by a multiple of some number of spaces, often 4.

In  statements, you indent everything you want to be run if the if conditional is. For example:

>>> James = 35 >>> Alice = 30 >>> if James > Alice: ...    print "James is older than Alice." ... James is older than Alice. >>>

Because James really is older than Alice, the  conditional is  , so Python does execute the code indented under the if line. In this case we print "James is older than Alice."

>>> James = 35 >>> Alice = 30 >>> if James < Alice: ...    print "James is younger than Alice." ... >>>

Because James is not older than Alice, the  conditional is , so Python does not execute the code indented under the if line.

In for loops, you indent everything you want to be run each loop For example:

>>> names = ["Jessica", "Adam", "Liz"] >>> for name in names: ...    print "Hello", name ... Hello Jessica Hello Adam Hello Liz

The  line is indented 4 spaces under the. That's how Python knows to execute the print line for every name in names.

range
>>> range(5) [0, 1, 2, 3, 4] >>> for i in range(5): ...    print "Hi" * i ...

Hi HiHi HiHiHi HiHiHiHi

statements inside loops
>>> for i in range(80): ...    if i % 9 == 0: ...        print i, "is divisible by 9." ... 0 is divisible by 9. 9 is divisible by 9. 18 is divisible by 9. 27 is divisible by 9. 36 is divisible by 9. 45 is divisible by 9. 54 is divisible by 9. 63 is divisible by 9. 72 is divisible by 9.

loops inside loops
>>> letters = ["a", "b", "c"] >>> numbers = [1, 2, 3] >>> for letter in letters: ...    for number in numbers: ...        print letter * number ... a aa aaa b bb bbb c cc ccc >>> for number in numbers: ...    for letter in letters: ...        print number * letter ... a b c aa bb cc aaa bbb ccc

Imports
Imports look like this:

>>> import random >>> import time

In the above example,  and   are both Python modules. Modules are Python files outside of the current Python file that contain Python code, like functions and variables. You can use code from modules by first importing the module. Here's an example from the module:

>>> import random >>> random.randint(0, 10) 7 >>> random.randint(0, 10) 6 >>> random.randint(0, 10) 1 >>> random.randint(0, 10) 3 >>> random.randint(0, 10) 4 >>> random.randint(0, 10) 9

is a function in the  module. It takes a lower bound as the first argument and an upper bound as the second argument and returns a random integer between those bounds.

keyword
to say backwards:

>>> fruits[::-1] ['cherries', 'bananas', 'apples']

Regular expressions

 * ^ is an anchor that means "beginning"
 * $ is an anchor that means "end"
 * . means a single wildcard character
 * .* means any number (including zero) of wildcard characters

Examples:


 * will match anything that contains "rstu", for example "understudy".
 * will match anything that starts with "aa", for example "aardvark".
 * will match anything containing "a", then any single character, then "b", then any single character, then "c", for example "iambic".
 * will match anything containing "ss", then anything, then "ss", for example "messiness".
 * will match anything that starts with "c", then any single character, then d, then any single character, then ends with an "e", for example "cadre".

Python Wordplay Project Instructions
Here are the Wordplay Project Instructions.