Skip to content

Instantly share code, notes, and snippets.

def groups_of(n, xs, **kwargs):
"""
Returns n elements from xs until empty.
Optional 'pad' keyword argument will pad a value to the resulting iterator
to meet
>>> for x in groups_of(2, [1, 2, 3, 4, 5]):
... print x
[1, 2]
[3, 4]
[5]
@carymrobbins
carymrobbins / mac_py_setup.sh
Created March 19, 2014 15:27
Python developer setup script for Mac OS X.
# TODO: Write the script! ;)
@carymrobbins
carymrobbins / get_failed_tests.py
Last active August 29, 2015 13:57
Simple helper to parse test runner results into a list of test names.
import re
def get_failed_tests(s):
"""Pass in the string output from your test runner to get the test names
in the format path.to.test.module:TestClass.test_name
"""
regex = re.compile(r'(FAIL|ERROR): (\w+) \(([^\)]+)\)')
return [
'{0[0]}.{0[1]}'.format(*((':'.join(path.rsplit('.', 1)), name)
@carymrobbins
carymrobbins / repr.js
Last active August 29, 2015 14:00 — forked from jacob414/repr.coffee
var repr = function (o, depth, max) {
var result, i;
depth = depth === undefined ? 0 : depth;
max = max === undefined ? 2 : max;
if (depth > max) {
return '<..>';
}
switch (typeof o) {
case 'string': return '"' + o.replace(/"/g, '\\"') + '"';
case 'function': return o.toString();
from itertools import izip
class TestCaseBase(unittest.TestCase):
def assertDictEqualVerbose(self, d1, d2, message=None):
"""Compare two dictionaries for equality showing exact differences.
:param Mapping d1: First dict to compare.
:param Mapping d2: Second dict to compare.
:param basestring message: Optional error message.
:raises AssertionError: if the two dicts are not equal.
@carymrobbins
carymrobbins / curry.js
Last active August 29, 2015 14:01
Function currying in JavaScript
var partial = function (f, partialArgs) {
return function () {
var args = [];
Array.prototype.push.apply(args, partialArgs);
Array.prototype.push.apply(args, arguments);
return f.apply(this, args);
};
};
var curry = function (f) {
@carymrobbins
carymrobbins / ack-limit
Last active August 29, 2015 14:01
Limit ack output. Helpful when finding matches in minified files but you don't want to see the entire file in the output.
#!/bin/sh
if [ -z "$1" ]; then
echo "Usage: ack-limit REGEX [OPTIONS]"
exit 1
fi
REGEX=$1
shift
ACK=$(which ack || which ack-grep)
@carymrobbins
carymrobbins / examples.js
Last active August 29, 2015 14:01
Algebraic Data Types for JavaScript.
var assert = require('assert'),
typed = require('./typed.js'),
data = typed.data,
matcher = typed.matcher;
// This will define a Maybe type with two constructors.
var Maybe = data(a => [Just(a), Nothing]);
// We can perform pattern matching on the constructor name.
var fromJust = matcher({ Just: x => x });
@carymrobbins
carymrobbins / retry
Last active November 3, 2020 10:38
Retry a bash command until successful.
#!/bin/bash
if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "" ]; then
echo "Usage: $(basename $0) n command"
echo "Execute command n times or until it succeeds (exit code 0)."
exit $([ $1 ] || echo 1)
fi
control_c() {
echo "$(tput setaf 1)Killing process per user request.$(tput sgr0)"
@carymrobbins
carymrobbins / limechat-to-text.js
Last active August 29, 2015 14:04
Copy Limechat discussion to HTML file, open it in the browser, then run this in the console to print the discussion in a simple text format.
var rows = [];
var xs = $('body').children;
for (var i = 0; i < xs.length; ++i) {
var ys = xs[i].children;
if (ys.length >= 3) {
rows.push([ys[1].textContent, ys[0].textContent, ys[2].textContent])
}
}
var maxNickLen = Math.max.apply(null, rows.map(function(r) { return r[0].length }));
console.log(rows.map(function(r) {