Skip to content

Instantly share code, notes, and snippets.

View gavinwahl's full-sized avatar

Gavin Wahl gavinwahl

  • Fusionbox
  • Colorado
View GitHub Profile
expand (Plus a) = foldr interleave [] $ do
match <- expand a
return $ match:(map (match++) (expand (Plus a)))
expand (Plus a) = (concat . ap (iterate . liftM2 (++) . expand) expand) a
@gavinwahl
gavinwahl / gist:1244417
Created September 27, 2011 05:43
relation algebra
(define (zip-cons a b)
(if (null? a)
'()
(cons (cons (car a) (car b))
(zip-cons (cdr a) (cdr b)))))
(define (p c r)
(cond
((null? c) (map (lambda (x) '()) r))
((eq? (car c) (caar r))
@gavinwahl
gavinwahl / tags.py
Created September 28, 2011 19:34
tmb and div nodes
from django import template
register = template.Library()
class ParseClassesNode(template.Node):
def parse_until(self, parser, token, end):
contents = token.split_contents()
self.classes = ' '.join(contents[1:])
self.nodelist = parser.parse((end,))
parser.delete_first_token()
@gavinwahl
gavinwahl / commit-msg
Created December 7, 2011 23:54
commit-msg hook that checks Verheoff checksums
#! /usr/bin/env python
# Verhoeff implementation from Hermann Himmelbauer,
# http://en.wikibooks.org/wiki/Algorithm_Implementation/Checksums/Verhoeff_Algorithm#Python
verhoeff_table_d = (
(0,1,2,3,4,5,6,7,8,9),
(1,2,3,4,0,6,7,8,9,5),
(2,3,4,0,1,7,8,9,5,6),
(3,4,0,1,2,8,9,5,6,7),
@gavinwahl
gavinwahl / gist:3768115
Created September 22, 2012 22:44
simple python-like expressions
import ast
import operator
ops = {
ast.Add: operator.add,
ast.Mult: operator.mul,
ast.Sub: operator.sub,
ast.Div: operator.truediv,
ast.FloorDiv: operator.floordiv,
<html>
<body>
<div>
<input type=file id="the_file">
<button id="prev" onclick="goPrevious()">Previous</button>
<button id="next" onclick="goNext()">Next</button>
&nbsp; &nbsp;
<span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
</div>
@gavinwahl
gavinwahl / python.snippets
Created November 8, 2012 20:12
python super call snippet
snippet sup
super(`GetCurrentPythonClass()`, `GetFirstPythonArg()`).`GetCurrentPythonMethod()`(${1:`GetCurrentPythonArgs()`})${2}
@gavinwahl
gavinwahl / base.py
Created April 10, 2013 20:22
`Resource` class to make django-rest-framework less tedious.
class Resource(object):
class Base(object):
pass
class List(generics.ListCreateAPIView):
pass
class Detail(generics.RetrieveUpdateDestroyAPIView):
pass
@gavinwahl
gavinwahl / tags.py
Created May 8, 2013 23:00
`map` template tag
import uuid
from django import template
from django.template.base import Token, TOKEN_BLOCK
register = template.Library()
class MapNode(template.Node):
def __init__(self, var_name, tag, list):
@gavinwahl
gavinwahl / mixins.py
Created June 2, 2013 18:18
DecoratorMixin is a mixin factory that converts any view decorator into a class-based view mixin.
def DecoratorMixin(decorator):
"""
Converts a decorator written for a function view into a mixin for a
class-based view.
::
LoginRequiredMixin = DecoratorMixin(login_required)
class MyView(LoginRequiredMixin):