Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
import re
explicits = (
u'\u202a', # LEFT-TO-RIGHT EMBEDDING
u'\u202b', # RIGHT-TO-LEFT EMBEDDING
u'\u202d', # LEFT-TO-RIGHT OVERRIDE
u'\u202e', # RIGHT-TO-LEFT OVERRIDE
)
pdf = u'\u202c' # POP DIRECTIONAL FORMATTING
"""
Generator that crawls an Apache directory listing (automatically using the
?F=0 querystring parameter which causes Apache to return simpler HTML) and
yields file URLs one by one.
"""
import urllib, urlparse, re
always = lambda x: True
@simonw
simonw / random_index_with_bias.py
Created March 18, 2010 13:47
Given a list of scores, returns the index of one of those items at random, biased towards the items with the highest scores.
"""
Given a list of scores, returns the index of one of those items at random,
biased towards the items with the highest scores.
e.g. random_index_with_bias([84, 62, 61, 67, 45])
To show it works, run it 1,000,000 times:
>>> scores = {}
>>> for i in range(1000000):
/* Fade in/out a different background image every 60 seconds
Relies on the background image itself being applied to the html tag,
using classes bg1 through bg4 for four different background images.
*/
jQuery.fx.step.bgfadeout = function(fx) {
fx.elem.style.backgroundColor = 'rgba(255,255,255,' + fx.now + ')';
};
jQuery.fx.step.bgfadein = function(fx) {
fx.elem.style.backgroundColor = 'rgba(255,255,255,' + (1 - fx.now) + ')';
"""
A ridiculously simple term extractor - extracts sequences of one or more Title Case
Words, e.g. "Gordon Brown is the Prime Minister" would extract "Gordon Brown" and
"Prime Minister".
"""
import re
# Stopwords from http://snowball.tartarus.org/algorithms/english/stop.txt
stop_words = set(map(lambda s: s.strip(),
@simonw
simonw / node-reverse-proxy.js
Created April 19, 2010 12:56
A reverse proxy in Node - GET only at the moment
// node-reverse-proxy
var sys = require('sys'),
http = require('http');
function proxy(backend, request, response) {
var bits = backend.split(':');
var host = bits[0];
if (bits.length == 2) {
var port = parseInt(bits[1], 10);
# Fiddly.
def binary_search(haystack, needle, lo=0, hi=None, size=None):
if hi is None:
hi = len(haystack) - 1
if size is None:
size = len(haystack)
if size == 0:
return -1
t = ((hi - lo) / 2)
"For generating fake IP addresses"
import random
def make_ip_issuer(chance_of_a_repeat = 0.0):
issued_ips = []
def issue():
if issued_ips and random.uniform(0.0, 1.0) < chance_of_a_repeat:
return random.choice(issued_ips)
ip = '%s.%s.%s.%s' % (
"Extracts the text from every slide in a keynote presentation"
# Limitation: doesn't currently deal with markup nested inside a p tag, e.g.
# the following:
# <ns2:p ns2:list-level="1" ns2:style="SFWPParagraphStyle-990">Pen knife photo:
# herzogbr <ns2:link
# href="http://www.flickr.com/photos/80516279@N00/2274372747/"><ns2:span
# ns2:style="SFWPCharacterStyle-158"
# >http://www.flickr.com/photos/80516279@N00/2274372747/</ns2:span></ns2:link>
# </ns2:p>
"""
Simple timer class. Example usage:
t = Timer()
for url in ('http://www.google.com/', 'http://www.yahoo.com/'):
t.start('fetch url')
content = urllib.urlopen(url).read()
t.stop('fetch url')
t.start('reorder bytes')
bytes = list(content)