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 / signals.py
Last active August 29, 2015 14:22
RealtimeSignalProcessor for widgy that understands making commits need to reindex their owners.
from django.db import models
from haystack.signals import RealtimeSignalProcessor
from widgy.models.versioning import VersionCommit
class WidgyRealtimeSignalProcessor(RealtimeSignalProcessor):
def handle_commit(self, sender, instance, **kwargs):
for owner in instance.tracker.owners:
self.handle_save(sender=type(owner), instance=owner, **kwargs)
{-# LANGUAGE GADTs, StandaloneDeriving, TypeOperators, ExistentialQuantification #-}
import Data.Type.Equality
data UTerm = UTrue
| UFalse
| UIf UTerm UTerm UTerm
| UZero
| USucc UTerm
| UIsZero UTerm
@gavinwahl
gavinwahl / example.sql
Last active August 29, 2015 14:19
Temporal foreign key
DROP TABLE IF EXISTS parent;
DROP TABLE IF EXISTS child;
CREATE TABLE parent(
identity_id integer,
valid_range int4range
);
CREATE TABLE child(
valid_range int4range,
@gavinwahl
gavinwahl / gist:ef4be04fadf0296f005f
Created April 1, 2015 21:44
Inheritance and composition are isomorphic
class Parent(object):
def __init__(self, x):
self.x = x
def frob(self):
self.x += 1
class Child1(Parent):
def __init__(self, x, y):
def terminal_split(items, is_terminal):
SENTINAL = object()
ret = [[]]
for item, next_item in izip_longest(items, items[1:], fillvalue=SENTINAL):
ret[-1].append(item)
if next_item is not SENTINAL and is_terminal(item) and not is_terminal(next_item):
ret.append([])
return ret
def is_terminal(x):
@gavinwahl
gavinwahl / gist:234e20b534745bf816a8
Last active August 29, 2015 14:05
Why not look up method implementations lazily?
diff --git a/django/utils/functional.py b/django/utils/functional.py
index c512084..11a5500 100644
--- a/django/utils/functional.py
+++ b/django/utils/functional.py
@@ -84,13 +84,14 @@ def lazy(func, *resultclasses):
called on the result of that function. The function is not evaluated
until one of the methods on the result is called.
"""
- __dispatch = None
+ __prepared = False
@gavinwahl
gavinwahl / gist:17c07335c8dd1b832911
Created July 29, 2014 23:18
CheckedAtBooleanField
class CheckedAtBooleanField(models.DateTimeField):
"""
A db field that acts like a checkbox in forms, but is backed by a
DateTimeField in the database. `None` represents that the box was
not checked, a timestamp value indicates that the box was checked
at that time. If the form is saved again, the time is not updated.
Specifically, the timestamp indicates when a user chose to opt in.
When setting the attribute on the model, it's possible to use the
{-# LANGUAGE RankNTypes #-}
(!) = flip (.)
lit :: a -> s -> (s, a)
lit x s = (s, x)
run :: (forall a. a -> (a, b)) -> b
run x = snd $ x undefined
{-# LANGUAGE RankNTypes #-}
type Lens t i = Functor f => (i -> f i) -> t -> f t
data Person = Person { _name :: String, _age :: Int } deriving Show
nameLens :: Lens Person String
nameLens f (Person name age) = fmap (\newName -> Person newName age) (f name)
ageLens :: Lens Person Int
@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: