Skip to content

Instantly share code, notes, and snippets.

View bgreenlee's full-sized avatar

Brad Greenlee bgreenlee

View GitHub Profile
@bgreenlee
bgreenlee / check_echo_nest.py
Created December 20, 2012 02:06
Check for available band names from @bwhitman's "Ten Thousand Statistically Grammar-Average Fake Band Names" (http://alumni.media.mit.edu/~bwhitman/10000.html) using The Echo Nest #python #music #fun
#!python
import requests
import time
API_KEY = '<YOUR KEY>'
# names saved from http://alumni.media.mit.edu/~bwhitman/10000.html
for name in open("names.txt"):
name = name.strip()
r = requests.get('http://developer.echonest.com/api/v4/artist/profile', params={'api_key': API_KEY, 'name': name})
@bgreenlee
bgreenlee / useful.js
Created December 13, 2012 00:28
A collection of useful Javascript functions #javascript
// sum an array, optionally providing a function to call on each element of the
// array to retrieve the value to sum
Array.prototype.sum = function(fn) {
return this.reduce(function(accum, elem) {
return accum + (fn ? fn(elem) : elem);
}, 0);
};
// flatten an array
// [1,2,[3,4]] -> [1,2,3,4]
@bgreenlee
bgreenlee / sieve.scala
Created November 9, 2012 17:40
Sieve of Eratosthenes in Scala #scala #algorithms
# sieve of eratosthenes
def sieve(s: Stream[Int]): Stream[Int] = s.head #:: sieve(s.tail filter (_ % s.head != 0))
def from(n: Int): Stream[Int] = n #:: from(n + 1)
def primes = sieve(from(2))
@bgreenlee
bgreenlee / stackscript.sh
Created August 13, 2012 04:32
Linode Chef Bootstrap StackScript #linode #chef
#!/bin/bash
# <udf name="hostname" label="Hostname">
# <udf name="ssh_key" label="SSH Key">
# <udf name="ubuntu_mirror" label="Ubuntu Mirror URL" default="http://us.archive.ubuntu.com/ubuntu">
# <udf name="distro" label="Ubuntu Distro" default="maverick">
# <udf name="default_ruby_interpreter" label="Default Ruby Interpreter"
# oneOf="ruby-1.8.6,ruby-1.8.7,ruby-1.9.1,ruby-1.9.2,ruby-head"
# default="ruby-1.9.2">
@bgreenlee
bgreenlee / DefaultKeyBinding.dict
Created February 28, 2012 14:53 — forked from indirect/DefaultKeyBinding.dict
Type unicode arrows and other cool things #unicode #osx
/* Put this in ~/Library/KeyBindings/DefaultKeyBinding.dict */
/* Based on a file from @eventualbuddha */
{
/* Modifier keys: start with C-m */
"^m" = {
"^ " = ("insertText:", "\U2423"); /* C-space space */
"^e" = ("insertText:", "\U21A9"); /* C-e return */
"e" = ("insertText:", "\U2305"); /* e enter */
@bgreenlee
bgreenlee / logging_subprocess.py
Created November 29, 2011 00:58
Variant of subprocess.call that accepts a logger instead of stdout/stderr #python
import subprocess
import select
from logging import DEBUG, ERROR
def call(popenargs, logger, stdout_log_level=DEBUG, stderr_log_level=ERROR, **kwargs):
"""
Variant of subprocess.call that accepts a logger instead of stdout/stderr,
and logs stdout messages via logger.debug and stderr messages via
logger.error.
@bgreenlee
bgreenlee / gist:1328254
Last active September 27, 2015 20:38
Words with 6 consecutive consonants #words
$ egrep -i "[bcdfghjklmnpqrstvwxz]{6,}" /usr/share/dict/words
archchronicler
bergschrund
Eschscholtzia
fruchtschiefer
latchstring
lengthsman
Nachschlag
postphthisic
veldtschoen
@bgreenlee
bgreenlee / autocorrrect.py
Created October 28, 2011 00:01
Simple ngram autocorrect #python #algorithms
import os.path
import collections
from operator import itemgetter
WORDFILE = '/usr/share/dict/words'
class Autocorrect(object):
"""
Very simplistic implementation of autocorrect using ngrams.
"""
@bgreenlee
bgreenlee / multipart_binary_posts.js
Created September 17, 2011 00:07
Multipart Binary POSTs in Javascript #javascript
var dataURL = this.canvas.toDataURL(this.getImageType()); // grab the snapshot as base64
var imgData = atob(dataURL.substring(13 + this.getImageType().length)); // convert to binary
var filenameTimestamp = (new Date().getTime());
var separator = "----------12345-multipart-boundary-" + filenameTimestamp;
// Javascript munges binary data when it undergoes string operations (such as concatenation), so we need
// to jump through a bunch of hoops with streams to make sure that doesn't happen
// create a string input stream with the form preamble
@bgreenlee
bgreenlee / version_string.rb
Created September 13, 2011 22:36
Compare two version strings. #ruby
class VersionString < String
def <=>(other)
my_parts = self.split('.').map(&:to_i)
other_parts = other.split('.').map(&:to_i)
my_parts.each_with_index do |part, i|
break if i >= other_parts.length
next if part == other_parts[i]
return part <=> other_parts[i]
end
if my_parts.length > other_parts.length