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 / 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 / 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 / levenshtein.swift
Created Jun 27, 2014
Levenshtein Distance in Swift
View levenshtein.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 / UIImage+Resize.swift
Created Nov 23, 2015
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/
View UIImage+Resize.swift
//
// 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 {
@bgreenlee
bgreenlee / report.rb
Created Mar 1, 2009
Displays a mysql-style report for an array of ActiveRecord objects. Stick it in your .irbrc.
View report.rb
# 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 / rescuewindows.lua
Created Jan 24, 2018
Hammerspoon function to move off-screen windows onto the main screen
View rescuewindows.lua
-- Rescue Windows
-- Move any windows that are off-screen onto the main screen
function rescueWindows()
local screen = hs.screen.mainScreen()
local screenFrame = screen:fullFrame()
local wins = hs.window.visibleWindows()
for i,win in ipairs(wins) do
local frame = win:frame()
if not frame:inside(screenFrame) then
win:moveToScreen(screen, true, true)
@bgreenlee
bgreenlee / flashScreen.swift
Created Apr 1, 2015
Flashing the screen in Swift. Sticking this here because it took me a while to work out the right variable types and casting.
View flashScreen.swift
func flashScreen() {
let inDuration: CGDisplayFadeInterval = 0.5
let outDuration: CGDisplayFadeInterval = 0.5
let color = NSColor.redColor()
var fadeToken: CGDisplayFadeReservationToken = 0
let colorToUse = color.colorUsingColorSpaceName(NSCalibratedRGBColorSpace)!
let err = CGAcquireDisplayFadeReservation(inDuration + outDuration, &fadeToken)
if Int(err) != Int(kCGErrorSuccess.value) {
NSLog("Error acquiring fade reservation")
@bgreenlee
bgreenlee / data-tech-blog-club-faq.md
Last active Jan 14, 2020
Data Tech Blog Club FAQ
View data-tech-blog-club-faq.md

Data Tech Blog Club FAQ

What is Data Tech Blog Club?

Blog Club is a weekly meeting where we discuss an article or short paper on a topic at least vaguely adjacent to our work.

Can I Come to Blog Club?

Yes! The more the merrier! You don't have to be in the Data Tech org. Meetings are Monday at 11:30a Eastern in A-511. Hit up @brad to get an invite.

Do I Have to Read the Article?

Well, no, but you'll get more out of it if you do. The articles we pick usually don't require more than fifteen minutes or so to read. You can do it! But, if you totally spaced and it's five minutes before the meeting and what was the article anyway...no worries, come anyway. Someone will give a summary and we always end up having an interesting discussion.

@bgreenlee
bgreenlee / WiFiInfo.swift
Created Nov 7, 2015
Swift Class to encapsulate wifi network info
View WiFiInfo.swift
//
// WiFiInfo.swift
//
// Created by Brad Greenlee on 11/7/15.
//
import Foundation
import CoreWLAN
extension CWPHYMode: CustomStringConvertible {
@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]