kbjctw
Last Updated: February 25, 2016
·
792
· scott2b
Scott bradley 300x450

How I am using Fech (the FEC filing data fetcher & parser Ruby library) in a Python project

Fech, from the New York Times, is a nice Ruby library for accessing FEC (Federal Election Commission) filing data. I am working on a project in Python that requires the ability to download and parse FEC filing data. Here is how I am using Fech in a Python project to build a dictionary of summary and contribution data for a filing.

In file filing.rb:

require "fech"
require "json"

filing = Fech::Filing.new(ARGV[0])
filing.download
data = {
    "summary" => filing.summary,
    "contributions" => filing.rows_like(/^sa/)
}
puts data.to_json

In my python file:

import json
import os
import subprocess

def fetch_filing(fileno):
    """Assumes filing.rb is in same directory as this file."""
    fechpath = os.path.join(os.path.dirname(__file__), 'filing.rb')
    p = subprocess.Popen(['ruby', '-rubygems', fechpath, fileno],
        stdout=subprocess.PIPE)
    output = p.communicate()
    return json.loads(output[0])

A call to fetch_filing with a filing number returns a dictionary with the keys summary and contributions.

Fech is nicely done. This subprocess approach might not be the most elegant code in the world, but it is saving me from writing a bunch of parsing code and re-inventing the wheel, and most importantly, I can keep the project moving forward with FEC filing fetching and parsing being a solved problem, thanks to the team at the NYTimes. Check out Fech on github: https://github.com/NYTimes/Fech.

Say Thanks
Respond