Skip to content

Instantly share code, notes, and snippets.

View gavinwahl's full-sized avatar

Gavin Wahl gavinwahl

  • Fusionbox
  • Colorado
View GitHub Profile
@gavinwahl
gavinwahl / abcmodel.py
Created December 3, 2013 22:26
Abstract (PEP 3119) Django models.
from abc import ABCMeta, abstractmethod
class AbstractModelMeta(ABCMeta, type(models.Model)):
pass
class ABCModel(models.Model):
__metaclass__ = AbstractModelMeta
class Meta:
var group_reduce = function (dependentKey, key_func, reduce) {
return Ember.reduceComputed.call(null, dependentKey, {
initialValue: Ember.A,
initialize: function(initialValue, changeMeta, instanceMeta) {
instanceMeta.meta = new Ember.Map();
},
addedItem: function (accumulatedValue, obj, changeMeta, instanceMeta) {
var key = key_func(obj), reduced_object;
if (instanceMeta.meta.get(key) === undefined) {
@gavinwahl
gavinwahl / python_implementation
Last active June 16, 2024 07:42
Triggers to enforce referential integrity for foreign keys, if postgres didn't support them natively.
CREATE OR REPLACE FUNCTION check_fk_child() RETURNS trigger AS $$
DECLARE
fk_local TEXT := TG_ARGV[0];
parent_table TEXT := TG_ARGV[1];
fk_val INT;
is_valid BOOLEAN;
query TEXT;
BEGIN
-- fk_val = getattr(NEW, fk_local)
EXECUTE format('SELECT $1.%I', fk_local) USING NEW INTO fk_val;
@gavinwahl
gavinwahl / gist:5952850
Created July 8, 2013 21:59
consume_once
from itertools import tee
class consume_once(object):
"""
Takes an iterator and returns an iterable that can be consumed
multiple times while only consuming the original a single time.
>>> x = consume_once(i for i in range(3))
>>> list(x)
[0, 1, 2]
@gavinwahl
gavinwahl / split_up.py
Created June 3, 2013 20:36
Splits up a string into pages of a maximum length. Includes pages numbers. Tries not to split in the middle of a word. Good for SMS.
from math import ceil
def split_up(s, length=160):
"""
Splits the string s into pages of no longer than length. Includes
pages numbers. Tries not to split in the middle of a word.
>>> split_up("abc", 5)
['abc']
>>> split_up("abcdef", 5)
@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):
@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 / 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 / python.snippets
Created November 8, 2012 20:12
python super call snippet
snippet sup
super(`GetCurrentPythonClass()`, `GetFirstPythonArg()`).`GetCurrentPythonMethod()`(${1:`GetCurrentPythonArgs()`})${2}
<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>