This file contains 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
-- Author: Michael-Keith Bernard | |
-- Date: August 10, 2012 | |
-- | |
-- Notes: This Trie implementation is a port of the Clojure implementation | |
-- below. I had to implement quite a few functions from clojure.core to keep the | |
-- same basic functionality. Probably very little if any of this code is | |
-- production worthy, but it's interesting all the same. Finally, all of the | |
-- documentation strings are pulled directly from clojure.core and may not | |
-- accurately reflect the Lua implementation. | |
-- |
This file contains 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 operator import attrgetter, itemgetter | |
from urlparse import urlparse, parse_qs | |
def compose(*fns): | |
def _comp(f, g): | |
def inner(*args, **kwargs): | |
return f(g(*args, **kwargs)) | |
return inner | |
return reduce(_comp, fns) |
This file contains 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
#!/usr/bin/env python | |
# Ported from clojure.pprint/print-table | |
from StringIO import StringIO | |
def format_row(headers, row, fmts): | |
out = [] | |
for col, fmt in zip([row[k] for k in headers], fmts): | |
out.append(fmt % col) |
This file contains 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
#!/usr/bin/env python | |
import sys | |
import yaml | |
import json | |
def convert(data): | |
y = yaml.load(data) | |
return json.dumps(y, | |
indent=4, |
This file contains 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 operator | |
from pprint import pprint | |
def is_dict(d): | |
return isinstance(d, dict) | |
def get(c, k, default=None): | |
try: | |
return c[k] | |
except (IndexError, KeyError, TypeError): |
This file contains 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
#!/usr/bin/env python | |
import csv | |
import argparse | |
from itertools import islice | |
def load_csv(filename, headers=None): | |
reader = csv.reader(open(filename, "rU")) | |
if headers is None: | |
headers = next(reader) |
This file contains 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 __future__ import print_function | |
import time | |
import contextlib | |
def format_checkpoints(cps): | |
pairs = [cps[i:i+2] for i in range(len(cps)-1)] | |
fmts = ["{0[0]} to {1[0]} in {2:.04f}s".format(start, end, end[1] - start[1]) | |
for start, end in pairs] | |
return ", ".join(fmts) |
This file contains 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 __future__ import print_function | |
import time | |
import functools | |
import contextlib | |
LOG_FMT = "{name}: {cp} @{total:0.4f}s ({dur:0.4f}s since {prev})" | |
def coroutine(f): | |
@functools.wraps(f) | |
def coro(*args, **kwargs): |
This file contains 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 StringIO import StringIO | |
from operator import attrgetter | |
import textwrap | |
class Node(object): | |
def __init__(self, val=None, *children): | |
self.val = val | |
self.children = list(children) | |
test1 = Node("foo") |
This file contains 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
#!/usr/bin/env python | |
# The MIT License (MIT) | |
# Copyright (c) 2016 Michael-Keith Bernard | |
# | |
# 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 use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is |