Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
with snippets as (
select
substr(body, instr(body, 'refcode=') + 8, 128) as snippet
from
emails
where
body LIKE '%refcode%'
),
refcodes as (
select
@simonw
simonw / Weird CSV sniffing error.ipynb
Created December 7, 2021 00:12
Notebook illustrating a strange edge-case I found with Python's CSV dialect sniffer
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Single File Web Component</title>
<style>
body {
background-color: #eee;
font-family: Helvetica, sans-serif;
}
@simonw
simonw / twitter-to-sqlite-with-datasette-desktop.md
Created October 26, 2021 19:52
Running twitter-to-sqlite using Datasette Desktop

Running twitter-to-sqlite using Datasette Desktop

Here's how to run twitter-to-sqlite on macOS without having to first installing Python, using the Datasette Desktop macOS application.

  1. Install Datasette Desktop - it's a Mac application that you download
  2. Launch the application once. This will set up a Python environment completely independent of anything else you may have on your machine, ideally avoiding the horrors of XKCD 1987.
  3. Install twitter-to-sqlite by running this command: ~/.datasette-app/venv/bin/pip install twitter-to-sqlite
  4. Now you can run twitter-to-sqlite by following that tool's documentation - but each time you run it you'll need to use this path: ~/.datasette-app/venv/bin/twitter-to-sqlite
@simonw
simonw / output.txt
Created August 24, 2021 23:56
Output of "sqlite-utils memory global_power_plant_database.csv --analyze"
global_power_plant_database.country: (1/25)
Total rows: 33643
Null rows: 0
Blank rows: 0
Distinct values: 167
Most common:
8688: USA
@simonw
simonw / urls.py
Last active August 14, 2021 19:23
Partial Django code for Google OAuth flow without JavaScript (Apache 2.0 license)
from django.urls import path
from app.views import complete_google_login, start_google_login
urlpatterns = [
path("login/google/", start_google_login),
path("login/google/complete/", complete_google_login),
]
@simonw
simonw / type_tracker.py
Created June 15, 2021 03:25
Experiment to guess the types of CSV data
import csv, re
def is_float(s):
try:
float(s)
return True
except ValueError:
return False
@simonw
simonw / README.md
Created June 14, 2021 16:00
alnum encoding scheme

alnum encoding scheme

The goal is to be able to take any Python string and reversibly convert it into a string that consists only of a-zA-Z9-0_ characters.

>>> alnum_encode("hello.csv")
'hello_2e_csv'
>>> alnum_encode("this é has ü accents")
'this_20__e9__20_has_20__fc__20_accents'
@simonw
simonw / auto-fill-slug-from-title.js
Created May 8, 2021 15:04
Pre-fill the slug form field based on the title, but stop doing that if the slug is manually edited
function slugify(s) {
return s
.toLowerCase()
.replace(/[^-\w\s]/g, "") // remove non-alphanumerics
.trim()
.replace(/[-\s]+/g, "-") // spaces to hyphens
.replace(/-+$/g, ""); // trim trailing hyphens
}
function slugAutoFill() {
@simonw
simonw / jq-helicopter.md
Created March 12, 2021 19:32
jq to reshape a helicopter trace