Skip to content

Instantly share code, notes, and snippets.

@peterbe
peterbe / gist:630965
Created Oct 17, 2010
Use this instead of version numbering in your git projects
View gist:630965
import os, re
import logging
from subprocess import Popen, PIPE
def get_git_revision():
return _get_git_revision()
def _get_git_revision():
# this is actually very fast. Takes about 0.01 seconds on my machine!
home = os.path.dirname(__file__)
@peterbe
peterbe / setup.py
Created Nov 28, 2010
This doesn't work: When you pip install it you get: IOError: [Errno 2] No such file or directory: '/home/peterbe/bin/build/python-donecal/README.md'
View setup.py
from setuptools import setup
import sys, os
version = '0.2.3'
here = os.path.dirname(__file__)
def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()
def md2stx(s):
@peterbe
peterbe / edit_distance.js
Created Feb 5, 2011
EditDistanceMatcher function
View edit_distance.js
exports.EditDistanceMatcher = function(against, alphabet) {
this.against = against;
this.alphabet = alphabet || "abcdefghijklmnopqrstuvwxyz";
this.edits1 = function(word) {
var n = word.length;
var al = this.alphabet.length;
var set = new Array(), t;
// deletion
for (var i=0, len=n; i < len; i++) {
@peterbe
peterbe / test_edit_distance.js
Created Feb 5, 2011
Tests for EditDistanceMatcher
View test_edit_distance.js
var EditDistanceMatcher = require('../edit_distance').EditDistanceMatcher;
var testCase = require('nodeunit').testCase;
module.exports = testCase({
test_match: function(test) {
var edm = new EditDistanceMatcher(["peter"]);
test.equal(typeof edm.match('petter'), 'object');
test.equal(typeof edm.match('junk'), 'object');
// edm.match returns an array and remember,
// in javascript ['peter'] == ['peter'] => false
View tornado_route_decorator.py
from tornado.web
class route(object):
"""
decorates RequestHandlers and builds up a list of routables handlers
Tech Notes (or "What the *@# is really happening here?")
--------------------------------------------------------
Everytime @route('...') is called, we instantiate a new route object which
saves off the passed in URI. Then, since it's a decorator, the function is
@peterbe
peterbe / base_forms.py
Created Feb 22, 2011
Use these instead of django.forms.Form or django.forms.ModelForm
View base_forms.py
class _BaseForm(object):
def clean(self):
cleaned_data = super(_BaseForm, self).clean()
for field in cleaned_data:
if isinstance(cleaned_data[field], basestring):
cleaned_data[field] = \
cleaned_data[field].replace('\r\n','\n')\
.replace(u'\u2018',"'").replace(u'\u2019',"'").strip()
return cleaned_data
@peterbe
peterbe / gist:905558
Created Apr 6, 2011
Mixing class for dealing with HTTPS URL related stuff
View gist:905558
class HTTPSMixin(object):
def is_secure(self):
return self.request.headers.get('X-Scheme') == 'https'
def httpify_url(self, url=None):
url = url if url else self.request.full_url()
if url.startswith('/'):
parsed = urlparse(self.request.full_url())
return 'http://%s%s' % (parsed.netloc, url)
@peterbe
peterbe / local_settings.py
Created Apr 7, 2011
local settings to do github oauth on localhost:8000
View local_settings.py
OAUTH_SETTINGS = {
'client_id': '1ccb7f00082bc45047e7', # peterbe
'client_secret': 'b406117bf98898e0ffe1b1757e64f76a3c439bea',
'base_url': 'https://github.com/login/oauth/',
'redirect_url': 'http://www.peterbe.com/tornadogists/callback'
}
@peterbe
peterbe / gist:908208
Created Apr 7, 2011
Starting tornado with preforking (multiple threads, one port)
View gist:908208
def main(): # pragma: no cover
tornado.options.parse_command_line()
if options.showurls:
for path, class_ in route.get_routes():
print path
return
http_server = tornado.httpserver.HTTPServer(Application())
print "Starting tornado on port", options.port
if options.prefork:
@peterbe
peterbe / gist:920958
Created Apr 15, 2011
Like an abstract class that requires you subclass certain methods but for settings. Just a thought
View gist:920958
class LazyNotImplementedError:
def __init__(self, error_msg):
self.error_msg = error_msg
def __call__(self, *__):
raise NotImplementedError(self.error_msg)
__str__ = __add__ = __call__
# settings.py example
BASE = LazyNotImplementedError("You must set this in your settings_local")