This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dict_flatten(d, delim='.', prefix=[]): | |
def pairs(d, prefix): | |
def flatten(k, v): | |
if isinstance(v, dict): | |
return pairs(v, k) | |
else: | |
return [(k, v)] | |
for k, v in d.iteritems(): | |
for e in flatten(prefix + [k], v): | |
yield e |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static <E> Iterable<Iterable<E>> chunker(final Iterator<E> stream, final long size) { | |
return new Iterable<Iterable<E>>() { | |
public Iterator<Iterable<E>> iterator() { | |
return new Iterator<Iterable<E>>() { | |
public boolean hasNext() { | |
return stream.hasNext(); | |
} | |
public Iterable<E> next() { | |
return new Iterable<E>() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
import sys | |
class ConsistencyError(Exception): | |
pass | |
class SudokuBoard(object): | |
def __init__(self): | |
self.cells = {} | |
for i in xrange(9): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import colorsys | |
MIN = -500 # m | |
MAX = 9000 # m | |
CYCLE = 500 # m | |
""" | |
make an elevation color palette suitable for global use, while still preserving | |
small variations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
alpha_lower = 'abcdefghijklmnopqrstuvwxyz' | |
alpha_upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
numeric = '0123456789' | |
punct = '''`-=[]\;',./''' | |
shift_punct = '!@#$%^&*()~_+{}|:"<>?' | |
shift_chars = set(c for c in alpha_upper + shift_punct) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import os.path | |
import sys | |
from lxml import etree | |
from lxml.builder import ElementMaker | |
E = ElementMaker() | |
def loadcsv(path): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import os.path | |
import re | |
import time | |
import json | |
# script to create an ssh tunnel from a public server back to a private (firewalled/NATted) machine; a poor man's VPN, basically | |
# how it works: | |
# private machine run this script at regular intervals, which attempts to ssh into public server; if successful, it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from xml.etree import ElementTree as et | |
import sys | |
from datetime import datetime | |
def _(tag): | |
return '{%s}%s' % ('http://www.topografix.com/GPX/1/1', tag) | |
def parse(f): | |
node = et.parse(f).getroot() | |
for p in node.findall('.//%s' % _('trkpt')): |