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 / 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 / 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 / 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;
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 / 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:
{-# 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
{-# 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
@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
@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
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):