Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
# 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
@simonw
simonw / install-lxml-osx-static-deps
Created August 14, 2009 11:43
A working recipe for installing lxml on OS X with static dependencies and no need for MacPorts or Fink
cd /tmp
wget http://codespeak.net/lxml/lxml-2.2.2.tgz
tar -xzvf lxml-2.2.2.tgz
cd lxml-2.2.2
cd libs/
wget ftp://xmlsoft.org/libxml2/libxml2-2.7.3.tar.gz
wget ftp://xmlsoft.org/libxml2/libxslt-1.1.24.tar.gz
cd ..
python setup.py build --static-deps --libxml2-version=2.7.3 --libxslt-version=1.1.24
sudo python setup.py install
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,
import freebase, json
freebase.search('tiger',
type = '/biology/organism_classification',
limit = 5,
mql_output = json.dumps([{
'id': None,
'name': None,
'/biology/organism_classification/scientific_name': None
}])
import datetime, time
def to_isoformat(dt):
return dt.isoformat().split('.')[0]
def from_isoformat(s):
s = s.split('.')[0]
return datetime.datetime(
*time.strptime(s, '%Y-%m-%dT%H:%M:%S')[:6]
)
@simonw
simonw / json_diffs.py
Created September 3, 2009 10:22
Returns a list of differences between two JSON objects - useful for smarter unit test error messages
def json_diffs(a, b, path='_'):
errors = []
if type(a) != type(b):
return ['%s: Type of a (%s) != type of b (%s)' % (path, type(a), type(b))]
if type(a) == dict:
for key in a:
new_path = path + ('[%s]' % key)
if key not in b:
errors.append('%s: Key %s present in a but not in b' % (new_path, key))
else:
svn co svn://svn.xapian.org/xapian/branches/geospatial xapian-geo
sudo apt-get install build-essential m4 perl python zlib1g-dev uuid-dev wget
sudo apt-get install libpcre3-dev
sudo apt-get install flex bison
cd xapian-geo/
./bootstrap
./configure --enable-maintainer-mode --enable-documentation=no --with-python
make
sudo make install
cd /usr/lib