Skip to content

Instantly share code, notes, and snippets.

View markwatson's full-sized avatar
💻
hacking the mainframe

Mark Watson markwatson

💻
hacking the mainframe
View GitHub Profile
@markwatson
markwatson / stats.py
Created May 3, 2012 20:37
Random stats functions
def mean(x): return sum(x) / len(x)
def std_dev(x, mx=None):
if mx is None:
mx = mean(x)
return math.sqrt(sum((i - mx)**2 for i in x)/len(x))
@markwatson
markwatson / coroutine_io.py
Created April 26, 2012 19:07
A python IO class that writes to a coroutine.
import io
class CoroutineIO(io.TextIOBase):
"""
Creates an writable IO interface to a coroutine.
"""
def __init__(self, coroutine):
"""
Creates a new IO object with a coroutine. The
coroutine should take no arguments.
@markwatson
markwatson / iter_utils.py
Created April 13, 2012 19:39
Random python functions for working with iterators.
"""
A number of functions for working with iterators.
"""
def all(iterable):
for element in iterable:
if not element:
return False
return True
@markwatson
markwatson / eq.py
Created March 22, 2012 17:59
Find out if all values in a sequence are equal.
def eq(*vals):
"""
Finds out if a bunch of values are equal to each other.
"""
if len(vals) == 0:
return False
else:
return reduce(lambda x, y: x if x == y else not x, vals) == vals[0]
@markwatson
markwatson / print_unique.py
Created March 8, 2012 18:29
A Python class that only prints unique files. Useful for debugging inner loops.
class PrintUnique(object):
"""
A class that keeps track of what it has printed to stdout and won't
print anything twice.
"""
printed = set()
@classmethod
def write(cls, s):
"""
@markwatson
markwatson / knapsack.scala
Created February 11, 2012 15:32
Branch and Bound Knapsack Solution
// Branch and bound knapsack problem
import collection.mutable.PriorityQueue
/** This class represents a thing we can put in the napsack.
*/
class Thing(wIn:Int, vIn:Int) extends Ordered[Thing] {
val w = wIn
val v = vIn
def v_w = v / w
override def toString = "w=%d, v=%d, v/w=%d".format(w, v, v_w)
@markwatson
markwatson / Base File.sublime-settings
Created November 8, 2011 17:27
Sublime user file settings
{
"caret_style": "phase",
"color_scheme": "Packages/Color Scheme - Default/Blackboard.tmTheme",
"default_line_ending": "unix",
"draw_white_space": "all",
"ensure_newline_at_eof_on_save": true,
"font_face": "ProggySquareTT",
"font_size": 12,
"line_padding_bottom": 1,
"rulers":
# Tested on Windows 7 and Python 2.7
# the code
def lagrangian_interpolate(samples):
"""
Takes some samples as a list of tuples and returns a function that's
a lagrangian interpolation of all the samples.
"""
X = 0 # the tuple index of the X variable in the samples
Y = 1 # the tuple index of the Y variable in the samples
def super_split(string, delim):
segment = ''
for c in string:
if c in delim:
yield segment
segment = ''
else:
segment += c
yield segment
import mechanize
from BeautifulSoup import BeautifulSoup
class Dmoz(object):
def __init__(self):
self.br = mechanize.Browser()
def get_page_urls(self, term):
result = self.br.open("http://www.dmoz.org/search?q="+term)
result_html = result.read()