Skip to content

Instantly share code, notes, and snippets.

@dcrosta
dcrosta / gist:1142407
Created August 12, 2011 16:27
launchd plist for mongodb
We couldn’t find that file to show.
# report will be something like:
#
# { test_name: 'signup button',
# alternatives: ['Red', 'Green'],
# results: [
# { attempted: 248,
# completed: 12
# },
# {
# attempted: 226,
@dcrosta
dcrosta / htmlabbrev.py
Created November 4, 2011 17:50
truncates HTML without screwing up tag nesting
import re
from HTMLParser import HTMLParser
whitespace = re.compile('(\w+)')
class HTMLAbbrev(HTMLParser):
def __init__(self, maxlength, *args, **kwargs):
HTMLParser.__init__(self, *args, **kwargs)
self.stack = []
from flask import Flask
app = Flask(__init__)
@app.route('/', methods=['GET'])
def getindex():
return 'You did a GET'
@app.route('/', methods=['POST'])
def postindex():
return 'You did a POST'
>>> def adder(amt):
... def inner(start):
... return start + amt
... return inner
...
>>> plusone = adder(1)
>>> plusone(4)
5
@dcrosta
dcrosta / gist:1578946
Created January 8, 2012 16:47
multiple-file context manager
class openFiles(object):
def __init__(self, *args):
if not all(isinstance(arg, tuple) for arg in args):
raise TypeError('arguments to openFiles must all be 2-tuples')
if not all(len(arg) in (1, 2) for arg in args):
raise TypeError('arguments to openFiles must all be 2-tuples')
self.args = args
def __enter__(self):
self.fhs = [file(*arg) for arg in self.args]
@dcrosta
dcrosta / puny.py
Created March 8, 2012 17:50
declarative, readable python testing
from functools import wraps
class Equalizer(object):
def __init__(self, rv):
self._rv = rv
def __eq__(self, other):
assert self._rv == other
class Puny(object):
@dcrosta
dcrosta / gist:2562350
Created April 30, 2012 20:17
A Python Mystery
Guess the output:
>>> def exec_code_object_and_return_x(codeobj, x):
... exec codeobj
... return x
...
>>> co1 = compile("""x = x + 1""", '<string>', 'exec')
>>> co2 = compile("""del x""", '<string>', 'exec')
>>> exec_code_object_and_return_x(co1, 1)
# What do you get here?
@dcrosta
dcrosta / fakemodule.py
Created August 4, 2012 03:14
Use a Python class as a fake module
import sys
class TheModule(object):
def __getattr__(self, name):
# you can do whatever you want with `name` here
print (self, name)
sys.modules[__name__] = TheModule()
-----> Heroku receiving push
-----> Python app detected
-----> Preparing Python interpreter (2.7.2)
-----> Creating Virtualenv version 1.7.2
New python executable in .heroku/venv/bin/python2.7
Not overwriting existing python script .heroku/venv/bin/python (you must use .heroku/venv/bin/python2.7)
Installing distribute..................................................................................................................................................................................................done.
Installing pip................done.
Overwriting .heroku/venv/bin/activate with new content
Overwriting .heroku/venv/bin/activate.fish with new content