Skip to content

Instantly share code, notes, and snippets.

@teepark
teepark / stdlib-shadows.sh
Created March 1, 2011 02:50
see which of your modules will shadow standard library modules
#!/usr/bin/env sh
EXCLUDES="htdocs mod py vendor tools client qa test"
stdlib_dir=`python -c 'import os; print os.path.dirname(os.__file__)'`
stdlib=`find $stdlib_dir -maxdepth 1 -name '*.py' -exec basename {} .py \;;\
find $stdlib_dir -maxdepth 2 -name __init__.py -exec dirname {} \; | xargs -L1 basename`
find `[ -n "$1" ] && echo $1 || echo .` \
-not \( -false \
@teepark
teepark / httpdate.py
Created March 24, 2011 22:05
parse any dates in HTTP headers
'''
parse these: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
'''
import datetime
import re
import pytz
#
@teepark
teepark / mapreduce.py
Created March 31, 2011 04:54
A quick MapReduce implementation on greenhouse and junction
import random
try:
import cPickle as pickle
except ImportError:
import pickle
import greenhouse
import junction
import select
import zmq
ctx = zmq.Context()
requester = ctx.socket(zmq.REQ)
replyer = ctx.socket(zmq.REP)
replyer.bind("inproc://testology")
requester.connect("inproc://testology")
yield """<!--
browsers won't actually display data as it comes in until there is a certain
minimal amount already received, so this prefix is just to send something to
push the browser over that limit so that the stuff we care about will be
rendered immediately.
nope, the previous paragraph wasn't enough (for chrome at least). so, what's
new with you? *sigh*, this forced conversation just feels so awkward.
it's ok, we ALREADY have around 40% of the padding we need! I think I'll take
import math
import sys
import traceback
def minus1(x):
return x - 1
def plus1(x):
return x + 1
class WeakMethodRef(object):
def __init__(self, method):
if getattr(method, "im_self", None):
self.obj = weakref.ref(method.im_self)
elif getattr(method, "im_class", None):
self.obj = weakref.ref(method.im_class)
else:
self.obj = None
if getattr(method, "im_func", None):
@teepark
teepark / sizeboundfile.py
Created May 27, 2011 00:55
length-limited file-like object
from cStringIO import StringIO
class SizeBoundFile(object):
"""a length-limiting file object wrapper
it reads from the underlying file object but never past the given size, so
it is suitable for use around a socket.makefile() file given a particular
Content-Length -- reading from this simulates EOF rather than reading into
the next message.
@teepark
teepark / gist:1003105
Created June 1, 2011 19:34
what's so fast about namedtuple?
$ python -m timeit -s "import collections; Point = collections.namedtuple('Point', ('x', 'y'));" "Point(5, 11)"
1000000 loops, best of 3: 0.739 usec per loop
$ python -m timeit "(5, 11)"
10000000 loops, best of 3: 0.0267 usec per loop
$ python -m timeit -s "import collections; p = collections.namedtuple('Point', ('x', 'y'))(5, 11)" "x, y = p"
10000000 loops, best of 3: 0.134 usec per loop
$ python -m timeit -s "p = (5, 11)" "x, y = p"
10000000 loops, best of 3: 0.0678 usec per loop
$ python -m timeit -s "import collections; p = collections.namedtuple('Point', ('x', 'y'))(5, 11)" "p.x"
10000000 loops, best of 3: 0.15 usec per loop
@teepark
teepark / gist:1154654
Created August 18, 2011 17:52
recommended additions to /etc/hosts
127.0.0.1 s.meebocdn.com
127.0.0.1 www.meebo.com
127.0.0.1 meebo.com