Skip to content

Instantly share code, notes, and snippets.

View Flushot's full-sized avatar
👀

Chris Lyon Flushot

👀
View GitHub Profile
@Flushot
Flushot / map2.rb
Last active October 2, 2015 06:08
Like map, but for two lists instead of one.
#
# Combines 2 vectors into a single vector of size av,
# by applying a binary operation (defined in block)
# on each element.
#
# Examples:
# map2([1,2,3], [4,5,6]) { |a,b| a*b } == [4,10,18]
# map2([1,2,3], [4,5] ) { |a,b| a+b } == [5,7,nil]
# map2([1,2], [4,5,6]) { |a,b| a+b } == [5,7]
#
@Flushot
Flushot / backbone.js
Created April 28, 2012 09:41
Underscore.js 1.3.3 and Backbone.js 0.9.2 w/ AMD support (RequireJS)
// Backbone.js 0.9.2
// (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc.
// Backbone may be freely distributed under the MIT license.
// For all details and documentation:
// http://backbonejs.org
// Use a factory function to attach Backbone properties to an exports value.
// Code at the end of this file creates the right define, require and exports
// values to allow Backbone to run in a CommonJS or AMD container or in the
@Flushot
Flushot / fact.clj
Created February 1, 2013 07:28
Clojure factorial
(defn fact2 [n]
(if (= n 1)
1
(* n (fact2 (- n 1)))))
@Flushot
Flushot / rgb2hex.py
Created June 14, 2013 19:38
Converts an RGB color value into hex, e.g.: (128,23,45) -> '#80172d'
rgb2hex = lambda r,g,b: '#'+''.join([ hex(x)[2:].zfill(2) for x in (r,g,b) ])
@Flushot
Flushot / mysql-backup-nocreatedb.sh
Last active December 18, 2015 12:39
Comments out 'CREATE DATABASE' lines in a MySQL backup script
#!/bin/sh
sed -i.bak "s@^\(CREATE DATABASE\)@#\1@g" $1
@Flushot
Flushot / Makefile
Created June 14, 2013 19:50
Makefile for Python virtualenv and pip dependency manifest. Assumes virtual environment will be created in a 'venv' subdirectory.
.PHONY: init_venv deps freeze clean_venv
all: init_venv deps
PYTHONPATH=venv ; . venv/bin/activate
init_venv:
if [ ! -e "venv/bin/activate_this.py" ] ; then PYTHONPATH=venv ; virtualenv --clear venv ; fi
deps:
PYTHONPATH=venv ; . venv/bin/activate && venv/bin/pip install -U -r requirements.txt && if [ "$(ls requirements)" ] ; then venv/bin/pip install -U -r requirements/* ; fi
@Flushot
Flushot / subdict.py
Created June 14, 2013 19:55
Subsets a dictionary based on a set of specified keys to include
def subdict(d, include_keys=[]):
"""
Subsets a :dict: by keys specified in :include_keys:
>>> subdict({ 'a': 1, 'b': 2, 'c': 3 }, ['a', 'c'])
{ 'a': 1', 'c': 3 }
>>> subdict({ 'a': 1, 'b': 2, 'c': 3 }, ['x', 'y'])
{}
>>> subdict({}, [])
{}
@Flushot
Flushot / jdumps.py
Last active December 18, 2015 12:39
Equivalent of json.dumps() but includes ISO-8601 date encodings of datetime and date data.
import json
import datetime
from dateutil import tz
from naive_aware import isAware, makeAware, makeNaive
def jdumps(obj):
return json.dumps(obj,
default=lambda o: (makeAware(o) if isinstance(o, datetime.datetime) else o).isoformat() \
if (isinstance(o, datetime.datetime) or isinstance(o, datetime.date)) else None,
sort_keys=True,
@Flushot
Flushot / keygen.sh
Created June 14, 2013 21:43
Generates an RSA key pair (private.key and public.key)
#!/bin/sh
openssl genrsa -out private.key 4096
openssl rsa -in private.key -pubout > public.key
@Flushot
Flushot / newmac.sh
Created June 14, 2013 22:14
Shell script to change MAC address on OSX
#!/bin/sh
IFACE=en0
macaddr=`openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'`
sudo airport -z
osascript -e 'tell application "System Preferences" to quit'
sudo scselect -n
sudo ifconfig $IFACE lladdr $macaddr
# These should match
echo $macaddr