Skip to content

Instantly share code, notes, and snippets.

@jimjh
jimjh / truecrypt_fix.bash
Created Oct 14, 2012
brew doctor and truecrypt
View truecrypt_fix.bash
#!/bin/bash
libs=( "/usr/local/lib/libmacfuse_i32.2.dylib" \
"/usr/local/lib/libosxfuse_i32.2.dylib" \
"/usr/local/lib/libosxfuse_i64.2.dylib" \
"/usr/local/lib/libmacfuse_i64.2.dylib" \
"/usr/local/lib/libosxfuse_i32.la" \
"/usr/local/lib/libosxfuse_i64.la" \
"/usr/local/lib/pkgconfig/osxfuse.pc" )
@jimjh
jimjh / padding.go
Created Nov 4, 2012
Padding with sprintf
View padding.go
package main
import "fmt"
func main() {
// make sure that printf pads digits with zeros so we can sort the strings
// without converting to them back to ints
fmt.Printf("%010d\n", 1)
@jimjh
jimjh / parallel_rw.go
Created Nov 27, 2012
Experiments with parallel read/write in Go without file.Sync or file.Close
View parallel_rw.go
package main
// parallel_rw.go experiments with parallel read/write from a file without
// syncing (aka flushing) or closing.
import (
"encoding/binary"
"fmt"
// "math/rand"
"os"
@jimjh
jimjh / crash_recover.go
Created Nov 27, 2012
Experiments with pagecache-centric logging and recovery in Go.
View crash_recover.go
package main
// crash_recover.go experiments with recovering from a file that suddenly
// crashed (files are written without sync or close.)
import (
"encoding/binary"
"fmt"
"math/rand"
"os"
@jimjh
jimjh / flushing_test.go
Created Nov 27, 2012
Benchmarks with 10% syncs versus no syncs.
View flushing_test.go
package flushing
import (
"encoding/binary"
"fmt"
"math/rand"
"os"
"testing"
)
@jimjh
jimjh / deep_freeze.rb
Last active Dec 12, 2015
deep_freeze ruby enumerables.
View deep_freeze.rb
# ~*~ encoding: utf-8 ~*~
module Enumerable
# Freezes elements and nested containers recursively.
def deep_freeze
each { |o| o.respond_to?(:deep_freeze) ? o.deep_freeze : o.freeze }
freeze
end
end
@jimjh
jimjh / should_validate_existence_of.rb
Created Mar 24, 2013
validates_existence_of - a custom Shoulda/RSpec matcher
View should_validate_existence_of.rb
RSpec::Matchers.define :validate_existence_of do |association|
description { "require an existing #{association}" }
@association = association
@attribute = "#{association}_id"
@message_finder_factory = Shoulda::Matchers::ActiveModel::ValidationMessageFinder
def unique_id
@jimjh
jimjh / should_have_valid_factory.rb
Created Mar 28, 2013
have_a_valid_factory - a custom Shoulda/RSpec matcher
View should_have_valid_factory.rb
RSpec::Matchers.define :have_a_valid_factory do
match do |subject|
factory = subject.class.name.underscore.to_sym
FactoryGirl.build(factory).should be_valid
end
end
@jimjh
jimjh / grad.R
Created Apr 25, 2013
An easier way to build custom color gradients with R.
View grad.R
color.grad <- function(input, bins=10) {
colors <- c('#FFFFFF', heat.colors(bins-1)) # white for 0/NA
input[is.na(input)] <- 0 # replace NA with 0
colors[cut(input, bins, labels=F)] # break up into bins, project colors
}
@jimjh
jimjh / fix_ids.rb
Last active Dec 17, 2015
Fix Yelp primary keys.
View fix_ids.rb
#!/usr/bin/env ruby
require 'mongo'
require 'ruby-progressbar'
conn = Mongo::Connection.new
db = conn['yelp']
coll = db['reviews']
user_ids, biz_ids = {}, {}