Skip to content

Instantly share code, notes, and snippets.

@sbirch
sbirch / ipython_logging.py
Created November 27, 2015 05:03
Printing output to the kernel console from IPython notebooks
from IPython import get_ipython
# Levels less than warn (info, debug, etc.) are surpressed in the console output.
get_ipython().kernel.log.warn("Hello, world!")
import pandas as pd
import numpy as np
import re
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
def load_grid(path):
f = open(path, 'r')
headers = None
@sbirch
sbirch / keepassXML_to_CSV.py
Created March 31, 2014 21:42
A script to convert KeepassX XML exports to a CSV as consumed by 1Password. Will read from keepass.xml and write to keepass.csv in current working directory.
import xml.etree.ElementTree as ET
import csv
from collections import defaultdict
# Note that 1Password is quite particular about the kind of escaping you use.
# The default Excel dialect blows it up & there is in fact no Python dialect
# corresponding to 1Password's requirements due to http://bugs.python.org/issue12178
output = csv.writer(open('keepass.csv', 'wb'), doublequote=False, escapechar='\\', quoting=csv.QUOTE_ALL)
dom = ET.parse('keepass.xml')
@sbirch
sbirch / convert.py
Created February 10, 2014 02:00
A quick script to convert from RecipeXML to text. YMMV.
import sys
import xml.etree.ElementTree as ET
import re, htmlentitydefs, textwrap
def wrap(text):
paragraphs = re.split('\n{2,}', text)
wrapped = []
for para in paragraphs:
para = re.sub('\s{2,}', ' ', para).strip()
wrapped.append('\n'.join(textwrap.wrap(para, 80)))
@sbirch
sbirch / data_dictionary.json
Created December 17, 2013 02:24
A copy of data_dictionary.json built from the 2003-2012 interview & CPS dictionaries.
{"TRTOHH_LN": {"files": ["Activity File"], "note": "TRTOHH_LN is calculated using TUCC5. It does not include activities with TRTIER2P=0101, 0301, 0302, 0303, or with TRCODEP=180381. It does not include any activity or part of any activity in which no household child was awake (determined by TUCC2 and TUCC4).", "validEntries": {"0": "Min Value", "1440": "Max Value"}, "description": "Total time spent during activity providing secondary childcare for own household children < 13 (in minutes)"}, "TRELHH": {"files": ["EC Roster File"], "note": "Questions about eldercare were introduced in January 2011.", "validEntries": {"1": "Recipient is a household member", "0": "Recipient is not a household member"}, "description": "Eldercare recipient is a household member"}, "PELAYLK": {"files": ["ATUS-CPS File"], "note": null, "validEntries": {"1": "Yes", "2": "No"}, "description": "Edited: even though you are to be called back to work, have you been looking for work during the last 4 weeks?"}, "PTDTRACE": {"files": ["ATUS-C
@sbirch
sbirch / activity_lexicon.json
Last active December 29, 2015 06:59
A copy of activity_lexicon.json built from the 2003-2012 activity lexicon.
{"13,2,16": "Watching hockey", "13,2,17": "Watching martial arts ", "13,2,14": "Watching golfing", "13,2,15": "Watching gymnastics", "13,2,12": "Watching fishing", "13,2,13": "Watching football", "13,2,10": "Watching equestrian sports", "13,2,11": "Watching fencing", "15,2,2": "Collecting & delivering clothing & other goods", "15,2,3": "Providing care", "1,4": "Personal Activities", "15,2,1": "Food preparation, presentation, clean-up", "1,2": "Grooming", "1,3": "Health-related self care", "13,2,18": "Watching racquet sports", "13,2,19": "Watching rodeo competitions", "5,4,99": "Job search and Interviewing, n.e.c.*", "15": "Volunteer Activities ", "3,1,99": "Caring for & helping hh children, n.e.c.*", "2,4,99": "Exterior maintenance, repair & decoration, n.e.c.*", "16,99": "Telephone Calls, n.e.c.*", "2,2,3": "Kitchen and food clean-up", "2,2,2": "Food presentation", "9,2,2": "Waiting associated w/ home main/repair/d\u00e9cor/constr", "9,2,1": "Using home maint/repair/d\u00e9cor/construction svcs", "7,1,3": "P
@sbirch
sbirch / underscorify.js
Created November 21, 2013 19:03
Underscorify bookmarklet (tested in Chrome)
javascript:(function(){var a=document.createElement("script");a.src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js",document.getElementsByTagName("head")[0].appendChild(a)})();
@sbirch
sbirch / classify.py
Last active December 27, 2015 23:19
Homework stencil for SVM / Brown CS195W
from svmutil import *
from os import devnull
import itertools
# Hello there!
# You should read the commentary in seven_steps_to_OK_performance, although
# you won't have to implement anything other than the data transformation
# (as it's somewhat tedious to go through the LibSVM docs, and they provide
# a similar module -- easy.py -- that does substantially the same thing
@sbirch
sbirch / Forbes400-2013.json
Created November 3, 2013 17:35
Forbes 400 from 2013, for Brown CS1950W assignment 6.
[{"rank":1,"name":"Bill Gates"},{"rank":2,"name":"Warren Buffett"},{"rank":3,"name":"Larry Ellison"},{"rank":4,"name":"Charles Koch"},{"rank":4,"name":"David Koch"},{"rank":6,"name":"Christy Walton & family"},{"rank":7,"name":"Jim Walton"},{"rank":8,"name":"Alice Walton"},{"rank":9,"name":"S. Robson Walton"},{"rank":10,"name":"Michael Bloomberg"},{"rank":11,"name":"Sheldon Adelson"},{"rank":12,"name":"Jeff Bezos"},{"rank":13,"name":"Larry Page"},{"rank":14,"name":"Sergey Brin"},{"rank":15,"name":"Forrest Mars, Jr."},{"rank":15,"name":"Jacqueline Mars"},{"rank":15,"name":"John Mars"},{"rank":18,"name":"Carl Icahn"},{"rank":19,"name":"George Soros"},{"rank":20,"name":"Mark Zuckerberg"},{"rank":21,"name":"Steve Ballmer"},{"rank":22,"name":"Len Blavatnik"},{"rank":23,"name":"Abigail Johnson"},{"rank":24,"name":"Phil Knight"},{"rank":25,"name":"Michael Dell"},{"rank":26,"name":"Paul Allen"},{"rank":27,"name":"Donald Bren"},{"rank":27,"name":"Ronald Perelman"},{"rank":29,"name":"Anne Cox Chambers"},{"rank":30,"name
@sbirch
sbirch / init.sh
Last active December 16, 2015 08:48
Quickstart on an Ubuntu image
echo "Making README..."
touch README
wget http://ipecho.net/plain -qO - >> README
echo -e "\n" >> README
echo "Updating packages..."
apt-get update
apt-get upgrade
echo "Installing default packages..."