Skip to content

Instantly share code, notes, and snippets.

@wbbradley
wbbradley / math.js
Created September 18, 2013 06:40
Javascript math helpers
Math.lerp = Math.lerp || function(a, p1, p2) {
return (p2 - p1) * a + p1;
};
Math.alpha = Math.alpha || function(a, p1, p2) {
return (a - p1) / (p2 - p1);
};
Math.clamp = Math.clamp || function(a, p1, p2) {
if (p2 > p1) {
if (a < p1) {
return p1;
# 1) Add MasqueradeMiddleware to Django's middleware stack (see comment below for placement)
class MasqueradeMiddleware(object):
'''
Looks for a field in the active session specifying a user to masquerade as
and sets request.user to that user, storing the real user to the session.
This middleware is dependent on the existence of sessions, so it should be
deployed after ('inside') Django's session middleware. It should probably
be deployed after middleware like the TermsAndConditionsMiddleware which
implement one-time intercepts, so that masquerading superusers don't get
@wbbradley
wbbradley / screencap.py
Created February 17, 2014 00:10
A little script that uses Mac OS X's screencapture, ImageMagick and ffmpeg to capture a bunch of screenshots with instructions for making an MPEG movie
#!/usr/bin/python
import os
import sys
import time
from datetime import datetime
from AppKit import NSWorkspace
# This will be a movie
process_filter = None
if len(sys.argv) >= 2:
@wbbradley
wbbradley / talkie.py
Created April 17, 2014 23:34
A Flask wrapper around say Mac OS
from flask import Flask
import subprocess
app = Flask(__name__)
def talkie(voice, text):
try:
output, error = subprocess.Popen(
["say", "-v", voice, text], stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
import time
class Timer(object):
def __init__(self, name):
self.name = name
def __enter__(self):
self.start = time.clock()
return self
import traceback
def log_stack():
with open('/var/tmp/stacktraces.log', 'a') as f:
f.write("-" * 80 + "\n")
f.write("\n".join(repr(s) for s in traceback.extract_stack()
if s[0].find('virtualenv') == -1
and s[0].find('System') == -1
and s[2].find('wrapped') == -1
and s[2].find('decorated_function') == -1
@wbbradley
wbbradley / merger
Last active August 29, 2015 14:04
merger - a command line tool for resolving merge conflicts with p4merge
#!/usr/bin/env python
#
# Author: wbbradley
# Created: Originally written in Perl in 2010
# Edited: Rewritten in Python in 2014
#
# You'll need to have P4Merge installed. It's free, and
# you can find it here:
# http://lmgtfy.com/?q=P4Merge+Tool+Download
#
@wbbradley
wbbradley / keybase.md
Created April 4, 2015 07:48
Verifying my identity

Keybase proof

I hereby claim:

  • I am wbbradley on github.
  • I am wbbradley (https://keybase.io/wbbradley) on keybase.
  • I have a public key whose fingerprint is 20EB AF79 C868 0EB9 8668 CD90 0293 5A06 877A 2379

To claim this, I am signing this object:

@wbbradley
wbbradley / recent.py
Last active September 2, 2015 21:40
A script to show a git reflog with names for any branches that are recognized
#! /usr/local/bin/python
import subprocess
def get_refs():
refs = {}
proc = subprocess.Popen([
'git', 'show-ref', '--heads', '--tags', '--dereference'
], stdout=subprocess.PIPE)
while True:
@wbbradley
wbbradley / easy_async.py
Created October 30, 2015 18:30
Easy async processing in Python
import uuid
from multiprocessing import Pool
def f(u):
print str(u) + ' is a uuid'
if __name__ == '__main__':
pool = Pool(processes=2)