Skip to content

Instantly share code, notes, and snippets.

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
@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
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)
)
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 = [
@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:
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 / 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):
@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
CC=g++
CPPFLAGS=-O2 -Wall -Wextra -Werror -std=c++11
PROJ=homework2
BIN=$(PROJ)
SRCS=main.cpp vector3d.cpp
OBJS=$(subst .cpp,.o,$(SRCS))
$(BIN): $(OBJS)
$(CC) -o $@ $^
@patricklucas
patricklucas / hlhl.py
Created September 6, 2012 16:23
Half-Life "Half Life" Calculator
"""Functions for calculating when the game Half-Life has been out for exactly
half of someone's life.
"""
import datetime
release_date = datetime.date(1998, 11, 19)
def for_birthdate(birth_date):
"""Given a person's date of birth, return the date on which Half-Life has
been out for half of their life.