PyCAR

March 2-3, 2017 - NICAR, Jacksonville

Tom Meagher / @ultracasual
Chris Keller / @chrislkeller
Heather Billings / @hbillings
Eric Sagara / @esagara
Adriana Homolova / @naberacka
Roberto Rocha / @robroc
Elaine Wong / @elthenerd
Jacob Sanders / @thejqs

 

bit.ly/pycar17

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 3, pip, virtualenv and virtualenvwrapper installed.

#create and activate a sandbox to work in
mkvirtualenv pycar17

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

#install the dependencies: 
#jupyter, requests, beautifulsoup4, lxml, pandas
pip3 install -r requirements.txt

#launch the interactive Jupyter notebook in your browser
jupyter notebook
					

Strings

Strings are ordered sequences of characters wrapped in quotes.


var1 = "This class is at NICAR in Jacksonville."
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, "Jax"]

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


Even more questions? Email Tom or ping me on Twitter