Skip to content

Instantly share code, notes, and snippets.

View natekupp's full-sized avatar

Nate Kupp natekupp

View GitHub Profile
@natekupp
natekupp / gist:1763661
Created February 8, 2012 00:55
Python B-Trees
class BTreeNode(object):
"""A B-Tree Node.
attributes
=====================
leaf : boolean, determines whether this node is a leaf.
keys : list, a list of keys internal to this node
c : list, a list of children of this node
"""
def __init__(self, leaf=False):
@natekupp
natekupp / gist:1072750
Created July 8, 2011 20:37
Identifying the list indices of duplicate elements in a list
dups = [x for x in myList if myList.count(x) > 1]
indices = [i for i, x in enumerate(myList) if x in dups]
@natekupp
natekupp / gist:1010281
Created June 6, 2011 13:43
Use Case for Python Decorators
def timeout(seconds_before_timeout):
def decorate(f):
def handler(signum, frame):
raise TimeoutError()
def new_f(*args, **kwargs):
old = signal.signal(signal.SIGALRM, handler)
signal.alarm(seconds_before_timeout)
try:
result = f(*args, **kwargs)
finally: