Skip to content

Instantly share code, notes, and snippets.

View bgreenlee's full-sized avatar

Brad Greenlee bgreenlee

View GitHub Profile
@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 / levenshtein.swift
Created June 27, 2014 05:54
Levenshtein Distance in Swift
/**
* Levenshtein edit distance calculator
* Usage: levenstein <string> <string>
*
* To compile:
* sudo xcode-select -switch /Applications/Xcode6-Beta.app/Contents/Developer
* xcrun swift -sdk $(xcrun --show-sdk-path --sdk macosx) levenshtein.swift
*/
import Foundation
@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 / gbd.sh
Last active August 8, 2023 16:49
Interactive git branch deletion with fzf
# gbd - interactive git branch deletion
# based off https://www.peterp.me/articles/cli-tips-interactive-branch-delete/
# use tab to select multiple
# Drop this in your .bashrc or .zshrc (assumes fzf is installed)
gbd() {
local branches branch
branches=$(git for-each-ref --sort=-committerdate refs/heads/ --format="[%(committerdate:short)] %(color:bold green)%(refname:short)%(color:reset) - %(contents:subject)" --color=always | egrep -v main) &&
branch=$(echo "$branches" | fzf --multi --ansi --preview 'git show {2}' ) &&
git branch -D $(echo "$branch" | awk '{print $2}')
}
@bgreenlee
bgreenlee / report.rb
Created March 1, 2009 06:01
Displays a mysql-style report for an array of ActiveRecord objects. Stick it in your .irbrc.
# mysql-style output for an array of ActiveRecord objects
#
# Usage:
# report(records) # displays report with all fields
# report(records, :field1, :field2, ...) # displays report with given fields
#
# Example:
# >> report(records, :id, :amount, :created_at)
# +------+-----------+--------------------------------+
# | id | amount | created_at |
@bgreenlee
bgreenlee / Versioning.sh
Last active June 13, 2022 16:42
Automatic project versioning for Xcode using git commits & tags #xcode #git
#!/bin/sh
# Versioning.sh
#
# https://gist.github.com/791352 by Marc Hedlund
#
# Found at http://kswizz.com/post/2686511526/git-xcode-versioning and slightly
# modified.
# To install:
@bgreenlee
bgreenlee / strip_trailing_whitespace.py
Created July 19, 2011 00:27
Sublime Text 2 plugin to strip trailing whitespace #python #sublimetext
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 / wordle.py
Last active February 7, 2022 21:24
Wordle "Solver"
#!/usr/bin/env python3
from datetime import datetime
import urllib.request
import re
# grab the word list
words_url = 'https://www.powerlanguage.co.uk/wordle/main.c1506a22.js'
req = urllib.request.Request(words_url, data=None,
headers={
'User-Agent': 'Wordle "Solver" 1.0'
@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 / UIImage+Resize.swift
Created November 23, 2015 05:24
Swift port of UIImage+Resize, UIImage+Alpha, and UIImage+RoundedCorner, from http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
//
// UIImage+Resize.swift
// Port of UIImage+Resize.m
// from http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
//
import Foundation
import UIKit
extension UIImage {