Skip to content

Instantly share code, notes, and snippets.

@mccutchen
mccutchen / evil.py
Created January 22, 2012 16:28
Evil Python monkeypatching test
import types
assert int(True) == 1 and int(False) == 0
# Who knew that __bulitins__ was a different object depending on whether you
# run a module directly or import it into another module?
if isinstance(__builtins__, types.ModuleType):
__builtins__.True, __builtins__.False = False, True
else:
__builtins__['True'], __builtins__['False'] = False, True
@mccutchen
mccutchen / ipythonrc
Created March 15, 2012 20:34
My current ipython config (~/.ipython/ipythonrc)
prompt_in1 '>>> '
prompt_in2 '... '
prompt_out ''
prompts_pad_left 0
nosep 1
confirm_exit 0
banner 0
pprint 1
@mccutchen
mccutchen / run_proc.py
Created June 12, 2012 18:50
run_proc.py
def run_proc(cmd, stdin=None, env=None):
"""Runs the given cmd as a subprocess, where cmd is a list suitable
for passing to subprocess.call. Returns a 3-tuple of
(exit code, stdout, stderr)
If stdin is not None, it will be passed into the subprocess on STDIN. If
env is not None, it will be used to augment the environment of the
subprocess.
"""
@mccutchen
mccutchen / gist:2961669
Created June 20, 2012 19:19
dupe data URI images
Compiling <LessCompiler src:../bitly2/templates/beta_sign_up_sign_in.html assets:1>
Inlined asset duplicated 2x: /s/beta/graphics/homepage/bubbles-2.png
Inlined asset duplicated 7x: /s/beta/graphics/homepage/top.png
Inlined asset duplicated 2x: /s/beta/graphics/homepage/seaweed.png
Compiling <LessCompiler src:../bitly2/templates/hamburger_base.html assets:1>
Inlined asset duplicated 2x: /s/beta/graphics/fs_littlefb_bblue.png
Inlined asset duplicated 2x: /s/beta/graphics/fs_big_blue_spinner.gif
Inlined asset duplicated 2x: /s/beta/graphics/vis/archive-big.png
Inlined asset duplicated 2x: /s/beta/graphics/vis/x-yellow.png
@mccutchen
mccutchen / gist:3018298
Created June 29, 2012 14:35
triangles!
A A---B
| \ \ |
| \ \ |
B---C C
B---C C
| / / |
| / / |
A A---B
@mccutchen
mccutchen / bound_methods.py
Created October 18, 2012 14:26
bound_methods.py
import random
import types
# Two ways to create "bound" methods from plain functions
def bind(instance, f):
"""Bind function f to the given instance."""
return lambda *args, **kwargs: f(instance, *args, **kwargs)
def make_method(instance, f):
@mccutchen
mccutchen / HOST remote_app
Created November 15, 2012 19:33
Hacky remote editor setup for bitly VMs
#!/bin/sh
app=$1
shift;
open -a $app $@
@mccutchen
mccutchen / pbcopy
Last active December 18, 2015 15:39
Provides a `pbcopy` command on a VM that allows me to easily copy stuff onto the (OS X) host's clipboard
#!/bin/sh
set -e
ssh mccutchen@$(remote_host) pbcopy
def mock_async_func(func, data):
"""The challenge: Mock an "async" function (ie, a function that takes a
callback arg that it calls with its result instead of returning it)
without needing to manually duplicate (and maintain) the original
function's signature.
Given this original function:
def get_page(url, callback):
libbitly.async_http.http_fetch(url, callback=callback)
def gen_batches(xs, size, overlap=0):
"""Given a sequence xs and a batch size, yield batches from the sequence as
lists of length size, where the last batch might be smaller than the
rest.
An optional overlap amount may be given as a float specifying a percentage
of the batch size or as an int specifying the number of items to overlap.
"""
assert size > 0
if isinstance(overlap, float):