Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Pretty swamped with work-work

Brad Greenlee bgreenlee

💭
Pretty swamped with work-work
View GitHub Profile
@bgreenlee
bgreenlee / strip_trailing_whitespace.py
Created Jul 19, 2011
Sublime Text 2 plugin to strip trailing whitespace #python #sublimetext
View strip_trailing_whitespace.py
import sublime_plugin
class StripTrailingWhitespaceCommand(sublime_plugin.TextCommand):
"""
Strip whitespace from the end of each line in the file.
"""
def run(self, edit):
trailing_white_space = self.view.find_all("[\t ]+$")
trailing_white_space.reverse()
for r in trailing_white_space:
@bgreenlee
bgreenlee / version_string.rb
Created Sep 13, 2011
Compare two version strings. #ruby
View version_string.rb
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
@bgreenlee
bgreenlee / multipart_binary_posts.js
Created Sep 17, 2011
Multipart Binary POSTs in Javascript #javascript
View multipart_binary_posts.js
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 / autocorrrect.py
Created Oct 28, 2011
Simple ngram autocorrect #python #algorithms
View autocorrrect.py
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 / gist:1328254
Last active Sep 27, 2015
Words with 6 consecutive consonants #words
View gist:1328254
$ egrep -i "[bcdfghjklmnpqrstvwxz]{6,}" /usr/share/dict/words
archchronicler
bergschrund
Eschscholtzia
fruchtschiefer
latchstring
lengthsman
Nachschlag
postphthisic
veldtschoen
@bgreenlee
bgreenlee / logging_subprocess.py
Created Nov 29, 2011
Variant of subprocess.call that accepts a logger instead of stdout/stderr #python
View logging_subprocess.py
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 / DefaultKeyBinding.dict
Created Feb 28, 2012 — forked from indirect/DefaultKeyBinding.dict
Type unicode arrows and other cool things #unicode #osx
View DefaultKeyBinding.dict
/* 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 / stackscript.sh
Created Aug 13, 2012
Linode Chef Bootstrap StackScript #linode #chef
View stackscript.sh
#!/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 / sieve.scala
Created Nov 9, 2012
Sieve of Eratosthenes in Scala #scala #algorithms
View sieve.scala
# 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 / useful.js
Created Dec 13, 2012
A collection of useful Javascript functions #javascript
View useful.js
// 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]