Skip to content

Instantly share code, notes, and snippets.

@stucchio
stucchio / gist:630918
Created October 17, 2010 14:56
Coroutine paren matcher
def matchparen():
ct = 0
msg = None
result = None
while (msg != True):
msg = (yield ct)
if msg == '(':
ct += 1
@stucchio
stucchio / gist:630921
Created October 17, 2010 15:00
Python coroutines
def cr():
state = False
for i in range(100):
new_state = None
if state:
new_state = (yield i)
else:
new_state = (yield "Off")
if not (new_state is None):
state = new_state
@stucchio
stucchio / gist:630939
Created October 17, 2010 15:24
Inverse Image pattern (remarkably useful)
def inv_image(f, collection):
"""Returns the inverse image (in the mathematical sense)
of the function applied to the collection.
"""
result = {}
for x in collection:
img = f(x)
if result.has_key(img):
result[img].add(x)
else:
@stucchio
stucchio / gist:715461
Created November 25, 2010 14:30
One fabric command, multiple roles
#Must be run with role foo, since foo has sudo access or other permissions
@hosts(*env.roledefs['foo'])
def do_foo_actions():
sudo("mkdir /usr/local/program")
sudo("chown -R bar:bar /usr/local/program")
#Most of the work here is done in role bar. But the folder /usr/local/program must exist before we can deploy
@hosts(*env.roledefs['bar'])
def deploy_program():
for h in env.roledefs['foo']:
@stucchio
stucchio / clone-ubuntu.sh
Created December 16, 2010 14:01
Clone a Virtualbox VM
#!/bin/sh
old=$1
new=$2
if [ $old ] && [ $new ]
then
echo "Cloning $old, saving result as $new"
else
echo "Usage: clone-ubuntu.sh oldname newname"
@stucchio
stucchio / calories.py
Created January 2, 2011 16:45
Inanity of Overeating - graphs
#!/usr/local/bin/python
from pylab import *
def bmr(height, weight, age):
"""Height in inches, weight in pounds, age in years.
Computes base metabolic rate for Harris Benedict Equation.
"""
return 66 + 6.23*weight + 12.7*height - 6.8 *age
@stucchio
stucchio / gist:837140
Created February 21, 2011 14:43
source code for graph in blog
#Created graph used here: http://crazybear.posterous.com/structural-shift-in-the-economy
import numpy.numarray as na
from pylab import *
labels = ["Financial Activites",
"Construction",
"Information Services",
"Retail",
@stucchio
stucchio / UUIDWritable.java
Created March 7, 2011 03:25
UUID Writable object - storage is half the size of storing UUID's as strings
import org.apache.hadoop.io.*;
import java.util.*;
import java.io.*;
public class UUIDWritable implements WritableComparable<UUIDWritable> {
private UUID value;
public UUIDWritable(long mostSignificantBits, long leastSignificantBits) {
value = new UUID(mostSignificantBits, leastSignificantBits);
@stucchio
stucchio / UUIDToDoubleMapWritable.java
Created March 25, 2011 23:14
A specialized map to save overhead in hadoop
package stylewok.utils.writable;
import org.apache.hadoop.io.*;
import java.util.*;
import java.io.*;
public class UUIDToDoubleMapWritable extends HashMap<UUIDWritable,Double> implements Writable {
public UUIDToDoubleMapWritable() { }
@stucchio
stucchio / employment_vs_investment.py
Created March 31, 2011 18:15
Employment vs investment
from pylab import *
from numpy import *
import matplotlib.cbook as cbook
from datetime import datetime
from scipy.interpolate import interp1d
def read_file(filename):
dates = []
values = []
for l in open(filename).readlines():