Skip to content

Instantly share code, notes, and snippets.

Avatar

Chris Van Horne cwvh

  • Seattle, WA, USA
View GitHub Profile
View __init__.py
#!/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
#
View admin.py
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',),
View gist:268448
(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)
View gist:519799
#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 Dec 1, 2011
Take a list of PIL-supported images and turn them into a gif meme.
View images2gif.py
# -*- 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 Dec 2, 2011
The Hoarder Pattern: beautiful on the outside, dead kittens on the inside. Thanks Sinatra!
View scopelift.rb
## 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 Dec 9, 2011
Clean code with better named captures.
View namedmatch.py
"""`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 Dec 9, 2011
Example of asymmetric and symmetric multimethods.
View game.py
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 Dec 9, 2011
Simple counting bloom filter in Python.
View countingbloom.py
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 Dec 10, 2011
Simple parsing with pretty decorators.
View parse.py
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()