Skip to content

Instantly share code, notes, and snippets.

View peterbe's full-sized avatar

Peter Bengtsson peterbe

View GitHub Profile
@peterbe
peterbe / gist:630965
Created October 17, 2010 16:04
Use this instead of version numbering in your git projects
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 November 28, 2010 23:56
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'
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 February 5, 2011 13:14
EditDistanceMatcher function
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 February 5, 2011 13:15
Tests for EditDistanceMatcher
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
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 February 22, 2011 11:17
Use these instead of django.forms.Form or django.forms.ModelForm
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 April 6, 2011 12:24
Mixing class for dealing with HTTPS URL related stuff
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 April 7, 2011 07:24
local settings to do github oauth on localhost:8000
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 April 7, 2011 17:03
Starting tornado with preforking (multiple threads, one port)
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 April 15, 2011 01:27
Like an abstract class that requires you subclass certain methods but for settings. Just a thought
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")