Skip to content

Instantly share code, notes, and snippets.

@ericmoritz
ericmoritz / counter.py
Created March 22, 2011 21:04
A counter stored in Riak that handles conflict resolutions on read
import logging
import riak
log = logging.getLogger(__name__)
class RiakCounter(object):
def __init__(self, bucket, key):
self.bucket = bucket
self.bucket.set_allow_multiples(True)
self.key = key
@ericmoritz
ericmoritz / greetings.py
Created March 23, 2011 19:51
My nano-framework
"""A simple little hello/goodbye example which takes advantage of urlvars"""
from nano import FrontController
import routes
from routes.middleware import RoutesMiddleware
from webob.dec import wsgify
from webob.exc import HTTPNotFound
from webob import Response
resources = {}
@ericmoritz
ericmoritz / README.rst
Created May 13, 2011 01:17
crazy-template.js

crazy_template

A template engine in 42 lines of code.

About

I was thinking of a way to generate HTML without using strings as embedding strings in Javascript is a pain if they are multi-lined. There isn't a nice triple quote like there exists in Python.

So I took my inspiration from Lisp HTML generators that use S-expressions to generate HTML. I thought, hell, S-expressions are just a bunch of nested lists, I could do the same thing in Javascript.

@ericmoritz
ericmoritz / README.rst
Created May 13, 2011 15:07
tnetstring shootout

I was a bit skeptical about a new data format call tnetstring. It claims to be both human and machine readable. They claim that a parser is easier to built for a tnetstring and therefore is better. In fact, while the implementation of the parser may be easier, human readability suffers with negligible speed gains.

I assert that if human readability is harmed in favor of speed, then you would be much better off using a binary solution such as Protobufs.

Results

Time is in milliseconds:

@ericmoritz
ericmoritz / comm.py
Created May 24, 2011 19:02
Simple Redis powered chat server for hyper-local chat.
import redis
import simplejson as json
import logging
import settings
import math
log = logging.getLogger(__name__)
#
# The following formulas are adapted from the Aviation Formulary
@ericmoritz
ericmoritz / demo.py
Created May 26, 2011 01:01
webtail
import eventlet
from eventlet import GreenPool
from eventlet.green import urllib2
import logging
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
class Reader(object):
def __init__(self, i):
{
"inputs": "goog",
"query": [
{"map": {"language": "javascript", "source": "function(v) { ejsLog('/tmp/reduce-test.log', 'map'); return [1]; }"}},
{"reduce": {"language": "javascript", "source": "function(v) { ejsLog('/tmp/reduce-test.log', 'reduce1: ' + v.length); return v;}"}},
{"reduce": {"language": "javascript", "source": "function(v) { ejsLog('/tmp/reduce-test.log', 'reduce2: ' + v.length); return v;}"}}
]
}
/*
Uses http://jsonselect.org/ to select fields from a riak object
*/
function map_jsonselect(values, keydata, arg) {
var data = Riak.mapValuesJson(values)[0];
if(typeof arg == "string") {
return JSONSelect.match(arg, data);
} else if(typeof arg == "object") {
var accum = {}
import time
from pyquery import PyQuery as pq
seen = set()
start = "http://www.reddit.com/r/pics/comments/i24rp/every_time_i_see_the_vancouver_kiss_submitted/c209bus"
next = start
while True:
if next in seen:
print "Recursion detected, abort!"
generating 0
mapping 0
generating 1
mapping 1
generating 2
mapping 2
generating 3
mapping 3
generating 4
mapping 4