Skip to content

Instantly share code, notes, and snippets.

@sontek
sontek / gist:1009793
Created June 6, 2011 05:49
Proper way to use django class based views
from django.views.generic.base import TemplateResponseMixin
from django.views.generic.base import View
class SomeFormView(TemplateResponseMixin, View):
template_name = 'some_form.html'
def get(self, request):
form = SomeForm()
return self.render_to_response({
@sontek
sontek / gist:1290480
Created October 16, 2011 03:41
Convert a mako file to jinja2 in Vim
:%s/${\([^}]*\)}/{{ \1 }}/g
:%s/% if\([^:]*\):/{% if\1 %}/g
:%s/% endif/{% endif %}/g
:%s/% else:/{% else %}/g
:%s/% for\([^:]*\):/{% for\1 %}/g
:%s/% endfor/{% endfor %}/g
@sontek
sontek / gist:1327060
Created October 31, 2011 07:11
Crazy ass ruby string interpolation by zedshaw
# Ruby style string interpolation with a giant bag of evil python.
import inspect
class __(str):
def __init__(self, other):
super(__, self).__init__(other)
def __invert__(self):
frame = inspect.currentframe()
@sontek
sontek / gist:1362947
Created November 14, 2011 00:06
Run gunicorn webserver and kill its workers when finished.
#!/bin/bash
# run if user hits control-c
control_c()
{
echo "Oh no, exiting!"
for pid in $(cat gunicorn.pid)
do
echo "killing $pid"
kill -SIGKILL $pid
done
@sontek
sontek / gist:1420255
Created December 1, 2011 22:09
Pyramid Example tests
import unittest
from pyramid import testing
from paste.deploy.loadwsgi import appconfig
from webtest import TestApp
from mock import Mock
from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
from app.db import Session
@sontek
sontek / snowjob.sh
Last active April 5, 2024 06:51
Make your terminal snow
#!/bin/bash
LINES=$(tput lines)
COLUMNS=$(tput cols)
declare -A snowflakes
declare -A lastflakes
clear
@sontek
sontek / snowjob.py
Created December 22, 2011 04:24
Make your terminal snow with python
#!/usr/bin/env python
import os
import random
import time
import platform
snowflakes = {}
try:
# Windows Support
@sontek
sontek / gist:1522303
Created December 26, 2011 23:53
Watch less files and generate css
#!/usr/bin/env bash
while :; do
find "$PWD" -name '*.less' | while read line; do
REPLACE=`echo $line | sed "s|\.less|\.css|"`
mt1=$(stat -c %Y $line)
mt2=$(stat -c %Y $REPLACE)
if [ "$mt1" -gt "$mt2" ]; then
@sontek
sontek / button.py
Created May 25, 2012 04:24 — forked from derpston/button.py
Python libusb interface for getting key up/down events from USB HIDs like keyboards.
import sys
import time
import usb
class Button:
def __init__(self, vendor_id, device_id):
"""
Find and open a USB HID device.
"""
@sontek
sontek / gist:3661788
Created September 7, 2012 00:18
Hogan.js 3.0
/*
* Copyright 2011 Twitter, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,