Skip to content

Instantly share code, notes, and snippets.

@tkaemming
tkaemming / gist:1997894
Created March 8, 2012 01:39
left fold a list of dictionaries into one (overwriting earlier keys)
data = [{1:1, 2:2, 3:3}, {1:2, 4:4, 5:5}, {6:6, 7:7, 4:0}]
reduce(lambda x, y: dict(x, **y), data, {})
@tkaemming
tkaemming / example.py
Created March 8, 2012 01:25
python logging timer decorator
import logging, time
from timer import timed
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
@tkaemming
tkaemming / profiler-stats.py
Created March 5, 2012 21:04
simple cli for hotshot profiler stats output
#!/usr/bin/env python
"""
run `profile-stats -h` for help
example usage:
view all calls, ordered by internal time, call count (default): $ profiler-stats /path/to/log
view top 25 calls, ordered by internal time, call count: $ profiler-stats -n 25 /path/to/log
view top 50 calls, ordered by cumulative time: $ profiler-stats -s cumu -n 50 /path/to/log
view all calls with callers: $ profiler-stats -c /path/to/log
@tkaemming
tkaemming / decorators.py
Created March 1, 2012 22:06
django class-based view mixins
from django.utils.decorators import method_decorator
def view_class_decorator(decorator):
"""
Wraps a class based view dispatcher with the provided decorator.
Usage:
@view_class_decorator(login_required)
@mikeal
mikeal / gist:1840641
Created February 16, 2012 01:33
get a new/clean port with node.js
var portrange = 45032
function getPort (cb) {
var port = portrange
portrange += 1
var server = net.createServer()
server.listen(port, function (err) {
server.once('close', function () {
cb(port)
@tkaemming
tkaemming / flush.py
Created January 23, 2012 19:15
also flush cache, redis with flush management command
import django.core.management.commands.flush
from django.conf import settings
from django.core.cache import get_cache
from django.db.backends.creation import TEST_DATABASE_PREFIX
from django.db.models.signals import post_syncdb
from redis import Redis
def is_test_database(database):
@tkaemming
tkaemming / as.py
Created January 16, 2012 00:06
rendering django template blocks to a context variable
from django import template
register = template.Library()
@register.tag(name='as')
def as_variable(parser, token):
"""
Adds the rendered content of this block to the context, using the argument
as the key name.
@tkaemming
tkaemming / querytools.py
Created January 15, 2012 05:59
querystring template tag helpers for django
from copy import copy
from urllib import urlencode
from urlparse import parse_qs, urlparse, urlunparse
from django import template
register = template.Library()
@register.filter
def with_querystring(value, request):
@tkaemming
tkaemming / get.py
Created January 15, 2012 00:50
django template filter for retrieving a variable dict key
from django import template
register = template.Library()
@register.filter
def get(value, key):
"""
Returns the item of the `value` dictionary at `key`.
Usage: {{ somedict|get:somevar }}
@tkaemming
tkaemming / decorators.py
Created January 10, 2012 22:29
django class-based view class decorator
from django.utils.decorators import method_decorator
def view_class_decorator(decorator):
"""
Wraps a class based view dispatcher with the provided decorator.
Usage:
@view_class_decorator(login_required)