Skip to content

Instantly share code, notes, and snippets.

View brehaut's full-sized avatar

Andrew Brehaut brehaut

View GitHub Profile
@brehaut
brehaut / render.py
Last active December 21, 2015 23:49
simple views and contexts for django
from render import render
def some_context(request):
return {…}
def some_different_context(request):
return {…}
@render("template.html")
function monkeyPunch<A, B extends A>(o:A, extend:(o:B)=>void) {
var o2 = <B>Object.create(o);
extend(o2);
return o2;
}
interface Foo {
name: string;
}
@brehaut
brehaut / animals.ts
Created July 11, 2013 22:25
variance soundness in typescript
class Animal {
constructor(public name) {}
toString() { return this.name; }
}
class Dog extends Animal {
fetch (thing:any) {
alert("fetching: " + thing);
@brehaut
brehaut / gist:5880480
Last active December 19, 2015 02:08
generalized ridiculous greeting
(require '[clojure.string :as str])
(->> (map (comp rand-nth
(group-by first (seq (.split (slurp "/usr/share/dict/words") "\n")))
char)
[121 111 108 111])
(str/join " ")
print)
@brehaut
brehaut / typescriptalt.py
Last active December 18, 2015 22:38
Backporting the TypeScript filter to webassets 0.8 for use with tsc 0.9
"""This is an alternate typescript handler from future webassets as there
is a bug in the 0.8 version with recent tsc
"""
import os
import subprocess
import tempfile
from io import open # Give 2 and 3 use same newline behaviour.
from webassets.filter import Filter, register_filter
@brehaut
brehaut / adminthumbnail.py
Created September 25, 2012 02:43
A function for generating admin_thumbnail methods for the django admin using sorl
from sorl.thumbnail import get_thumbnail
def make_thumbnail_method(get_fn=lambda o:o.image,
dimensions="100x60",
crop="center"):
"""A function factory for producing admin_thumbnail methods.
"""
def admin_thumbnail(self):
image = get_fn(self)
@brehaut
brehaut / lambdamanager.py
Created September 25, 2012 02:41
A django manager that takes a function to filter a queryset
from django.db import models
class LambdaManager(models.Manager):
"""LambdaManager is a simple manager extension that is instantiated with a callable.
This callable is passed the query set by get_query_set so that it can perform any
additional transformations to it such as eg filtering.
"""
def __init__(self, f):
@brehaut
brehaut / on_drag.js
Created September 21, 2012 02:40
Trivial drag event registering function for jquery
function on_drag(el, handler) {
var el = $(el);
el.on("mousedown", function (e) {
var initial_x = e.clientY;
var initial_y = e.clientY;
function drag_handler(e) {
var delta_x = e.clientX - initial_x;
var delta_y = e.clientY - initial_y;
@brehaut
brehaut / ref.roy
Created August 29, 2012 01:55
mutable refs in Roy
// the first hack needed to support mutable refs in Roy is allowing in place edits
// to JS arrays
let aSet (arr:[#a]) (idx:Number) (v:#a) :[#a] =
Array.prototype.splice.call arr idx 1 v
arr
// wrapper type is currently a structural type rather than an ADT because I couldn't
// get ADTs to have an Array as a field
type Ref = {_val: [#a]}
@brehaut
brehaut / picopico.js
Created July 10, 2012 23:43
a minimal parser combinator lib hauled out of a project
// parser
var _parser_global_state = null;
function ParseFailed () {};
function run_parser(p, input_list) {
input_list = input_list.slice()
try {
var old_parser_input = _parser_global_state;