Skip to content

Instantly share code, notes, and snippets.

View SegFaultAX's full-sized avatar

Michael-Keith Bernard SegFaultAX

View GitHub Profile
@SegFaultAX
SegFaultAX / gist:3318559
Created August 10, 2012 22:12
Lua Trie
-- 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.
--
@SegFaultAX
SegFaultAX / gist:4073727
Created November 14, 2012 18:07
Functional URL Manipulation in Python
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)
@SegFaultAX
SegFaultAX / easy_table.py
Created February 26, 2013 20:24
Formats a list of dicts into a textual table.
#!/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)
#!/usr/bin/env python
import sys
import yaml
import json
def convert(data):
y = yaml.load(data)
return json.dumps(y,
indent=4,
@SegFaultAX
SegFaultAX / gist:10507478
Last active August 22, 2021 02:40
Dotted path expansion for Python dictionary keys
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):
@SegFaultAX
SegFaultAX / gist:9b05d9922ecd2b49c304
Last active August 22, 2021 02:40
CSV file splitter (each file after the split keeps the header line)
#!/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)
@SegFaultAX
SegFaultAX / gist:7916de1d008e94814257
Last active August 22, 2021 02:40
Elapsed time context manager with checkpoints
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)
@SegFaultAX
SegFaultAX / gist:d51da36f7eb65f58db7d
Last active August 22, 2021 02:40
Eager, re-entrant elapsed time logger
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):
@SegFaultAX
SegFaultAX / nested_table.py
Last active August 22, 2021 02:40
Draw nested ascii tables in Python
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")
@SegFaultAX
SegFaultAX / nginx_logs.py
Created January 23, 2017 23:24
Parse nginx logs to sqlite3
#!/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