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 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)] |
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 __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) |
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 __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) |
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 itertools import islice | |
from time import time | |
def after(n=0): | |
while 1: | |
n += 1 | |
yield n | |
def filter_factors(n, g): | |
while 1: |
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 itertools import islice | |
def after(n=0): | |
while 1: | |
n += 1 | |
yield n | |
def filter_factors(n, g): | |
while 1: | |
x = g.next() |
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 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) |
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 foo(x,y): | |
while 1: | |
x = yield(x) | |
y = yield(y) | |
>>> f = foo(1,2) | |
>>> f.next() | |
1 | |
>>> f.send(3) |
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
"Maximally functional" HTTP server | |
# High level | |
HTTPServer : (http_request, db) -> (response, db_delta) | |
# "Internals" | |
Resolve : http_request -> db_query | |
Lookup : db, db_query -> data |
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
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]; |
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
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_] = {}; | |
} |