Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
@simonw
simonw / baseconverter.py
Created February 27, 2014 01:23
A class for converting integers to encoded strings and back again.
"""
Convert numbers from base 10 integers to base X strings and back again.
Sample usage:
>>> base20 = BaseConverter('0123456789abcdefghij')
>>> base20.from_int(1234)
'31e'
>>> base20.to_int('31e')
1234
@simonw
simonw / update_pixstar.py
Last active August 29, 2015 13:57
Our pixstar album frame stopped automatically refreshing (it's meant to pull our PhotoRSS feed every 6 hours), so I wrote a script to click the "refresh" button.
"""
1. GET
extract csrf token <input type='hidden' name='csrfmiddlewaretoken' value='1RNvCJoYdwKwtoOAd7kM73sBKJxhzN50' />
2. POST https://www.pix-star.com/accounts/login/
next:/
username:...
password:...
csrfmiddlewaretoken:1RNvCJoYdwKwtoOAd7kM73sBKJxhzN50
- keep the cookies
3. POST album ID to https://www.pix-star.com/album/is2/downloading2/
@simonw
simonw / python_logging.py
Created December 4, 2014 23:22
How to make Python logging output everything to the console
import logging, sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
@simonw
simonw / splunk_user_agents.py
Created January 5, 2015 19:22
Generate splunk query for extracting user agents
browser_mapping = (
('MSIE 7.*Trident/4.0', 'Internet Explorer 8.0'),
('MSIE 6.0', 'Internet Explorer 6.0'),
('MSIE 7.0', 'Internet Explorer 7.0'),
('MSIE 8.0', 'Internet Explorer 8.0'),
('MSIE 9.0', 'Internet Explorer 9.0'),
('MSIE 10.0', 'Internet Explorer 10.0'),
('Trident/7.0; rv:11.0','Internet Explorer 11.0'),
('droid', 'Android'),
('Chrome', 'Chrome'),
@simonw
simonw / example.sql
Created January 27, 2015 22:55
Useful MySQL pattern for quickly finding the most recent 10 rows that match a complex where clause in a giant table
select * from Table FORCE INDEX (primary)
where /* lots of complex where clauses */
order by id desc limit 10;
from django.conf import settings
from django.http import HttpResponse
from django.conf.urls import url
settings.configure(
DEBUG = True,
SECRET_KEY = 'yourrandomsecretkey',
ROOT_URLCONF = __name__,
MIDDLEWARE_CLASSES = (),
)
@simonw
simonw / tryout.sh
Created July 24, 2015 23:48
Script for quickly trying out Python modules - run "tryout.sh jsonschema" to pip install jsonschema into your scratch virtualenv and run ipython with that module loaded.
#!/bin/bash
# Create scratch with 'virtualenv --python=python2.7 scratch && scratch/bin/pip install ipython'
scratch/bin/pip install $1
scratch/bin/ipython -c "import $1; print '\nimported $1'" -i
@simonw
simonw / oldest-lines-example-output.txt
Last active August 29, 2015 14:26
How to order the lines in a file by their age according to git blame (useful for checking things like which are the oldest lines in a Python requirements.txt file)
2014-10-22: https://pypi.python.org/packages/source/l/librabbitmq/librabbitmq-1.5.2.tar.gz#md5=842aea204fcfb5d7a541ae72d5ad38bc
2014-10-28: https://pypi.python.org/packages/source/r/raven/raven-3.5.2.tar.gz#md5=a3fe6c823d01cca0802b78d43cc4953a
2014-10-31: https://pypi.python.org/packages/source/e/exam/exam-0.10.5.tar.gz#md5=cb5a5848f3779283054d5556a6c16f55
@simonw
simonw / profile-this.py
Last active August 29, 2015 14:27 — forked from andrewgodwin/profile-this.py
Handy profiler pattern
import cProfile, pstats, StringIO
pr = cProfile.Profile()
pr.enable()
# ... do something ...
pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
function generateChart(figures) {
// figures is an object mapping labels to numbers
var cht = 'p'; // Chart type: pie
var chs = '460x200'; // Image dimensions
var chd = []; // Chart data
var chl = []; // Corresponding labels
var min = 0;
var max = 0;
$.each(figures, function(label, value) {
chl[chl.length] = label;