Skip to content

Instantly share code, notes, and snippets.

@beala
beala / argy.py
Created August 11, 2012 15:48
A command line argument parser using generators and co-routines.
"""
Inspired by:
http://eli.thegreenplace.net/2009/08/29/co-routines-as-an-alternative-to-state-machines/
"""
def parse_args(target):
"""A generator that parses a stream of arguments one character at a time.
As soon as a flag, or flag value pair ("-a" or "-a value") is processed
the pair is sent off as a tuple to the 'target' generator.
@beala
beala / butterbird.py
Created August 25, 2012 21:48
Tweet queue script
import twitter
import time
import sys
consumer_key=""
consumer_sec=""
access_key=""
access_sec=""
api = twitter.Api(
def find_order(cur_order, menu, valid_orders):
# Try every item in the menu.
for selection in menu:
new_order = cur_order + [selection]
# Try again if the current item blows the budget.
if sum(new_order) > 15.05:
continue
# If the order costs 15.05, then it meets our contraints.
elif sum(new_order) == 15.05:
valid_orders.append(new_order)
@beala
beala / picking-colors.py
Created October 7, 2012 17:19
The companion code to my picking colors blog post.
import math
import random
def getSuccessors(color):
def perm(l, n, str_a, perm_a):
"""Generate every permutation of length `n`, selecting from the
possible values in `l`.
"""
if len(str_a) == n:
return (str_a,) + perm_a
@beala
beala / AgeProgressMeter.pde
Created December 10, 2012 08:11 — forked from DrSkippy/AgeProgressMeter.pde
Graphic showing how many months old you are compared to median and long lifespans
// Small changes to DrSkippy's code. Turns graph horizontal. Makes margins smaller.
// Original (https://gist.github.com/4057438) Blog (http://blog.drskippy.com/2012/11/11/age-visualization/)
import java.util.*;
import java.text.*;
int boxSize = 5; // pixel size of month representation
int boxSpacing = 4; // vertical and horizontal spacing
int decadeSpacing = 2*boxSpacing; // extra space between decades
int markerOverhang = 18;
@beala
beala / semaphore.rb
Last active December 10, 2015 20:38
Implementation of semaphores in Ruby. Bonus: A bounded buffer queue.
class Semaphore
attr_reader :value, :max
def initialize(count)
@m = Mutex.new
@value = count
@waiting = []
end
def synchronize(*args)
@beala
beala / quine.js
Created January 22, 2013 21:39
Quine in JavaScript. Is this cheating? *grin* http://en.wikipedia.org/wiki/Quine_(computing)
/* For the SpiderMonkey command line interp. */
(function () {
print("(" + arguments.callee + ")();");
})();
/* For the browser. */
(function () {
console.log("(" + arguments.callee + ")();");
})();
@beala
beala / nan.js
Created February 5, 2013 20:10
What does this print?
switch(NaN){
case Infinity: print(Infinity); break;
case NaN: print(NaN); break;
default: print('default'); break;
}
@beala
beala / js_instanceof_wat.js
Created February 19, 2013 09:25
Fun with prototype inheritance in JS.
// Create constructors for Parent, Sub1, and Sub2.
function Parent(){}
function Sub1(){}
function Sub2(){}
// Sub1 and Sub2 inherit from Parent.
Sub1.prototype = Sub2.prototype = new Parent();
(new Sub1()) instanceof Sub2 // true. Wat?
@beala
beala / splitback.sh
Last active December 14, 2015 03:59
Script for encrypting a file with an autogenerated key and splitting that key into multiple parts using Shamir's secret sharing.
#!/bin/bash
# Uses my xkpa password generator: https://github.com/beala/xkcd-password
# The password generation method can be modified below.
# Depends on openssl and ssss (http://point-at-infinity.org/ssss/)
# Copyright (c) 2012 Alex Beal
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to