Skip to content

Instantly share code, notes, and snippets.

@stephenmcd
stephenmcd / persistent_test_session.py
Created January 30, 2012 04:45
Persistent Sessions in Django TestCase
"""
The Django test client implements the session API but doesn't persist values in it:
https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.client.Client.session
This Client subclass can be used to maintain a persistent session during test cases.
"""
from django.conf import settings
from django.test import TestCase
@stephenmcd
stephenmcd / declauser.py
Created January 21, 2012 02:16
Convert 3-clause BSD licenses to 2-clause in a directory of projects, commit and push to BitBucket and GitHub
#!/usr/bin/env python
import os
cwd = os.path.abspath(os.getcwd())
for project in os.listdir("."):
path = os.path.join(cwd, project)
if not os.path.isdir(path):
@stephenmcd
stephenmcd / tag_closer.py
Created November 18, 2011 20:07
HTMLParser that closes open tags
class TagCloser(HTMLParser):
"""
HTMLParser that closes open tags. Call close_tags with a HTML
string arg which returns it with any required closing tags
appended.
"""
def __init__(self):
HTMLParser.__init__(self)
self.tags = []
@stephenmcd
stephenmcd / gist:1374413
Created November 17, 2011 20:27
Sort object list by index of attribute in a keylist
def sort_by_keylist(objects, attr, keylist):
indexes = dict([(x, i) for i, x in enumerate(keylist)])
return sorted(objects, key=lambda x: indexes.get(getattr(x, attr)))
@stephenmcd
stephenmcd / gist:1341065
Created November 5, 2011 03:32
Trap every object method
class Foo(object):
def __getattribute__(self, name):
attr = object.__getattribute__(self, name)
if callable(attr):
def wrapper(*args, **kwargs):
try:
return attr(*args, **kwargs)
except Exception, e:
print "%s failed with exc %s" % (name, e)
@stephenmcd
stephenmcd / gist:1318777
Created October 27, 2011 04:29
Estimated project cost of a Mercurial repository
# A one-liner for calculating the cost of a project using a Mercurial repo.
# Replace:
# ignore|paths - pipe separated strings in paths to ignore
# eg: embedded libraries and generated code.
# mins_per_loc - estimated minutes per line of code.
# hourly_rate - hourly dollar rate being charged.
$ hg log --stat | egrep -v 'ignore|paths' | grep '|' | awk '{s+=$3} END {print "$" s / 60 * mins_per_loc * hourly_rate}'
@stephenmcd
stephenmcd / gist:1129135
Created August 6, 2011 07:28
Mini Mock
class MiniMock(object):
"""
A callable object that will always return a value for any
attribute/index/slice/call accessed. The value returned is a new
MiniMock instance allowing for successful chained access.
"""
def __getitem__(self, value):
"""
@stephenmcd
stephenmcd / gist:1050211
Created June 28, 2011 00:34
Safe concurrent saves for Django models
"""
With Django models, calling save() can have undesired consequences,
particularly in a concurrent environment such a Celery, where model
instances may be serialized across a message queue. The save() method
will write all fields to the database which may have already been
written to by another process or thread, and as such will override
fields incorrectly.
The mixin below can be used to safely update model instances without
overriding fields of no concern that may have been written to since
@stephenmcd
stephenmcd / jquery.squeezebox.js
Created May 11, 2011 05:20
Replacement for jquery.ui.accordion to avoid dealing with jquery.ui theming
// Replacement for jquery.ui.accordion to avoid dealing with
// jquery.ui theming.
//
// Usage: $('#container').squeezebox(options);
// where the direct child elements of '#container' are
// sequential pairs of header/panel elements, and options
// is an optional object with any of the following properties:
//
// activeHeaderClass: Class name to apply to the active header
// headerSelector: Selector for the header elements
@stephenmcd
stephenmcd / pullr.rb
Created May 4, 2011 03:12 — forked from benmacleod/pullr.rb
Directory aware version of Pullr
#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'yaml'
require 'rest-client'
require 'active_support'
class Pullr
ORGANIZATION = 'ImpactData'
REPO = 'Squawkbox'