Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
@simonw
simonw / jquery.comments.js
Created February 25, 2009 16:06
jQuery methods for accessing HTML comments and comment text. Only tested in Safari.
/* jQuery methods for accessing HTML comments and comment text
Only tested in Safari. */
jQuery.fn.comments = function() {
return this.contents().filter(function() {
return this.nodeType == 8;
})
};
jQuery.fn.commentText = function() {
var s = [];
@simonw
simonw / shorten-bookmarklet.js
Created April 12, 2009 12:32 — forked from singpolyma/shorten-bookmarklet.js
singpolyma's bit.ly version updated to detect rev=canonical on A elements as well
/* Expanded form of a bookmarklet for extracting rev=canonical OR tinyurling a page */
(function(){
var url=document.location;
var links=document.getElementsByTagName('link');
var found=0;
for(var i = 0, l; l = links[i]; i++) {
if (l.getAttribute('rev') == 'canonical' || (/alternate short/).exec(l.getAttribute('rel'))) {
found=l.getAttribute('href');
break;
}
@simonw
simonw / baseconv.py
Created April 11, 2009 10:32
Convert numbers from base 10 integers to base X strings and back again.
"""
Convert numbers from base 10 integers to base X strings and back again.
Sample usage:
>>> base20 = BaseConverter('0123456789abcdefghij')
>>> base20.from_decimal(1234)
'31e'
>>> base20.from_decimal('31e')
1234
(*
Open a new tab in Safari (and move focus to the URL bar)
I have this configured as a Quicksilver trigger for ctrl+alt+T
*)
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
# Code for using http://developer.yahoo.com/geo/placemaker/
import urllib, httplib2
h = httplib2.Http()
URL = 'http://wherein.yahooapis.com/v1/document'
API_KEY = 'your-Yahoo-API-key'
def placemaker(text, doctype='text/plain'):
head, response = h.request(URL, 'POST', headers={
# In settings.py:
import os
OUR_ROOT = os.path.realpath(os.path.dirname(__file__))
# In urls.py:
from django.conf import settings
import os
urlpatterns = patterns('',
# Django: validate that an uploaded file is a valid PDF
import pyPdf # from http://pybrary.net/pyPdf/
from pyPdf.utils import PdfReadError
class DocumentForm(forms.ModelForm):
pdf = forms.FileField()
class Meta:
model = Document
<script type="text/javascript">
(function(loc) {
if (loc.pathname == '/') {
return;
}
var uri_re =
/^(?:(?:[^:\/?#]+):)?(?:\/\/(?:[^\/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/;
var target_domain = '';
loc.href.replace(uri_re, function(all, path, query, frag) {
var dst, src; dst = src = path + (query ? '?' + query : '');
$ mkvirtualenv lxml-install-env
New python executable in lxml-install-env/bin/python
Installing setuptools...asy......... done.
(lxml-install-env)$ easy_install lxml
Searching for lxml
Reading http://pypi.python.org/simple/lxml/
Reading http://codespeak.net/lxml
Best match: lxml 2.2.2
Downloading http://codespeak.net/lxml/lxml-2.2.2.tgz
Processing lxml-2.2.2.tgz
import freebase # http://code.google.com/p/freebase-python/
def bulk_lookup(ids):
return freebase.mqlreadmulti([species_query(i) for i in ids])
def _species_query(id):
return {
'id': id,
'type': '/biology/organism_classification',
'name': None,