Skip to content

Instantly share code, notes, and snippets.

@danielhfrank
danielhfrank / arg_default_dict.py
Created January 22, 2013 21:59
Extend `collections.defaultdict` to actually be useful and call its `default_factory` with the missing key as an argument. Essentially just a concise way of memoizing the `default_factory` function.
from collections import defaultdict
class ArgDefaultDict(defaultdict):
def __missing__(self, key):
self[key] = self.default_factory(key)
return self[key]
@danielhfrank
danielhfrank / shred.go
Created July 10, 2012 21:34
Go Diamond Problem
package main
// Seems like this could be problematic when extending a type in a 3rd party module...
import (
"fmt"
)
type Shredder interface {
Shred()
@danielhfrank
danielhfrank / mc_lock.py
Created December 8, 2011 22:51
MC Lock Decorator
#====== Decorator to use memcached for locking =======
def mc_lock(namespace, on_lock_failed) :
'''
This uses memcached to provide a lock on a key before performing some operation.
The first argument provided to the decorated function will be the key.
It will be preceded by $namespace to ensure uniqueness
on_lock_failed is a function that will be called if we fail to acquire the lock
'''
@danielhfrank
danielhfrank / nycnames.py
Created October 26, 2011 23:47
Code for GA Data Science class
#!/usr/bin/env python
# encoding: utf-8
"""
untitled.py
Created by Daniel Frank on 2011-10-24.
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
"""
import sys