Skip to content

Instantly share code, notes, and snippets.

import time
from contextlib import contextmanager
@contextmanager
def easyprofile(msg):
before = time.time()
yield
print '%s took %0.2fsec' % (msg, time.time() - before)
from flask import Module, request, redirect, url_for, render_template, abort
from formalchemy import FieldSet
from example import db
from example import Service, User, Forum, Category, Topic, Post
mod = Module(__name__)
models = {
#!/usr/bin/env python
import httplib
import simplejson
# Based on Christian Dowski's iresponse http://blog.dowski.com/2008/04/02/reading-chunked-http11-responses/
class IterableResponse(httplib.HTTPResponse, object):
def __iter__(self):
assert self.chunked != httplib._UNKNOWN, "Response is not chunked!"
"""
I've been thinking lately about how perfect Redis would be for storing a
simple social graph. I posited that it would be relatively few lines of code,
and that it'd be clean code too. So here it is: a basic social graph built on Redis.
"""
class FriendGraph(object):
def __init__(self, ring):
self.ring = ring
import redis
from django.conf import settings
from django.core.signals import request_finished
try:
from eventlet.corolocal import local
except ImportError:
from threading import local
"""
jQuery templates use constructs like:
{{if condition}} print something{{/if}}
This, of course, completely screws up Django templates,
because Django thinks {{ and }} mean something.
Wrap {% verbatim %} and {% endverbatim %} around those
blocks of jQuery templates and this will try its best
@ericflo
ericflo / 00_logging.py
Created October 25, 2010 07:49
Log structured data about events that happen in your system (in JSON format for maximum flexibility).
# 00 prepended to filename so this shows up first in the list of gists.
import socket
import time
import simplejson
from django.conf import settings
def log_event(logger, event, request=None, data=None,
@ericflo
ericflo / redis_sessions.py
Created November 9, 2010 06:41
A Redis-based Django session store.
import simplejson
from django.contrib.sessions.backends.base import SessionBase, CreateError
from django_ext.redis_helper import get_redis
class SessionStore(SessionBase):
"""
A Redis-based session store.
@ericflo
ericflo / json_session_messages.py
Created November 15, 2010 05:18
Message backend which spits out dictionaries able to be json encoded/decoded, for those session backends which do not pickle.
from django.contrib.messages.storage.base import Message, BaseStorage
class JSONSessionStorage(BaseStorage):
"""
Stores messages in JSON-compatible format
"""
session_key = '_messages'
def _to_dict(self, message):
return {
@ericflo
ericflo / re_parts.py
Created November 20, 2010 23:18
An iterator which operates like re.split, except it takes a list of regexes, and lets you know which regex is responsible for each split (or whether that part of the split was the result of not matching any regex)
# Extracted from django-oembed, because I keep having a need for this snippet
import re
from heapq import heappush, heappop
def re_parts(regex_list, text):
"""
An iterator that returns the entire text, but split by which regex it
matched, or none at all. If it did, the first value of the returned tuple
is the index into the regex list, otherwise -1.