Skip to content

Instantly share code, notes, and snippets.

View objcode's full-sized avatar

Sean McQuillan objcode

  • Mountain View, CA
View GitHub Profile
@objcode
objcode / fail.py
Created September 18, 2011 02:34
python fail
def shell(cmdline):
start = time.time()
outbuffer = StringIO.StringIO()
print "asked to shell", cmdline
subcommands = cmdline.split('|')
subcommands = [command.strip() for command in subcommands]
first, last, subcommands = subcommands[0], subcommands[-1], subcommands[1:-1]
@objcode
objcode / signal_timer.py
Created August 9, 2011 09:44
signal_timer context manager
import signal
import time
class TimerException(Exception): pass
from contextlib import contextmanager
@contextmanager
def signal_timer(mili):
import signal
class TimerException(Exception): pass
def timeout(signum, frame):
print "signal handler exception", signum
print frame
raise TimerException(frame)
signal.signal(signal.SIGALRM, timeout)
@objcode
objcode / sadgenerators.py
Created August 9, 2011 08:00
Python generators sadface
import functools
class generatorfunction(object):
"""
Apparently you can't monkeypatch generator objects for some reason.
Lets go ahead and proxy them instead to add __call__.
"""
def __init__(self, generator):
self.delegate = generator
class CachingInterface (object):
def __init__(self):
"To be overridden by the sub-class."
raise Exception("not implemented")
def store(key, value, type='post'):
"""
Store a key/value pair in the cache. Returns a Deferred.