Skip to content

Instantly share code, notes, and snippets.

@stober
stober / sparse_error.py
Created August 15, 2012 20:33
Sparse Matrix Failure
#! /usr/bin/env python
"""
Author: Jeremy M. Stober
Program: ERROR.PY
Date: Thursday, August 2 2012
Description: Example of how sparse matrix operations may fail.
"""
@stober
stober / Bash.sublime-build
Created August 15, 2012 20:31
Build Bash Script in Sublime Text
{
"cmd": ["bash $file"],
"shell": true,
"selector": "source.shell"
}
@stober
stober / average.py
Created July 13, 2012 22:00
A generator for incremental averaging in Python
def consumer(func):
"""
See: http://www.dabeaz.com/generators/
"""
def start(*args,**kwargs):
c = func(*args,**kwargs)
c.next()
return c
return start
@stober
stober / perceptron.py
Created June 15, 2012 21:13
A simple perceptron.
#!/lusr/bin/python
"""
Author: Jeremy M. Stober
Program: PERCEPTRON.PY
Date: Friday, April 4 2008
Description: A simple Perceptron implementation.
"""
import os, sys, getopt, pdb
from numpy import *
@stober
stober / multilayer.py
Created June 15, 2012 21:12
A simple multilayer backprop network.
#!/usr/bin/python
"""
Author: Jeremy M. Stober
Program: MULTILAYER.PY
Date: Monday, April 14 2008
Description: A simple implementation of a feed-forward multilayer network with back-propagation.
"""
import os, sys, getopt, pdb
from numpy import *
@stober
stober / axline.py
Created May 24, 2012 00:25
Axis Lines in Matplotlib
#! /usr/bin/env python
"""
Author: Jeremy M. Stober
Program: AXLINE.PY
Date: Wednesday, May 23 2012
Description: Plot arbitrary lines.
"""
import pylab
@stober
stober / pqueue.py
Created May 18, 2012 23:41
Priority queue with variable tie breaking in Python
import heapq as hq
import itertools
class pqueue:
"""
A priority queue with fast member checking and variable tie
breaking (LIFO or FIFO). Updated using the recipe from the heapq
documentation.
"""
@stober
stober / ploaditer.py
Created May 2, 2012 19:47
Generator for Multiple Pickle Loads
import pickle
def loader(fp):
# a load iter
while True:
try:
yield pickle.load(fp)
except EOFError:
raise StopIteration
@stober
stober / load_or_compute.py
Created March 23, 2012 20:33
Pickle Expensive Computations
import cPickle as pickle
import bz2
def load_or_compute(method, filename, recompute=False):
try:
if recompute: raise Exception
fp = bz2.BZ2File(filename)
return pickle.load(fp)
fp.close()
@stober
stober / save_show.py
Created March 9, 2012 21:50
Pylab Save/Plot Decorator
import matplotlib.pyplot as plt
def save_show_no(plotfunc):
# A function decorator that adds the option to save or show a plot
# depending on whether a filename option is set.
def decorate(*args,**kwargs):
ax = plotfunc(*args)