Skip to content

Instantly share code, notes, and snippets.

@jsundram
jsundram / dict_timing.ipy
Last active August 29, 2015 14:01
Explore the relationship between Key Miss Rate and timing for Python dicts.
"""
Explore the relationship between Key Miss Rate and time.
i.e. if you are doing a lot of dictionary lookups, how few misses
do you need to get in order to use try/except vs methods without exceptions
run via $ ipython dict_timing.ipy
"""
def a(d, i):
@jsundram
jsundram / install_bokeh.sh
Created May 12, 2014 04:18
How I installed Bokeh without conda on OSX 10.8.x
pip install bokeh # Fail
# gevent requires libevent, although you'd never know it from the error message
brew install libevent
# configure: error: C compiler cannot create executables ???
# Update Command Line tools for XCode
# No love
# find this: http://stackoverflow.com/questions/13041525/osx-10-8-xcrun-no-such-file-or-directory
sudo mv /usr/bin/xcrun /usr/bin/xcrun.bak
sudo vim /usr/bin/xcrun # Replace contents with #!/bin/bash $@
@jsundram
jsundram / dot_map_links.txt
Created January 5, 2015 02:11
Resources for making dot maps
@jsundram
jsundram / parse_batlog.py
Last active August 29, 2015 14:19
Code to parse data file generated by @jradavenport's batlog (https://github.com/jradavenport/batlog) for the purposes of plotting/analysis.
import datetime
import dateutil.parser
import itertools
import json
import os
import pytz
import re
# For use with data generated from:
@jsundram
jsundram / runnable.py
Last active August 29, 2015 14:21
A context-manager wrapper for running external binaries. Great for hiding scripts in other languages. Possibly an antipattern, but it works.
class Wrapper(object):
"""Communicate with a program via stdin/stdout.
Wrap the communication in python so it "feels" like native code.
See use by Runner, below.
"""
def __init__(self, script):
self.scriptname = os.path.basename(script)
self.p = Popen([script], stdin=PIPE, stdout=PIPE, bufsize=1)
print("%s initialized" % self.scriptname)
{
"metadata": {
"name": "",
"signature": "sha256:fa014ff7b33faeb7067a121aa857dd54b41b9c8f17d6299ea5b8df7f93e98601"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
@jsundram
jsundram / compress.py
Created June 24, 2015 05:30
Totally insane little script I use for deploying single-folder static apps to viz.runningwithdata.com. Everything gets gzipped up and minified.
import boto
import gzip
import os
import shutil
import subprocess
from subprocess import Popen, PIPE
import sys
import tempfile
import json
@jsundram
jsundram / space.py
Last active August 29, 2015 14:24
You have a cartesian product of a countable number of countable sets and you want to index the elements of that cartesian product. The goal is to find explicit formulas to go either direction in this one-to-one mapping.
"""
Problem -- you have a bounded space defined by the cardinality of each dimension.
You want to:
a) Go from coordinates inside that space to an index of inside that space (map_point)
b) Reverse that (go from an index to a set of coordinates) (unmap_point)
"""
import operator
def get_multipliers(cardinalities):
@jsundram
jsundram / data.png
Last active August 29, 2015 14:27
What does a geek do when ordering pizza? Plots the price per square inch, of course.
data.png
@jsundram
jsundram / ladder.cxx
Created January 27, 2011 00:37
problem 1
int step(int curr, int steps, const int n)
{
curr += steps;
if (curr < n)
return step(curr, 1, n) + step(curr, 2, n);
if (curr == n)
return 1;
return 0;
}