Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
@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)
@simonw
simonw / gargoyle-exclude.md
Created August 25, 2015 20:32
How Gargoyle exclude conditions work
@simonw
simonw / article.html
Last active October 8, 2015 18:24
Comparing an article on the Guardian before and after it is processed by Google's amp.gstatic.com proxy. Visit https://gist.github.com/simonw/4a61dce8e6dc63ab3a60/revisions to see the difference between https://www.theguardian.com/us-news/2015/oct/08/kevin-mccarthy-drops-out-of-race-for-house-speaker/amp and https://amp.gstatic.com/v/www.theguar…
<!doctype html>
<html AMP>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
<meta name="format-detection" content="telephone=no"/>
<meta name="HandheldFriendly" content="True"/>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,minimal-ui">
@simonw
simonw / jsonlike.py
Last active October 15, 2021 23:59
Function for recursively checking that a Python data structure consists only of JSON-compatible primitives.
def is_jsonlike(obj):
# Recursively checks if obj is composed
# only of JSON-compatible primitives
t = type(obj)
if t in (str, unicode, int, float, bool, type(None)):
return True
elif t is dict:
for key, value in obj.items():
if type(key) not in (str, unicode):
return False
<p> <input type=time>
<p> <input type=date>
<p> <input type=datetime>
@simonw
simonw / mixins_demo.py
Created December 28, 2015 22:41
An illustration of how Python multiple inheritance / mixins work. I always forget the order in which super() calls other methods.
class CleanMixin(object):
def clean(self):
print " clean() in CleanMixin"
super(CleanMixin, self).clean()
class BaseClean(object):
def clean(self):
print " clean() in BaseClean"
@simonw
simonw / how-to-install-psycopg2-with-postgresqlapp-on-el-capitain.sh
Created January 15, 2016 23:25
How to install the Python psycopg2 PostgreSQL library on a brand new OS X El Capitain Mac using Postgres.app
# First, install Postgres.app from http://postgresapp.com/
# Next, install pip using the OS X system easy_install
sudo easy_install pip
# Now you can use pip to install psycopg2 - afteradding the Postgres.app bin directory to your path
PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.5/bin/ sudo pip install psycopg2
# The above PATH trick works using virtualenv as well
@simonw
simonw / remove-message.js
Last active May 15, 2025 20:37
JavaScript one-liner for removing a ?message=... parameter from the visible URL in the browser
history.replaceState && history.replaceState(
null, '', location.pathname + location.search.replace(/[\?&]message=[^&]+/, '').replace(/^&/, '?')
);
@simonw
simonw / gist:68d19a46e8edc2cd8c68
Last active October 13, 2021 00:04
Fix "Do you want the application "python" to accept incoming network connections?" by code signing the python executable in your virtualenv - copied here in case https://www.darklaunch.com/2014/02/02/fix-do-you-want-the-application-python-to-accept-incoming-network-connections ever goes away.
With the OS X firewall enabled, you can remove the "Do you want the application "python" to accept incoming network connections?" message.
Create a self-signed certificate.
Open Keychain Access. Applications > Utilities > Keychain Access.
Keychain Access menu > Certificate Assistant > Create a Certificate...
Enter a Name like "My Certificate".
Select Identity Type: Self Signed Root
Select Certificate Type: Code Signing
Check the Let me override defaults box
@simonw
simonw / heroku-nginx-logging.md
Last active January 22, 2019 18:41
How to get Heroku to log full user_agent strings using nginx

How to get Heroku to log full user_agent strings using nginx

Heroku's default logging format omits user agent and referrer strings - but these are useful to have! Especially if you want to be able to run your own analysis on what kind of browsers are using your service.

A default Heroku log line looks like this:

heroku/router:  at=info method=GET path="/page-on-your-site/"

host=your-site.herokuapp.com