Skip to content

Instantly share code, notes, and snippets.

@patricklucas
patricklucas / apper.py
Created March 2, 2012 19:15
Mini framework v2
import re
import webob
import webob.exc
class Servlet(object):
name = None
@patricklucas
patricklucas / apper.py
Created February 28, 2012 04:56
Mini framework
import re
import webob
import webob.exc
HTTP_METHODS = ('GET', 'POST', 'PUT')
class Apper(object):
from bottle import Bottl
import livesuggest
class MyApp(Bottle):
@get('/suggest/<prefix>')
def suggest(self, prefix):
self.request.content_type = 'application/json'
return livesuggest.prefix(prefix)
@patricklucas
patricklucas / trie.py
Created February 26, 2012 02:23
Python Trie
class TrieNode(object):
ident = None
children = None
def __init__(self):
self.children = {}
def insert(self, prefix, key=None):
if prefix:
import string
import sys
def perms(s):
letter_map = [c in string.letters for c in s]
num_letters = sum(letter_map)
num_perms = 2**num_letters
for perm in xrange(num_perms):
letter_perms = [
import sys
def perms(s):
num_perms = 2**len(s)
for perm in xrange(num_perms):
yield ''.join(
c.swapcase() if (1 << i & perm) else c
for i, c in enumerate(s)
)
@patricklucas
patricklucas / lrucache.py
Created January 25, 2012 20:30
LRU cache decorator
def LRUCache(max_size):
"""A simple dict-based LRU cache
The cached function must:
- Have no kwargs
- Have only hashable args
- If the decorated function is passed an unhashable arg, a TypeError
will be raised
- Be idempotent (be without side effects)
- Otherwise the cache could become invalid
class EmailVerification < ActiveRecord::Base
belongs_to :user
before_validation :default_values
UUID_GEN = UUID::Client.new '/tmp/uuid.sock'
validates :user_id, :presence => true
def verify!
self.verified = true