Skip to content

Instantly share code, notes, and snippets.

View stuartlangridge's full-sized avatar

Stuart Langridge stuartlangridge

View GitHub Profile
@stuartlangridge
stuartlangridge / example.js
Last active March 4, 2024 19:04
Exporting an SVG from Figma and identifying which SVG elements in the exported file correspond to which objects in Figma
const getSvgNamesForNodes = () => {
// this is guesswork. When Figma has two nodes with the same name (say, "Union"),
// it serialises them into SVG with names "Union" and "Union_2", but it's not
// clear which one becomes "Union" and which "Union_2". It is theorised that this
// is done in the same order that .findAll returns, so we make use of that.
// Figma might decide to change this at any time, of course. It would be much
// nicer if there were an SVG export option which also serialised getPluginData()
// data as data-* attributes in the output SVG, but there isn't, yet.
let names = {};
let nameIndices = {};
import numpy as np
import glob
import markdown
import re
from bs4 import BeautifulSoup
def make_pairs(corpus):
for i in range(len(corpus)-1):
yield (corpus[i], corpus[i+1])
@stuartlangridge
stuartlangridge / README.md
Last active November 25, 2020 18:30
Gimp 2.8 Sprite Sheet from layer groups (python-fu)

A thing to convert a Gimp image of multiple layered sprites into a sprite sheet.

The way I make pixel art sprites is this: imagine you're drawing, say, a knight. I have all the frames for that knight in one great big Gimp XCF image, organised into groups.

So there's a layer group called "Running"; this defines a whole sprite, in multiple frames. In there, there's a set of layer groups, one per frame, called "Running 1" to "Running 9" (for example). In "Running 1", there's one or more layers which all go together to form that one single frame. This works out neatly because you can make an individual frame out of a few different parts, and edit those parts separately. This makes it easy to copy unchanging parts from one frame to the next, while just changing the bits that are different.

@stuartlangridge
stuartlangridge / mpnames.py
Created January 31, 2018 17:22
A simple script to count UK MPs, since Parliament began, by first name, so I could post https://twitter.com/sil/status/958750576900296706
#!/usr/bin/env python3
"""Daft script to count MPs by first name.
Expects to find people.json from https://github.com/mysociety/parlparse/tree/master/members
Run as "python3 mpnames.py Keir" or similar.
Written to answer Jess Phillips' question at https://twitter.com/sil/status/958750576900296706
"""
import json, pprint, sys
fp = open("people.json", encoding="utf-8")
data = json.loads(fp.read())
fp.close()
~/Scra+/ralight-configs $ python3 -c "import json; print(json.load(open('broken-config.json')))"
[snip]
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 10 column 20 (char 151)
~/Scra+/ralight-configs $ python3 -c "import yaml; print(yaml.safe_load(open('broken-config.yaml')))"
[snip]
yaml.scanner.ScannerError: while scanning a simple key
in "broken-config.yaml", line 9, column 5
could not found expected ':'
in "broken-config.yaml", line 10, column 18
@stuartlangridge
stuartlangridge / coalition2017.py
Created June 9, 2017 10:25
Work out possible coalitions for the UK 2017 general election
"""2017 general election
Given the vote totals, we have a hung parliament. Work out how coalitions
might be put together and display possibilities.
"""
import itertools
# vote totals correct as of about 10.30am Friday 9th June 2017
votes= {
'Conservative': 318, 'Sinn Fein': 7, 'DUP': 10,
'Sylvia Hermon': 1, 'Plaid Cymru': 4, 'Labour': 261, 'SNP': 35,
@stuartlangridge
stuartlangridge / unsong-initials-lcs.py
Created May 7, 2017 13:38
Calculate longest subsequences from Unsong's chapter initials to get a hint about what, if anything, it's a notarikon for
#!/usr/bin/env python3
"""Parses the text of Unsong, and the list of chapter initials, and finds
longest common subsequences. The idea here is that if the notarikon formed
by the first letter of each chapter conceals a secret message, then perhaps
parts of that message have already been revealed in the book... so if there
were "NIEAC" is somewhere in the list of chapter initials, then that probably
stands for "Nothing Is Ever A Coincidence", as per the many times that phrase
is mentioned. Idea from 75thTrombone at
https://www.reddit.com/r/unsong/comments/69k1im/preemptive_final_exam_suggestion/
This script calculates those substrings. The answers are not all that promising.

Keybase proof

I hereby claim:

  • I am stuartlangridge on github.
  • I am sil (https://keybase.io/sil) on keybase.
  • I have a public key ASDGq9mbJlRG62EgMM5EdILRa4w4G3NimkeoCWxL6bQbQwo

To claim this, I am signing this object:

@stuartlangridge
stuartlangridge / danmessages.py
Created March 9, 2017 21:33
Simple example of running a background thread to print things to output, a foreground thread to receive messages, and a queue to communicate
# Import standard python modules.
import sys
import time
import threading
import os
import queue
# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient
@stuartlangridge
stuartlangridge / Wallpaperify
Created March 29, 2016 22:41
Simple Nautilus wallpaperify script, as per https://plus.google.com/u/0/+joeysneddon/posts/a3i2L9dyESp. (Save as $HOME/.local/share/nautilus/scripts/Wallpaperify.)
#!/bin/bash
file --brief --mime-type "$1" | grep "^image/"
if [ $? != 0 ]; then
zenity --error --text="'$1' does not seem to be an image file."
exit 1
fi
if ! hash convert 2>/dev/null; then