Skip to content

Instantly share code, notes, and snippets.

@dsc
dsc / fix-setuptools.fish
Created November 30, 2010 06:53
fix-setuptools.fish: Reinstalls setuptools (as you'll inevitably do something silly, like `pip install -U distribute`)
#!/usr/bin/env fish
function fail
echo "FAIL. $argv" >&2
exit 1
end
not which python; and fail "Cannot find python on your path. Are you daft?"
set -l ver (python -c 'import sys; print sys.version[0:3]')
@dsc
dsc / closest_k_in_r3.py
Created September 14, 2011 00:45
Find the k closest points in R3
#!/usr/bin/env python
import heapq
from collections import namedtuple
Point = namedtuple('x y z')
PointState = namedtuple('dist point')
class ClosestKPoints(object):
@dsc
dsc / bind.js
Created November 8, 2011 22:17
Function.prototype.bind
// See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
// for a more verbose-but-standards-compliant version.
if (!Function.prototype.bind) {
Function.prototype.bind = function(context){
var fn = this, args = [].slice.call(arguments, 1);
return function(){
return fn.apply( context, args.concat([].call(arguments)) );
};
};
}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from IPython.core import ipapi
ip = ipapi.get()
### Auto-Activate VirtualEnv
import os, sys
@dsc
dsc / comb.py
Last active October 3, 2015 06:18
kcomb
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Write me a function that takes a collection S and an integer k, and returns a
collection of all collections such that each is a sub-collection of S with size k.
- S is a set-like structure that guarantees these collections contain totally
ordered and unique values; importantly, [1,2] is the same as [2,1].
- You can make sets as needed.
- They support indexing and slicing, as well as iteration.
@dsc
dsc / combi.py
Created April 27, 2012 17:56
testcomb.py
# when you asked the question, this is what popped in my head;
# a solution to abstracting the requested problem into indexes
def combi(n, k):
r = []
for i in range(n - k + 1):
for j in range(i + 1, n - k + 2):
s = [i]
for m in range(k - 1):
s.append(j + m)
@dsc
dsc / modelhaiku.py
Created June 22, 2012 20:31
modelhaiku.py
from sqlalchemy import *
class Haiku(Model):
id = Column(Integer, primary_key=True)
ctime = Column(DateTime, default=func.now())
line1 = Column(Text())
line2 = Column(Text())
line3 = Column(Text())
query = (Session.query(Haiku)
#!/bin/bash
for repo in $(cat repos.txt); do
repodir=$(perl -pe 's!/!-!g;' -e 's/^analytics-//' <<< "$repo")
echo git clone --mirror ssh://gerrit.wikimedia.org/$repo.git $repodir
if git clone --mirror ssh://gerrit.wikimedia.org/$repo.git $repodir; then
echo
else
break
fi
done
@dsc
dsc / events.coffee
Created September 17, 2012 19:28
events.co
# Technically, this is Coco, not CoffeeScript, but close enough.
{ toString:objToString } = Object::
isArray = Array.isArray or (o) -> objToString.call(o) is '[object Array]'
slice = [].slice
class exports.EventEmitter
@dsc
dsc / fix_block_indentation.py
Created September 19, 2012 19:35
Fix Block Indentation TextMate Command
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# TextMate command to correct block indentation for languages with
# semantic whitespace (like Python or CoffeeScript).
#
# Command settings:
# input: Selected Text or Document
# output: Replace Selected Text
# activation: cmd+shift+R