Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
@philikon
philikon / wtf8.py
Created February 8, 2011 08:22
WTF-8 codec for Python
# wtf8.py
import codecs
def encode(input, errors='strict'):
return input.encode('utf-8', errors).decode('latin-1', errors).encode('utf-8', errors), len(input)
def decode(input, errors='strict'):
return input.decode('utf-8', errors).encode('latin-1', errors).decode('utf-8', errors), len(input)
class StreamWriter(codecs.StreamWriter):
@misja
misja / country_detect.py
Created March 16, 2014 11:28
Get a country for a longitude / latitude point. World Borders Dataset to be found at http://thematicmapping.org/downloads/world_borders.php
import rtree
import cPickle
import osgeo.ogr
import shapely.wkb
import shapely.speedups
shapely.speedups.enable()
class FastRtree(rtree.Rtree):
def dumps(self, obj):
@justecorruptio
justecorruptio / 2048.annotated.c
Created April 9, 2014 09:52
Annotated version of 2048.c
/* Annotated version of 2048.c */
M[16], // The board
X=16, // Shorthand for 16
W, // Game over bit mask: 1=won, 2=not lost
k;
main(){
T( // Call main game logic
system("stty cbreak") // Set tty mode to not wait for enter key
@nyergler
nyergler / methodinspector.py
Created September 11, 2013 23:11
Use mock.patch to inspect calls to a method under test.
import collections
from mock import patch
MethodCall = collections.namedtuple(
'MethodCall',
('args',
'kwargs',
'return_value',
),
@vartec
vartec / install.txt
Last active April 22, 2020 01:30
Setting up new MBP
0. launch `term`
1. Install Homebrew (see http://brew.sh)
2. While Homebrew is installing this and following things, modify settings (see settings.txt)
3. `brew install cask`
4. `brew cask install iterm2`
5. Launch iterm2
6. `brew install python git` # pre-installed versions are old, see also https://opensource.com/article/19/5/python-3-default-mac
6.1. export PATH=/usr/local/bin:$PATH to make homebrew stuff take precedence
7. `brew cask install visual-studio-code slack google-chrome`
8. Powerline
@psychemedia
psychemedia / sqlite_utils_magic.ipynb
Last active October 22, 2020 06:36
Proof of concept sqlite_utils magic
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@simonw
simonw / http_get_headers_and_truncated_body.py
Created August 30, 2010 12:58
Do a GET against a URL, timing out sensibly, following up to a max number of redirects and pulling down only the first X bytes of the body
import httplib, urlparse, socket
def http_get_headers_and_truncated_body(
url, max_redirects=5, body_length=2048, timeout=5,
allowed_content_types = ('text/html',), num_redirects_followed=0,
redirect_chain = None
):
redirect_chain = redirect_chain or []
# Returns {'ok':True,'headers':{},'redirected':False,'url':'','body': '',
# 'max_redirects_reached': False, 'num_redirects_followed': 0,
@atomotic
atomotic / himalayandatabase.md
Last active December 5, 2021 13:31
himalayandatabase - from dbf to json api

himalayandatabase

from a Visual FoxPro GUI to json api with datasette

http://himalayandatabase.com

The Himalayan Database is a compilation of records for all expeditions that have climbed in the Nepalese Himalaya. The database is based on the expedition archives of Elizabeth Hawley, a longtime journalist based in Kathmandu, and it is supplemented by information gathered from books, alpine journals and correspondence with Himalayan climbers.

The Himalayan Database is a Microsoft Visual Foxpro 9 program.

Creating a redis Module in 15 lines of code!

A quick guide to write a very very simple "ECHO" style module to redis and load it. It's not really useful of course, but the idea is to illustrate how little boilerplate it takes.

Step 1: open your favorite editor and write/paste the following code in a file called module.c

#include "redismodule.h"
/* ECHO <string> - Echo back a string sent from the client */
int EchoCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
@border
border / long_polling.go
Created September 25, 2012 09:58
long polling for golang
package main
import (
"container/list"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"