Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
@simonw
simonw / demo.js
Created March 30, 2017 22:34
Mucking around with http://dev.apollodata.com/
// Apollo Client lets you attach GraphQL queries
// to your UI components to easily load data
const FeedWithData = graphql(gql`{
feed (type: TOP, limit: 200) {
repository {
name, owner { login }
# Uncomment the line below to get stars!
# stargazers_count
}
@simonw
simonw / README.md
Created April 14, 2017 06:00
The readme file that is automatically created for any new create-react-app project
@simonw
simonw / export-google-docs-to-restructured-text.js
Last active September 19, 2025 04:18
Google Apps script to convert a Google Docs document into reStructuredText
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('Convert to .RST')
.addItem('Convert to .RST and email me the result', 'ConvertToRestructuredText')
.addToUi();
}
// Adopted from https://github.com/mangini/gdocs2md by Renato Mangini
// License: Apache License Version 2.0
String.prototype.repeat = String.prototype.repeat || function(num) {
@simonw
simonw / placetypes_tree.json
Created June 15, 2017 23:20
whosonfirst placetypes tree - for each whosonfirst placetype, shows what other placetypes are allowed to be its direct children
{
"building": [
"venue"
],
"county": [
"postalcode",
"localadmin",
"locality"
],
"borough": [
@simonw
simonw / recursively_find_bad_characters.py
Last active June 28, 2017 18:29
I use Atom and the flake8 plugin, and I was getting a barrage of flake8 UnicodeDecode errors. I couldn't figure out why, so I wrote this script to recursively scan all of the files in a directory and show me which ones had unexpected non-ascii characters in them based on a whitelist. This helped me track down the file causing the problem.
import os
import re
skip_paths = (
'.git',
'.DS_Store',
'.bz2',
)
bad_chars_re = re.compile(
r'[^a-zA-Z0-9_\(\),\.\s"%\':;=@\[\]`\{\}\-!#\*\+/<>\?\$&\\\^\|~]'
@simonw
simonw / google-sheets-unix-timestamp-formula.txt
Created June 30, 2017 19:46
How to convert a unix timestamp to a human-readable datetime string in Google Sheets
=((A1-21600000)/86400000)+25569
# Then select the column and use Format -> Number -> Date time
@simonw
simonw / transform_cleaned_to_openelex.py
Created July 2, 2017 19:24
Script for converting my cleaned-up CSV version of the 2006 TN Governor election results into openelections-compatible format.
import csv
import argparse
import sys
def convert_cleaned_to_openelex(filepath, office='Governor'):
yield (
'county', 'precinct', 'office', 'district',
'candidate', 'party', 'votes'
)
@simonw
simonw / python-virtual-environment-howto.md
Created July 18, 2017 23:53
How to set up a Python virtual environment

How to set up a Python development environment

A Python development environment is a folder which you keep your code in, plus a "virtual environment" which lets you install additional library dependencies for that project without those polluting the rest of your laptop.

mkdir my-new-python-project
cd my-new-python-project
virtualenv --python=python2.7 venv

This will create a my-new-python-project/venv folder

@simonw
simonw / tcpdump-elasticsearch-docker.txt
Created July 24, 2017 23:36
Using tcpdump to spy on an elasticsearch docker container
# First, attach to the docker container
bay attach elasticsearch
# Now install tcpdump
apt-get update && apt-get install tcpdump
# Now run it, sniffing all traffic to port 9200
tcpdump -A -s 0 'tcp port 9200 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
@simonw
simonw / example_output.json
Created August 3, 2017 23:43
Python function for retrieving all git commits (from "git log" in the current directory) as a useful data structure
[
{
"date": "Mon Jan 23 13:03:00 2017 -0800",
"message": "Recommended by Django https://docs.djangoproject.com/en/1.10/ref/databases/#setting-sql-mode",
"title": "SET sql_mode='STRICT_TRANS_TABLES';",
"hash": "a395f401c9a6d3e8602b80ecc729d736da871f46",
"author": "Simon Willison <simonw@example.com>"
},
{
"date": "Mon Jan 23 11:37:38 2017 -0800",