Skip to content

Instantly share code, notes, and snippets.

View cwvh's full-sized avatar

Chris Van Horne cwvh

  • Seattle, WA, USA
View GitHub Profile
#!/usr/bin/env python
#
# Copyright 2009 Chris Van Horne
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
from django.contrib import admin
from foobar import models
class CommonSearchCriteria(admin.ModelAdmin):
search_fields = ('title',)
class AdvancedOptions(admin.ModelAdmin):
fieldsets = (
('Advanced options', {
'classes': ('collapse',),
(setq frame-title-format (concat "%b - emacs@" system-name))
(setq inhibit-startup-screen t)
(setq make-backup-files nil)
(setq require-final-newline t)
(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)
(fset 'yes-or-no-p 'y-or-n-p)
(setq-default indent-tabs-mode nil)
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/* IEEE-754 values taken from page 2 of:
* "Introduction to Floating point calculations and IEEE 754 standard"
* Jamil Khatib, August 10, 2000
*/
typedef uint32_t uint;
@cwvh
cwvh / images2gif.py
Created December 1, 2011 04:01
Take a list of PIL-supported images and turn them into a gif meme.
# -*- coding: utf-8 -*-
# Copyright (c) 2010, Almar Klein, Ant1, Marius van Voorden
#
# This code is subject to the (new) BSD license:
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
@cwvh
cwvh / scopelift.rb
Created December 2, 2011 07:04
The Hoarder Pattern: beautiful on the outside, dead kittens on the inside. Thanks Sinatra!
## Making kick-ass global objects for fun-and-profit DSLs.
module ScopeLifting
class Application
attr_accessor :options
def initialize
@options = Hash.new
end
@cwvh
cwvh / namedmatch.py
Created December 9, 2011 03:00
Clean code with better named captures.
"""`namedmatch` mimics `re.match` but also creates attributes based on the
values of the named captures.
Example
>>> string = 'foo bar baz'
>>> match = namedmatch(r'(?P<start>\w+) (?P<end>\w+)', string)
>>> print match.start
"foo"
>>> print match.end
@cwvh
cwvh / game.py
Created December 9, 2011 05:51
Example of asymmetric and symmetric multimethods.
from mm import multimethod, symmultimethod
class Ship(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
class Asteroid(object):
def __init__(self, name):
@cwvh
cwvh / countingbloom.py
Created December 9, 2011 23:04
Simple counting bloom filter in Python.
def hashfn(item):
h = hash(item)
return (1 << (h%64)) | (1 << (h/64%64))
def mask(val):
return bin(hashfn(val))[2:]
class CountingBloom(object):
def __init__(self):
self.items = [0] * 64
@cwvh
cwvh / parse.py
Created December 10, 2011 02:26
Simple parsing with pretty decorators.
import os
import re
class Env(object):
"""Maintains the environment variables for local and global scope."""
def __init__(self, env={}, parent=os.environ):
self.env = env.copy()
self.parent = parent.copy()