Skip to content

Instantly share code, notes, and snippets.

View jamesdabbs's full-sized avatar

James Dabbs jamesdabbs

View GitHub Profile
@jamesdabbs
jamesdabbs / models.py
Created June 25, 2012 05:11
A simple example of dynamic models in Django
from django.db import models
def get_column_attrs(width):
""" Creates the specified number of fields and adds them to a dict,
suitable to be used as a model's attrs
"""
attrs = dict(
('col_{}'.format(i), models.IntegerField(default=1))
for i in range(0, width)
@jamesdabbs
jamesdabbs / dbpull.py
Created July 2, 2012 15:33
A custom django-admin command to replace Heroku's db:pull command
from optparse import make_option
import os
import subprocess
import urllib2
from django.conf import settings
from django.core.management.base import BaseCommand
class Command(BaseCommand):
@jamesdabbs
jamesdabbs / Engine convenience functions
Created December 14, 2012 15:29
From the root of the relevant engine project, `run path/to/engine_io/dir` will re-run the current binary with the stored flags and input file. `debug` works similarly, but starts gdb with the run command copied to the clipboard (so you can set breakpoints before running it). These are not intended to be comprehensive, but feel free to use them a…
function run {
local command=`cat "$1/command" | grep -o 'bin.*' | cut -d '<' -f 1 | sed "s/'//"`
$command < $1/in.*
}
function debug {
local command=`cat "$1/command" | grep -o 'bin.*' | cut -d '<' -f 1 | sed "s/'//"`
local bin=`echo $command | cut -d '-' -f 1`
local args=`echo $command | grep -o '\-.*'`
echo -n "cmd: $command\tbin: $bin\targs: $args"
echo -n "run $args < $1/in.*" > $1/run.txt
@jamesdabbs
jamesdabbs / post-merge
Last active December 14, 2015 02:59
Never again forget to bundle and migrate after merging in changes
#!/usr/bin/env ruby
# Notifies you when certain files are changed which necessitate further action on your part.
#
# To enable:
# - copy this file to .git/hooks/post-merge
# - chmod +x .git/hooks/post-merge
commits = `git reflog -n 2`.lines.map { |l| l.split.first }
changed = `git diff --name-only #{commits.first} #{commits.last}`.lines.map &:strip
@jamesdabbs
jamesdabbs / jason.rb
Created April 15, 2013 22:37
My solution to EPP13 puzzle #7 - jfmamjJASONd. Puzzle available at: http://www.ericharshbarger.org/epp/2013/puz/07_jfmamjJASONd/jfmamjJASONd_v04.pdf
require 'colorize'
require 'csv'
def color? date
date.wday == 5 # Friday
end
CSV.foreach('grid') do |row|
row.each do |cell|
next unless cell
@jamesdabbs
jamesdabbs / long_job.rb
Last active December 19, 2015 00:29
A sample job for testing out "kill a bad resque job" logic.
# Queue this up manually with `Resque.enqueue LongJob, foo: :bar, ...`
# The extra opts don't do anything, but should show up in the UI
#
# You may want to fiddle around with the @queue / your resque-pool configuration
# for testing purposes. By default, :download is the only queue with 2 workers
#
# Also, you might want to run resque(-pool) with VERBOSE=true
class LongJob
@queue = :download
@jamesdabbs
jamesdabbs / multiple_exceptions.rb
Created September 12, 2013 14:12
Rescuing multiple exception classes
begin
i = rand(2)
i == 0 ? ([] + '') : (foo)
rescue TypeError, NameError => e
puts "oops: #{e.message}"
end
@jamesdabbs
jamesdabbs / collector.yml
Created September 26, 2013 20:00
Sample [tmuxinator](https://github.com/aziz/tmuxinator) config for the collector
# ~/.tmuxinator/collector.yml
name: collector
root: ~/src/ScoutCollector
# Optional tmux socket
# socket_name: foo
# Runs before everything. Use it to start daemons etc.
# pre: sudo /etc/rc.d/mysqld start
@jamesdabbs
jamesdabbs / .tmux.conf
Last active December 24, 2015 09:39
My current tmux config
set -g prefix C-b
# remap prefix to Control + a
# unbind C-b
# set -g prefix C-a
bind-key r source-file ~/.tmux.conf \; display-message " Config reloaded ... "
set -g history-limit 50000
@jamesdabbs
jamesdabbs / ggb
Created October 16, 2013 16:27
A simple amalgam of git grep and git blame, mostly intended for finding FIXMEs that I introduced (as evidenced by the defaults) before merging them in.
#!/usr/bin/env ruby
word = ARGV.shift || "FIXME|TODO"
name = ARGV.shift || "James"
lines = `git grep -n -E '#{word}'`
lines.scan /(.*):(\d+)/ do |file, line|
up, down = line.to_i - 2, line.to_i + 5
blame = `git blame -w -L#{up},#{down} #{file}`
if blame =~ /#{name}.*(#{word})/