Skip to content

Instantly share code, notes, and snippets.

import re
class ReDict(dict):
def __setitem__(self, key, value):
dict.__setitem__(self, re.compile(key), value)
def __getitem__(self, key):
return [v for k,v in self.items() if k.match(key)]
from __future__ import division
from collections import namedtuple
Stats = namedtuple('Stats', 'count min max average variance')
def statgenerator(initial=None):
l = u = None # lower and upper values seen
n = v = a = 0 # count, variance, average (arithmetic mean)
q = 0 # running sum (see wikipedia)
from __future__ import division
from collections import namedtuple
Stats = namedtuple('Stats', 'count min max average variance')
def statgenerator(initial=()):
l = u = None # lower and upper values seen
n = v = a = 0 # count, variance, average (arithmetic mean)
q = 0 # running sum (see wikipedia)
from itertools import islice
from time import time
def after(n=0):
while 1:
n += 1
yield n
def filter_factors(n, g):
while 1:
from itertools import islice
def after(n=0):
while 1:
n += 1
yield n
def filter_factors(n, g):
while 1:
x = g.next()
>>> def delay(n):
... x = [None]*(n+1)
... i = 0
... while 1:
... x[i] = yield(x[i])
... i = (i + 1) % (n + 1)
...
>>> f = delay(3)
>>> f.next()
>>> f.send(1)
>>> def foo(x,y):
while 1:
x = yield(x)
y = yield(y)
>>> f = foo(1,2)
>>> f.next()
1
>>> f.send(3)
"Maximally functional" HTTP server
# High level
HTTPServer : (http_request, db) -> (response, db_delta)
# "Internals"
Resolve : http_request -> db_query
Lookup : db, db_query -> data
function I(x) { return x; }
function walk(obj, pre, post) {
var k, v;
obj = (pre||I)(obj);
if (typeof(obj) === 'object') {
for (k in obj) if (obj.hasOwnProperty(k)) {
v = (post||I)(walk(obj[k], pre, post));
if (v === undefined) {
delete obj[k];
goog.memoize = function(f, opt_serializer) {
var functionHash = goog.getHashCode(f);
var serializer = opt_serializer || goog.memoize.simpleSerializer;
return function() {
// Maps the serialized list of args to the corresponding return value.
var cache = this[goog.memoize.CACHE_PROPERTY_];
if (!cache) {
cache = this[goog.memoize.CACHE_PROPERTY_] = {};
}