This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
from BeautifulSoup import BeautifulSoup, Tag, NavigableString | |
register = template.Library() | |
@register.filter | |
def enumerate_paras(html): | |
""" | |
Given a block of HTML, create id attributes on each | |
paragraph (p) and write in a link at the end of each. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function permalinks(id) { | |
var entry = document.getElementById(id); | |
var paras = entry.getElementsByTagName('p'); | |
for (var i=0; i < paras.length; i++) { | |
var p = paras[i]; | |
p.id = 'p' + i; | |
var a = document.createElement('a'); | |
a.href = '#p' + i; | |
a.title = 'Link to this paragraph'; | |
a.appendChild(document.createTextNode(' #')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
$('#entry-text > p').each(function(i) { | |
var p = $(this); | |
p.attr('id', 'p' + i); | |
var a = $('<a/>').attr('href', '#p' + i) | |
a.attr('title', 'Link to this paragraph'); | |
a.text(' #'); | |
p.append(a); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib, urllib2 | |
try: | |
import json | |
except ImportError: | |
import simplejson as json | |
BASE_URL = "http://www.documentcloud.org/api/search.json?" | |
def search(q, page=1, sections=False, annotations=False): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unique(items): | |
""" | |
Take a list or tuple. Return the same type containing only unique items. | |
>>> unique([1, 2, 2, 3]) | |
[1, 2, 3] | |
>>> unique((1, 2, 2, 3)) | |
(1, 2, 3) | |
""" | |
i = set(items) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fabric.api import * | |
def get_app_data(project_path=None, app=None): | |
if not project_path: | |
project_path = prompt("Where do we find your project?") | |
if not app: | |
app = prompt("What app do you want data from?") | |
with cd(project_path): | |
run('python manage.py dumpdata %s --format=json --indent=4 > %s.json' % (app, app)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sunlight import Sunlight, SunlightError, RESPONSE_KEYS | |
from tornado import escape, httpclient | |
class TornadoSunlight(Sunlight): | |
""" | |
Subclassing Sunlight (from simple-sunlight) to use Tornado's AsyncHTTPClient | |
""" | |
def __init__(self, *args, **kwargs): | |
super(TornadoSunlight, self).__init__(*args, **kwargs) | |
self.client = httpclient.AsyncHTTPClient() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymongo | |
import gevent | |
from gevent import monkey | |
monkey.patch_all() | |
jobs = [gevent.spawn(db.test2.insert, {'num': i}) for i in xrange(1000)] | |
gevent.joinall(jobs) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import csv | |
import json | |
def convert(fn, outfile): | |
""" | |
Open a filename, read it as CSV, | |
dump out a string of JSON to an outfile | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Models | |
var Tweet = Backbone.Model.extend({ | |
initialize: function(attrs) { | |
// add in a few helper attributes | |
if (attrs.text) { | |
this.set({ | |
html: twttr.txt.autoLink(attrs.text), | |
timestamp: Date.parse(attrs.created_at).setTimezone('GMT') |
OlderNewer