Skip to content

Instantly share code, notes, and snippets.

@jcarbaugh
Last active December 11, 2015 06:08
Show Gist options
  • Save jcarbaugh/4556951 to your computer and use it in GitHub Desktop.
Save jcarbaugh/4556951 to your computer and use it in GitHub Desktop.
An example of using python-saucebrush with legislators.csv from the Sunlight Foundation Congress API bulk download.
from saucebrush import emitters, filters, sources, stats
import saucebrush
import us
CHAMBERS = {"House": "Rep", "Senate": "Sen"}
STATES = [s.abbr for s in us.STATES]
TERRITORIES = [t.abbr for t in us.TERRITORIES]
class StateFilter(filters.ConditionalFilter):
def __init__(self, states):
self.states = [states] if isinstance(states, basestring) else states
def test_record(self, record):
return record["state"] in self.states
class InOfficeFilter(filters.ConditionalFilter):
def test_record(self, record):
return record["in_office"] == "1"
class ChamberFilter(filters.ConditionalFilter):
def __init__(self, chamber):
if chamber not in CHAMBERS:
raise ValueError("chamber must be one of: %s" % ", ".join(CHAMBERS.keys()))
self.title = CHAMBERS[chamber]
def test_record(self, record):
return record["title"] == self.title
if __name__ == "__main__":
histogram = stats.Histogram('state')
with open("legislators.csv") as infile:
saucebrush.run_recipe(
sources.CSVSource(infile),
InOfficeFilter(),
ChamberFilter("House"),
# ChamberFilter("Senate"),
StateFilter(STATES),
# StateFilter(TERRITORIES),
histogram,
emitters.CountEmitter(),
)
print histogram
434
AK *
AL *******
AR ****
AZ *********
CA *****************************************************
CO *******
CT *****
DC *
DE *
FL ***************************
GA **************
HI **
IA ****
ID **
IL *****************
IN *********
KS ****
KY ******
LA ******
MA *********
MD ********
ME **
MI **************
MN ********
MO ********
MS ****
MT *
NC *************
ND *
NE ***
NH **
NJ ************
NM ***
NV ****
NY ***************************
OH ****************
OK *****
OR *****
PA ******************
RI **
SC ******
SD *
TN *********
TX ************************************
UT ****
VA ***********
VT *
WA **********
WI ********
WV ***
WY *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment