Skip to content

Instantly share code, notes, and snippets.

@zgohr
zgohr / directives.js
Created January 26, 2013 00:22
Focus, blur, keyup, keydown, and keypress AngularJS directive
.directive( [ 'focus', 'blur', 'keyup', 'keydown', 'keypress' ].reduce( function ( container, name ) {
var directiveName = 'ng' + name[ 0 ].toUpperCase( ) + name.substr( 1 );
container[ directiveName ] = [ '$parse', function ( $parse ) {
return function ( scope, element, attr ) {
var fn = $parse( attr[ directiveName ] );
element.bind( name, function ( event ) {
scope.$apply( function ( ) {
fn( scope, {
$event : event
@zgohr
zgohr / post-checkout.sh
Created January 17, 2013 17:45
Post-checkout git hook to auto-add a commit when new branch is created from ```develop```. Useful because ```reflog``` doesn't stay around forever, and we don't want to hold onto defunct feature branches until we are able to run reports. There is an existing flaw in this code, can you spot it?
#! /bin/sh
from_hash=$1
to_hash=$2
checkout_type=$3
ignored=("develop" "staging" "production")
develop_hash=$(git rev-parse develop)
branch_name=$(git rev-parse --abbrev-ref HEAD)
containsElement () {
@zgohr
zgohr / admin.py
Last active April 17, 2019 10:01
Remove Tastypie from Django admin
from django.contrib import admin
from tastypie.models import ApiKey, ApiAccess
admin.site.unregister(ApiKey)
admin.site.unregister(ApiAccess)
@zgohr
zgohr / filters.py
Created December 16, 2012 19:04
UglifyJS django-compressor filter
from compressor.filters import CompilerFilter
class UglifyFilter(CompilerFilter):
command = "uglifyjs {infile} -o {outfile} -c -m"
@zgohr
zgohr / gist:3795583
Created September 27, 2012 18:30
xor
def xor(string, key):
data = ''
for char in string:
for ch in key:
char = chr(ord(char) ^ ord(ch))
data += char
return data
@zgohr
zgohr / gist:3399170
Created August 20, 2012 01:44
SVG vector data to PNG
brew upgrade
brew update
brew install cairo
brew install py2cairo
ln -s ~/Developer/lib/python2.7/site-packages/cairo ~/.virtualenvs/wpd/lib/python2.7/site-packages/cairo
# I needed to specify that my virtual environment uses python 2.7.3 because by default it used 2.7.2
# but brew compiled py2cairo with 2.7.3
mkvirtualenv --no-site-packages -p ~/Developer/bin/python wpd
@zgohr
zgohr / gist:3317634
Created August 10, 2012 20:31
returning tuples
def return_it():
return ('first', 'second',)
one, two = return_it()
print one
# first
print two
# second
@zgohr
zgohr / gist:3317552
Created August 10, 2012 20:22
single line for loop
my_list = [('one','two',),('three','four',)]
return [first + second for first, second in my_list]
# ['onetwo', 'threefour']
@zgohr
zgohr / gist:2492861
Created April 25, 2012 19:54
Socket security when your client is hosted by another application
// Assume that we are not using a redis session store
// instead storing relevant information in redis.
// client side
// assume Cookie.get(name) returns the cookie
// this can use jQuery's cookie plugin or
// some regular expression on document.cookie
var socket = new io.Socket();
@zgohr
zgohr / run.sh
Created April 5, 2012 18:38
simple hubot start script. do not use for production!
#!/bin/bash
export PATH="node_modules/.bin:$PATH"
export HUBOT_CAMPFIRE_TOKEN="token"
export HUBOT_CAMPFIRE_ROOMS="1,2,3,4"
export HUBOT_CAMPFIRE_ACCOUNT="subdomain"
export HUBOT_FOGBUGZ_HOST="some/fogbugz/server"
export HUBOT_FOGBUGZ_TOKEN="token"
export HUBOT_GITHUB_USER="gh_account"
export HUBOT_GITHUB_TOKEN="token"