Skip to content

Instantly share code, notes, and snippets.

@stephenmcd
stephenmcd / gist:311723
Created February 23, 2010 01:00
Formset Forms for Django
from copy import copy
from itertools import dropwhile, takewhile
from re import match
from django import template
from django.utils.datastructures import SortedDict
from django.utils.translation import ugettext as _
@stephenmcd
stephenmcd / ghetto_life_stream.php
Created April 16, 2010 06:46
Format different entry types from Google Buzz
<?
/**
* ghetto life stream script for http://jupo.org
*
* The main function of this script is to use the feed provided by Google Buzz
* to create a life stream of entries from various sources with specific
* handling for each different type of source. Sources are either directly
* integrated into Google Buzz such as Twitter and Flickr, or are subscribed to
* via Google Reader which when shared are then sent to Google Buzz. So far the
@stephenmcd
stephenmcd / create_test_products.py
Created April 22, 2010 23:40
Concurrent product data generation from Google Base / Flickr
"""
Stand-alone data generation routine that uses the ecommerce taxonomy found on
Google Base to generate a significant amount of category and product data, as
well as using the Flickr API to retrieve images for the products. The
multiprocessing module is also used for parallelization.
The Django models and environment used here are specific to the Cartridge
project but the approach could easily be reused with any ecommerce database.
"""
@stephenmcd
stephenmcd / gist:381981
Created April 28, 2010 10:39
Auto unique slugs for Django models
def save(self, *args, **kwargs):
"""
Create a unique slug from the title by appending an index.
"""
if self.id is None:
self.slug = slugify(self.title)
i = 0
while True:
if i > 0:
self.slug = "%s-%s" % (self.slug, i)
@stephenmcd
stephenmcd / changelog.py
Created July 12, 2010 22:03
Extract a RST changelog from a HG repo
#!/usr/bin/env python
"""
Use a mercurial repository to generate a reStructuredText changelog.
"""
from datetime import datetime
from mercurial import ui, hg
from django.utils.datastructures import SortedDict
@stephenmcd
stephenmcd / sloc.py
Last active September 5, 2015 10:54
SLOC and comment stats
import os, sys
c_like = {"single": "//", "multi_start": ("/*",), "multi_end": ("*/",),}
langs = {
"py": {"single": "#", "multi_start": ("'''", '"""'),
"multi_end": ("'''", '"""'),},
"html": {"single": "//", "multi_start": ("<!--", "/*"),
"multi_end": ("-->", "*/"),},
"js": c_like,
@stephenmcd
stephenmcd / nonblocking.py
Created December 31, 2010 23:29
Threaded function decorator
import functools
import thread
def nonblocking(func):
"""
Decorator that runs the given func in a separate thread
when called, eg::
@nonblocking
def some_blocking_func():
@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'
@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 / 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