This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'rubygems' | |
require 'json' | |
require 'yaml' | |
require 'rest-client' | |
require 'active_support' | |
class Pullr | |
ORGANIZATION = 'ImpactData' | |
REPO = 'Squawkbox' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |