Skip to content

Instantly share code, notes, and snippets.

@jhorman
jhorman / iptables.sh
Created April 22, 2011 18:29
iptables stuff
# Reject traffic out to a specific IP
iptables -o eth0 -I OUTPUT -d 10.207.47.148 -j REJECT
#!/bin/bash
launchctl setenv KEY value
@jhorman
jhorman / cache_decorator.py
Created March 29, 2011 02:45
Decorator for caching return values in an LRU. Supports deferred results as well.
def cache(lifetime=None, cache_size=0):
""" Caching decorator. """
def attach_caching(method):
method._cache = pylru.lrucache(cache_size) if cache_size else {}
method._cache_updated = {}
@functools.wraps(method)
def wrapper(*args, **kw):
# frozenset is used to ensure hashability
key = args, frozenset(kw.iteritems()) if kw else args
@jhorman
jhorman / twisted_timing.py
Created March 29, 2011 02:42
Times a method that may or may not return a deferred
def time_method(stat_name):
"""
Attaches a timeout to the deferred returned by the wrapped function.
After timeout seconds the deferred is canceled and an exception raised.
"""
def attach_timing(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
start = time()
@jhorman
jhorman / twisted_wrap_exception.py
Created March 29, 2011 02:41
Decorator that throws traps a twisted error and rethrows your app specific error.
def wrap_exception(handle, error_type, error_message, log=None):
def attach_check(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
d = method(self, *args, **kwargs)
def on_error(failure):
failure.trap(handle)
logger = logging if not log else logging.getLogger(log)
logger.error("%s, %s" % (error_message, failure.getErrorMessage()))
raise error_type(error_message)
@jhorman
jhorman / twisted_sleep.py
Created March 29, 2011 02:38
Simple non-blocking sleep in twisted.
def sleep(secs):
d = Deferred()
reactor.callLater(secs, d.callback, None)
return d
@jhorman
jhorman / twisted_timeout.py
Created March 29, 2011 02:35
Decorator for timing out a method that returns a twisted deferred.
def timeout(seconds=5, error_type=None, error_message=None):
"""
Attaches a timeout to the deferred returned by the wrapped function.
After timeout seconds the deferred is canceled and an exception raised.
"""
def attach_timeout(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
d = method(self, *args, **kwargs)
def on_timeout():

Initialize and sync from github

git init
git remote add origin git@github.com:something/something.git
git pull origin master

Make sure that git push pushes tags

git config --add remote.origin.push 'refs/tags/:refs/tags/'

@jhorman
jhorman / run_liquibase.sh
Created November 4, 2010 22:06
Running liquibase against a mysql db
#!/bin/sh
liquibase --driver=com.mysql.jdbc.Driver --url=jdbc:mysql://localhost/db --username=root --changeLogFile=changelog.xml $@