Skip to content

Instantly share code, notes, and snippets.

@mdellavo
mdellavo / anagram.py
Created September 28, 2012 03:25
anagram generator
import sys
import random
import itertools
from collections import Counter
clean = lambda s: s.replace('\'', '').lower().strip()
def is_word(i):
i = i.strip()
return i and (len(i) > 1 or i == 'a')
@mdellavo
mdellavo / python-redis-object.py
Created September 6, 2012 17:53
Python objects backed by Redis
import redis
from datetime import datetime
import uuid
import time
import json
import datetime
KEY_TTL = 24 * 60 * 60
@mdellavo
mdellavo / python-restful.py
Created September 6, 2012 17:44
A library for working with a RESTful web service
import datetime
import requests
import json
class StubObject:
def __init__(self, **kwargs):
self.__dict__['data'] = {}
self.data.update(kwargs)
@mdellavo
mdellavo / redis-object.py
Created August 31, 2012 23:10
Convenience class to map objects to Redis keys
import uuid
gen_uid = lambda: uuid.uuid4().hex
class RedisObject:
keyspace = 'object'
def __init__(self, store, uid=None, ttl=KEY_TTL):
self.__dict__['store'] = store
self.__dict__['uid'] = uid or gen_uid()
@mdellavo
mdellavo / timer-context-manager.py
Created July 19, 2012 23:18
simple python timer context manager
class timer(object):
def __init__(self, text, n=1):
self.text = text
self.n = n
def __enter__(self):
self.t1 = time.time()
def __exit__(self, *args):
delta = time.time() - self.t1
@mdellavo
mdellavo / equal-column-height.js
Created June 28, 2012 13:46
equal column height
$(function() {
var $columns = $('.column ul');
var heights = $columns.map(function() { return $(this).height(); });
var tallest = Math.max.apply(null, $.makeArray(heights));
$columns.height(tallest);
});
@mdellavo
mdellavo / equal-column-height.js
Created June 28, 2012 13:46
equal column height
$(function() {
var $columns = $('.column ul');
var heights = $columns.map(function() { return $(this).height(); });
var tallest = Math.max.apply(null, $.makeArray(heights));
$columns.height(tallest);
});
@mdellavo
mdellavo / python-zmq-workers.py
Created June 12, 2012 17:21
python-zmq-worker-queue
import zmq
import logging
import uuid
MASTER_ENDPOINT = 'tcp://127.0.0.1:5000'
WORKER_ENDPOINT = 'tcp://127.0.0.1:5001'
log = logging.getLogger(__name__)
TASKS = {}
@mdellavo
mdellavo / jquery.format.js
Created May 24, 2012 13:16
Format phone number/credit card/ssn/etc inputs by regular expression
(function() {
var formatters = {
'phone' : [/^\(?\s?(\d{3})\s?\)?\s?\-?\s?(\d{3})\s?\-?\s?(\d{4})$/, '($1) $2-$3' ],
'ssn' : [/^(\d{3})\s?\-?\s?(\d{2})\s?\-?\s?(\d{3})$/, '$1-$2-$3' ],
'creditcard' : [/^(\d{4})\s?\-?\s?(\d{4})\s?\-?\s?(\d{4})\s?\-?\s?(\d{4})$/, '$1-$2-$3-$4']
};
jQuery.fn.formatWith = function(regex, replacement) {
$(this).change(function() {
this.value = this.value.replace(regex, replacement);
@mdellavo
mdellavo / mako-forms-bootstrap.html
Created May 7, 2012 15:14
Mako Form Lib for Bootstrap
<%def name="control_group(name, label, **kwargs)">
<div class="control-group ${'required' if kwargs.get('required') else ''}">
<label
class="control-label"
for="${kwargs.get('id') or name}">
${label}:
</label>
<div class="controls">