Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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 / cls_test.py
Created January 30, 2013 23:13
Methods not defined within class don't pass self as first arg
class X(object):
def __str__(self):
return 'x'
def my_print_arg(*args):
print args[0]
def print_arg(*args):
print args[0]
@danielhfrank
danielhfrank / unit_interval_scaler.py
Last active December 14, 2015 01:49
Scale to unit interval. Screw the mean and variance
class UnitIntervalScaler(object):
def fit(self, xs):
self._floor = np.min(xs)
self._range = np.max(xs) - self._floor
return self
def transform(self, x):
return (x - self._floor) / self._range
@danielhfrank
danielhfrank / doom.py
Created May 9, 2013 20:50
Attempt at a retry-with-backoff decorator. May it (and I) rest in peace
def retry_with_backoff(method):
"""
The idea was to use this to decorate a method that makes some kind of request and has a callback
kwarg. The callback will be decorated to retry the calling function a few times,
with backoff, if it receives an error.
"""
@functools.wraps(method)
def caller_wrapper(self, *args, **kwargs):
callback = kwargs['callback']
attempts = kwargs.pop('attempts', 0)
@danielhfrank
danielhfrank / passthrough_errors.py
Created June 18, 2013 21:36
This decorator is intended to simplify error handling in request cycles with many callbacks. The idea is to decorate a callback function that takes its main "data" as a first positional argument, and a callback as a kwarg called "callback". The decorator will inspect the data argument and attempt to determine if it is an error (either a tornado…
def passthrough_errors(method):
@functools.wraps(method)
def wrapper(*args, **kwargs):
callback = kwargs['callback']
data = args[0]
# Check if an error is being passed in, somehow
error = None
if isinstance(data, tornado.httpclient.HTTPResponse) and data.error:
error = data.error
@danielhfrank
danielhfrank / keybase.md
Created March 20, 2014 16:16
keybase.md

Keybase proof

I hereby claim:

  • I am danielhfrank on github.
  • I am df (https://keybase.io/df) on keybase.
  • I have a public key whose fingerprint is 6627 401D 1986 3A54 3235 A733 5B08 4339 A82D F24E

To claim this, I am signing this object:

#!/usr/bin/env sh
while [ `hub ci-status` = "pending" ]; do
sleep 5
done
terminal-notifier -message "Build finished"