Skip to content

Instantly share code, notes, and snippets.

@mtth
mtth / sql2csv
Created September 26, 2013 06:09
Parse SQL dump into csv.
#!/usr/bin/env bash
# convert sql dumps to csv, tsv, ssv...
# also can filter fields using a sed pattern
# only works on linux
function usage
{
echo "usage: $0 [-sc CORES] [-o OUTFILE] [-p PATTERN] SQLFILE" 2>&1 && exit 1
}
@mtth
mtth / gist:8637593
Created January 26, 2014 19:04
Python descriptors.
class Desc(object):
def __get__(self, obj, type=None):
return 'd'
def __set__(self, obj, value):
pass
class A(object):
pass
@mtth
mtth / gist:9458430
Created March 10, 2014 02:17
Late variable binding in python
#!/usr/bin/env python
# encoding: utf-8
"""Testing late binding of variables."""
# last value of j is used everywhere
bars = [lambda i: j * i for j in range(5)]
assert all(b(1) == 4 for b in bars), "for lambda functions"
#!/usr/bin/env python
# encoding: utf-8
"""Python numbers magic!
https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/
Adapted for python 2.7.
"""
@mtth
mtth / table.c
Created December 12, 2014 04:04
/**
* https://news.ycombinator.com/item?id=8737349
*
*/
#include <stdlib.h>
#define SIZE 1024
/**
* Benchmark functions serially.
*
* This is useful for async functions which yield too often to the event
* loop to be correctly benchmarked.
*
*/
function Benchmark() {
var fns = {};
@mtth
mtth / batch.js
Created February 14, 2015 20:39
BatchReadable stream
var stream = require('stream'),
util = require('util');
/**
* Batch loaded readable stream.
*
* @param {Function} `fn(cb)`, where `cb(err, iter)`. Function used to feed
* the stream. `iter` should either be an iterator or `null` to signal EOT.
* @param {Object} `opts` Options forwarded to `stream.Readable`, along with
* the following: `batchHighWatermark`, the maximum number of batches
@mtth
mtth / LazyIterator.js
Last active August 29, 2015 14:15
Lazy asynchronous iterator
/* jshint browser: true */
(function (root) {
'use strict';
/**
* Simple lazy asynchronous iterator.
*
* Lazily iterate over a collection served from a standard REST endpoint.
* Elements are loaded by batch (of size `highWaterMark`), and
@mtth
mtth / DuplexIterable.js
Created February 24, 2015 01:12
Bidirectional REST iterable.
/* jshint browser: true, node: true */
(function (root) {
'use strict';
/**
* Bidirectional lazy loader.
*
* This class exposes a very simple API (`reset`, `next`, and `prev`) via
* which we can iterate (lazily and in both directions!) over a traditional
def indices(m, n):
"""Index generator to visit a 2-dimensional matrix in increasing i + j.
:param m: Number of rows.
:param n: Number of columns.
"""
for i in range(m):
for j in range(min(n, i + 1)):
yield (i - j, j)