Skip to content

Instantly share code, notes, and snippets.

@brianewing
brianewing / remotemd5.py
Created May 26, 2011 23:02
Python MD5 of remote file (URL)
import os, hashlib, urllib2, optparse
def get_remote_md5_sum(url, max_file_size=100*1024*1024):
remote = urllib2.urlopen(url)
hash = hashlib.md5()
total_read = 0
while True:
data = remote.read(4096)
total_read += 4096
@brianewing
brianewing / gist:1325250
Created October 29, 2011 23:38
Fixnum#power_of?
class Fixnum
def power_of?(i)
self != 0 and Math.log(self.abs, i) % 1 == 0
end
end
puts 8.power_of? 2 # => true
puts 64.power_of? 2 # => true
puts 27.power_of? 3 # => true
puts 27.power_of? 4 # => false
@brianewing
brianewing / timer.js
Created January 1, 2012 23:36
Timer - utility class for controlling intervals
function Timer(func, delay, start) {
this.func = func;
this.delay = delay;
this.running = false;
if(start) this.start();
}
Timer.prototype.start = function(delay) {
this.running = true;
return this.interval = setInterval(this.func, (delay || this.delay));
@brianewing
brianewing / gist:2300335
Created April 4, 2012 10:50
Javascript symbol-to-proc
String.prototype.__defineGetter__('proc', function() {
var prop = this;
return function(obj) {
if(typeof obj[prop] == 'function')
return obj[prop]();
return obj[prop];
}
});
@brianewing
brianewing / extensions.coffee
Created April 28, 2012 13:05
CoffeeScript helpers
window?.exports = window # allows more conventional client-side 'exporting'
# nicer timeouts/intervals in coffeescript
exports.timeout = (ms, fn) -> setTimeout(fn, ms)
exports.interval = (ms, fn) -> setInterval(fn, ms)
@brianewing
brianewing / gist:3986912
Created October 31, 2012 13:04
Ruby bucket
# sometimes I just want a quick-and-dirty thing to shove attributes onto without caring.
class Bucket
def method_missing(*args)
prop = args.first.to_s.sub(/\=$/, '').to_sym
self.class.send :attr_accessor, prop
send *args
end
end
@brianewing
brianewing / gist:5735832
Created June 8, 2013 16:47
RSpec testing the parsing of various YML config options taking advantage of Ruby heredoc syntax with a block
# helper
def with_stubbed_yml_config(yml, &block)
# stub, yield, unstub
end
# in examples..
with_stubbed_yml_config(<<-YML) { url.should == 'http://test:5984' }
test:
@brianewing
brianewing / git-old
Created August 9, 2013 13:41
Git command for opening files from previous commits / other branches in your editor
#!/usr/bin/env ruby
unless ARGV.length >= 2
STDERR.puts "git old <commit/refspec> <file> [file2] ..."
Kernel.exit(1)
end
ref = ARGV.shift
ARGV.each { |f| `git show #{ref}:#{f} | $EDITOR` }
@brianewing
brianewing / fix.sh
Created September 19, 2013 21:41
fix postgres for @tommypalm
# stop postgres
pg_ctl -D /usr/local/var/postgres stop &&
# obliterate old postgres data
rm -Rf /usr/local/var/postgres &&
# fresh start..
initdb /usr/local/var/postgres -E utf8 &&
# start postgres
pg_ctl -D /usr/local/var/postgres start
@brianewing
brianewing / gist:7103939
Created October 22, 2013 16:41
Lock your Mac by pressing power button
Install "PowerBlock" (freeware) from this site: http://www.irradiatedsoftware.com/labs/
Choose "Run AppleScript when power button is pressed"
Make it run this AppleScript:
do shell script "open /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app"