Skip to content

Instantly share code, notes, and snippets.

@bsmithgall
bsmithgall / README.md
Created October 17, 2013 23:13
Congress
@bsmithgall
bsmithgall / top_stations.R
Created November 22, 2013 05:47
Original version of top_stations.py from github.com/bsmithgall/citibike
library('RCurl')
library('RJSONIO')
library('igraph')
stations.url <- getURL('http://citibikenyc.com/stations/json')
dist.url <- getURL('appservices.citibikenyc.com/data2/stations.php')
# prep stations for kmeans clustering
stations.prep <- function(url) {
stations.json2 <- fromJSON(url, method='C')
import datetime
import requests
import json
BASE_URL = 'http://charts.spotify.com/api/charts/most_streamed/global/%s'
first_date = datetime.date(2013,04,28)
data = {}
for i in xrange(50):
@bsmithgall
bsmithgall / get_members.py
Created February 5, 2014 05:42
Neighboring Districts with Metadata
# get congress members from govtrack api, parse for use in d3 topojson map
import requests
import json
GOVTRACK_MEMBER_LIST_URL = 'https://www.govtrack.us/api/v2/role?current=true&limit=541'
raw_data = requests.get(GOVTRACK_MEMBER_LIST_URL).json()
STATELOOKUP = {'AL': 1,'AK': 2,'AZ': 4,'AR': 5,'CA': 6,'CO': 8,'CT': 9,'DE': 10,'DC': 11,'FL': 12,'GA': 13,'HI': 15,'ID': 16,'IL': 17,'IN': 18,'IA': 19,'KS': 20,'KY': 21,'LA': 22,'ME': 23,'MD': 24,'MA': 25,'MI': 26,'MN': 27,'MS': 28,'MO': 29,'MT': 30,'NE': 31,'NV': 32,'NH': 33,'NJ': 34,'NM': 35,'NY': 36,'NC': 37,'ND': 38,'OH': 39,'OK': 40,'OR': 41,'PA': 42,'RI': 44,'SC': 45,'SD': 46,'TN': 47,'TX': 48,'UT': 49,'VT': 50,'VA': 51,'WA': 53,'WV': 54,'WI': 55,'WY': 56,'AS': 60,'FM': 64,'GU': 66,'MH': 68,'MP': 69,'PW': 70,'PR': 72,'UM': 74,'VI': 78}
@bsmithgall
bsmithgall / qgis_add_features.py
Last active September 4, 2015 04:32
Updating Feature Attributes in QGIS
# To avoid 'QVariant' is not defined error
from PyQt4.QtCore import *
import json
# Get all layers on the map's canvas
canvas = qgis.utils.iface.mapCanvas()
all_layers = canvas.layers()
# Make dictionary of election district features (individual
# districts) for inserting into spatial index
@bsmithgall
bsmithgall / 201402_citibike_trips_rels.json
Created April 27, 2014 22:31
Citibike Network Visualization Using Trips
This file has been truncated, but you can view the full file.
{"216": [{"station_lon": "-73.993915", "end_station": "442", "station_lat": "40.746647", "start_station": "216", "end_station_name": "W 27 St & 7 Ave", "trips": 1}, {"station_lon": "-73.99595065", "end_station": "406", "station_lat": "40.69512845", "start_station": "216", "end_station_name": "Hicks St & Montague St", "trips": 8}, {"station_lon": "-73.98963911", "end_station": "233", "station_lat": "40.69246277", "start_station": "216", "end_station_name": "Joralemon St & Adams St", "trips": 4}, {"station_lon": "-73.98329859", "end_station": "349", "station_lat": "40.71850211", "start_station": "216", "end_station_name": "Rivington St & Ridge St", "trips": 1}, {"station_lon": "-73.99422366", "end_station": "412", "station_lat": "40.7158155", "start_station": "216", "end_station_name": "Forsyth St & Canal St", "trips": 1}, {"station_lon": "-73.963198", "end_station": "2002", "station_lat": "40.716887", "start_station": "216", "end_station_name": "Wythe Ave & Metropolitan Ave", "trips": 1}, {"station_lon": "-73.
@bsmithgall
bsmithgall / inflation_calculator.py
Created July 6, 2014 21:55
Value of a 2014 Dollar over Time
import requests
from bs4 import BeautifulSoup
URL = "http://data.bls.gov/cgi-bin/cpicalc.pl?cost1=1.00&year1={year}&year2=2014"
# input a range of years here. The first valid year is 1913.
year_range = xrange(1939, 2014)
print [BeautifulSoup(requests.get(URL.format(year=i)).text).find('span', {'id': 'answer'}).text for i in year_range]
@bsmithgall
bsmithgall / lobsta.csv
Created July 6, 2014 22:13
Maine Lobster Sales, not in PDF
We can make this file beautiful and searchable if this error is corrected: It looks like row 9 should actually have 13 columns, instead of 7. in line 8.
YEAR,SPECIES,METRIC TONS,POUNDS,POUNDS (millions),VALUE,VALUE (millions),PRICE/LB,VALUE OF ONE 2014 DOLLAR (bls),PRICE (adj.),NUMBER OF LICENSE HOLDERS ,NUMBER OF TRAPS - MILLIONS,WATER TEMP AT BOOTHBAY HARBOR
1939,"LOBSTER, AMERICAN","3,005","6,624,823",6.6248,"$1,000,000.00",1,0.15,17.12,$2.57,"3,722",0.26,6.4
1940,"LOBSTER, AMERICAN","3,468","7,644,735",7.6447,"$1,300,000.00",1.3,0.17,16.99,$2.89,"3,717",0.222,7
1941,"LOBSTER, AMERICAN","4,055","8,939,070",8.9391,"$1,600,000.00",1.6,0.18,16.18,$2.91,"3,648",0.194,7.9
1942,"LOBSTER, AMERICAN","3,813","8,405,460",8.4055,"$1,800,000.00",1.8,0.21,14.6,$3.07,"3,511",0.187,8.1
1943,"LOBSTER, AMERICAN","5,203","11,470,410",11.4704,"$2,900,000.00",2.9,0.25,13.75,$3.44,"4,239",0.209,7.4
1944,"LOBSTER, AMERICAN","6,377","14,059,080",14.0591,"$4,000,000.00",4,0.28,13.52,$3.79,"4,926",0.252,8
1945,"LOBSTER, AMERICAN","8,678","19,132,785",19.1328,"$7,700,000.00",7.7,0.4,13.22,$5.29,"6,241",0.378,8.4
1946,"LOBSTER, AMERICAN","8,518","18,779,985",18.78,"$7,200,000.00",7
class PaperTowel:
def __init__(self, input='paper towel'):
if isinstance(input, str):
self.output = sum([ord(i) for i in input])
elif isinstance(input, int):
self.output = input
else:
raise Exception("that's not right!")
print type(PaperTowel().output)
@bsmithgall
bsmithgall / wavepot.js
Created September 18, 2014 03:55
Wavepot (wavepot.com) WIP
var bpm = 240; // beats per minute
var spb = 60/bpm; // seconds per beat
// gets note `n` frequency of `octave`
function note(n, octave){
n += 2;
return Math.pow(2, (n - 33 + (12 * (octave || 0))) / 12) * 440;
}
// random noise