PyCAR

March 10, 2016 - NICAR, Denver

By:
Tom Meagher / @ultracasual,
Alex Richards / @alexrichards,
Adriana Homolova / @naberacka,
Cathy Deng / @cthydng,
Scott Bradley / @scott2b

 

bit.ly/pycar16

Why should you code?

Cover more ground, faster

Helps document reporting

Makes analysis replicable

Automation!

We won't learn everything

The Command Line

Git or Github

pip or virtualenvs

Frameworks

News apps

Journalism > "Development"

This will not be nuanced, idiomatic Python.

Some programmers may be saddened by this code.

But you know what? If it works, and it works on deadline, that's what matters for us today.

The goal

To start thinking about
how to break problems down
into the smallest tasks
that can be programmed.

If you know Excel

You can learn to program.

  • "AZ Arizona" is a string.
  • "A2" is a variable.
  • "=left()" is a function.
  • A2 and 2 are function arguments.

Why Python?

Easy to learn.

Explicit.

Mature and well-documented.

Strong PythonJournos community of support.


Prep your workspace

To set up your machine, you'll need to have Python 2.7, pip and virtualenv installed.

#create and activate a sandbox to work in
virtualenv pycar16    
source pycar16/bin/activate

#clone or download the code repo from Github
git clone git@github.com:ireapps/pycar.git

#install the dependencies: requests, beautifulsoup4, unicodecsv
pip install -r requirements.txt

#launch the interactive interpreter
ipython
					

Strings

Strings are ordered sequences of characters wrapped in quotes.


var1 = "This class is at NICAR in Denver."
var2 = "&You!_123 Four"


Follow along at home here.
If you want to cheat, the answers are here.

Integers and Floats

Numbers that you can do math on.

Integers are whole numbers.
Floats are decimals.

Lists

An ordered collection of objects, wrapped in brackets.

my_list = [1, 2, "Mile High City"]

Dicts

A collection of named keys and their associated values, wrapped in curly braces.

my_dict = {'Fruit': 'Orange', 'Weight': 10}

Conditionals

Logic that can trigger other operations,
similar to Excel's if function.

In Excel:

In Python:

score = 1
if score > 2:
    print("Win")
else:
    print("Lose")

Onward

Keep learning

"Data Wrangling with Python"

"Web Scraping With Python"

PythonJournos listserv

Excellent post on ethics of scraping

More resources for learning Python

NICAR-L

Github, StackOverflow, Google

--30--

Clone the source code


Email Tom or ping me on Twitter