Skip to content

Instantly share code, notes, and snippets.

@dsc
dsc / stress.py
Created July 19, 2010 20:40
Modified stress.py for testing Cassandra
#!/usr/bin/env python
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
@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)) );
};
};
}
@dsc
dsc / ordered_json.py
Created December 22, 2011 22:05
Python JSON decoder that uses OrderedDict for JSON Objects.
import json
from collections import OrderedDict
ordered_decoder = json.JSONDecoder(object_pairs_hook=OrderedDict)
class OrderedJSONDecoder(json.JSONDecoder):
""" As `JSONDecoder`, but passing `collections.OrderedDict`
for `object_pairs_hook` by default.
"""
#!/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 / dsync.sh
Created May 23, 2012 17:54
dsync -- Copy files to many hosts in parallel
#!/bin/bash
#/ dsync -- Copy files to many hosts in parallel.
#/
#/ Usage: dsync [options] -- [RSYNC_OPTIONS] SRC[...] DEST
#/
#/ `dsync` copies files (using `rsync`) to many hosts in parallel.
#/
#/ Where possible, `dsync` follows semantics of `dsh` and `rsync` -- it
#/ understands `~/.dsh/groups`, and uses `dsh`-like flags for specifying
@dsc
dsc / git-fat-pack.sh
Created June 13, 2012 17:53
Permanently remove crap from a git repo.
#!/bin/bash
#set -x
# Shows you the largest objects in your repo's pack file.
# Written for osx.
#
# @see http://stubbisms.wordpress.com/2009/07/10/git-script-to-show-largest-pack-objects-and-trim-your-waist-line/
# @author Antony Stubbs
# set the internal field spereator to line break, so that we can iterate easily over the verify-pack output