Skip to content

Instantly share code, notes, and snippets.

//
// NSURLConnection+Blocks.h
// SteveHolt
//
// Created by Eric Florenzano on 10/19/11.
// Copyright (c) 2011 Boilerplate Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
@ericflo
ericflo / twitter_to_convore.py
Created February 20, 2011 10:02
A small script which takes keywords to track on Twitter and streams them live into a Convore topic.
import base64
import httplib
import threading
import urllib
import tweepy
CONVORE_BOT_USERNAME = ''
CONVORE_BOT_PASSWORD = ''
CONVORE_TOPIC_ID = '7612'
@ericflo
ericflo / backup_db.py
Created January 5, 2011 18:25
A management command to put on a cron to keep daily backups of a postgres database (with a 1-week window)
import datetime
import subprocess
from django.conf import settings
from django.core.management.base import BaseCommand
from boto.s3.connection import S3Connection
from boto.s3.key import Key
class Command(BaseCommand):
@ericflo
ericflo / twisted_proxy.py
Created November 22, 2010 01:11
Proxies local stuff, depending on whether it's /live/ or not.
#!/usr/bin/env python
from twisted.internet import reactor
from twisted.web import proxy, server
from twisted.web.resource import Resource
class ProxyResource(Resource):
def getChild(self, path, request):
request.received_headers['x-forwarded-host'] = request.received_headers['host']
@ericflo
ericflo / pycamo.py
Created November 22, 2010 01:00
Just makin' sure I know how to create a hex digest for Camo from Python
>>> import hashlib
>>> import hmac
>>> digest_maker = hmac.new('0x24FEEDFACEDEADBEEFCAFE', '', hashlib.sha1)
>>> digest_maker.update('http://farm5.static.flickr.com/4116/4857328881_fefb8e2134_z.jpg')
>>> digest_maker.hexdigest()
'2731d77b436b8a78f4cbe3624d8088fd5262f996'
@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.
@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 / 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 / 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,
"""
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