Skip to content

Instantly share code, notes, and snippets.

View eyeseast's full-sized avatar

Chris Amico eyeseast

View GitHub Profile
@eyeseast
eyeseast / paragraph_permalinks.py
Created February 7, 2010 18:02
Template filter to add paragraph-level permalinks to a block of HTML
@eyeseast
eyeseast / permalink-paras.js
Created February 10, 2010 16:12
JavaScript to add paragraph-level permalinks to some HTML
@eyeseast
eyeseast / permalinks.js
Created February 11, 2010 23:22
Add paragraph-level permalinks in JQuery
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):
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)
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))
@eyeseast
eyeseast / tornado_sunlight.py
Created September 12, 2010 17:15
A Sunlight subclass that uses Tornado's AsyncHTTPClient instead of urllib2
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()
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)
@eyeseast
eyeseast / csv_to_json.py
Created November 4, 2010 17:45
Dump a CSV file to a JSON string
#!/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
"""
@eyeseast
eyeseast / gist:723733
Created December 1, 2010 16:28
A tweet, as modeled in Backbone.js
// 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')