Skip to content

Instantly share code, notes, and snippets.

View aviddiviner's full-sized avatar

David Irvine aviddiviner

  • Cape Town, South Africa
View GitHub Profile
@aviddiviner
aviddiviner / .block
Last active January 5, 2018 20:23
Understanding d3 scale quantize / quantile
license: gpl-3.0
@aviddiviner
aviddiviner / user-data.sh
Created July 3, 2016 02:25
EC2 user-data script to run SSH on port 443 on startup
#!/bin/bash -ex
perl -pi -e 's/^#?Port 22$/Port 443/' /etc/ssh/sshd_config
service sshd restart || service ssh restart
@aviddiviner
aviddiviner / magic-iterm-open.py
Last active June 8, 2016 16:56
Smart hyperlinked stack traces in iTerm (open on correct line)
#!/usr/bin/python
import re
import sys
from subprocess import call
if len(sys.argv) > 2:
filename, linenum = sys.argv[1], sys.argv[2]
@aviddiviner
aviddiviner / split-interleaved.go
Created April 30, 2015 21:33
Slice a string into substrings, keeping the matches
// Splits a string into parts by a given pattern, interleaving the matched
// parts with the unmatched parts.
// For example:
// splitInterleaved(/a/, "aabbac") => []string{"a", "a", "bb", "a", "c"}
// Another way of looking at it is:
// regexp.Split() <zip> regexp.FindAllString() <flatten>
//
func splitInterleaved(re *regexp.Regexp, str string) []string {
splits := re.FindAllStringIndex(str, -1) // Find all the split points
slice := make([]string, len(splits)*2+1) // Allocate enough space
@aviddiviner
aviddiviner / noko.rb
Last active August 29, 2015 14:06
Command line Nokogiri
#!/usr/bin/env ruby
#
# A little wrapper around the Ruby Nokogiri gem for easy markup scanning.
#
# Usage: cat example.html | noko 'body .header[2]'
# Usage: curl -L http://google.com | noko 'table a'
# Usage: noko 'css selector'
# (Type in some lines, ctrl-d when done.)
#
require 'nokogiri'
@aviddiviner
aviddiviner / run-service.sh
Created March 4, 2014 00:11
Quick bash wrapper around a service; allows for environment setup and sends trapped signals
#!/bin/bash
handler () {
kill -s SIGINT $PID
while kill -0 $PID &>/dev/null
do
wait $PID
done
}
@aviddiviner
aviddiviner / monkeypatch-enumerable.rb
Last active August 29, 2015 13:56
Enumerable monkey patches
module Enumerable
# Iterate through a list of things, printing out dots every N items.
#
# cards.each_with_progress do |card|
# ...
# end
#
def each_with_progress(dot_interval = 20)
progress = 0
each do |item|
@aviddiviner
aviddiviner / delayed-hint.js
Last active August 29, 2015 13:56
A delayed reaction tooltip handler for jQuery
// A delayed reaction tooltip handler for jQuery.
// Useful for tooltips that are costly to load, such as images.
//
// $('#article a').delayedHint(function(anchor, callback) {
// var imgSrc = $(anchor).attr('href');
// $('<img/>').load(function() {
// var frame = $('<span class="frame"></span>').append(this);
// callback(frame);
// }).attr('src', imgSrc);
// });
@aviddiviner
aviddiviner / thread-pool.rb
Last active December 27, 2015 10:59
A simple thread pool for running N parallel jobs
require 'thread'
# A simple thread pool for running N parallel jobs.
#
# pool = ThreadPool.new(5)
# 20.times do
# pool.next_thread{ sleep 2 }
# end
# pool.join
#
@aviddiviner
aviddiviner / simple-logger.rb
Last active December 26, 2015 16:19
A simple logger to STDERR
# A simple logger to STDERR.
#
# log = SimpleLogger.new
# log.info "Helpful log message"
# log.debug "Some debugging output"
#
class SimpleLogger
LEVELS = %w(debug info warn error fatal)
def initialize(level = :debug)
@visible = LEVELS[LEVELS.index(level.to_s)..(-1)]