Skip to content

Instantly share code, notes, and snippets.

@tobywf
tobywf / save-gists.py
Created April 7, 2020 03:33
Save all gists from GitHub, including comments
"""
Save all gists from GitHub, including comments.
Requires the PyGithub library (``pip install PyGithub>=1.47``).
Pass a personal access token with the scope "gist" as the first
argument.
This probably won't work for large files (+10MiB), since they aren't
returned by the GitHub API, and must be cloned instead.
@tobywf
tobywf / sbix_extract.py
Created April 6, 2020 04:54
Extract SBIX glyphs from a font
# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6sbix.html
# requires fonttools lib (`pip install fonttools>=4.7.0`)
import sys
from fontTools.ttLib import TTFont
font = TTFont(sys.argv[1])
sbix = font["sbix"]
max_ppem = max(sbix.strikes.keys())
strike = sbix.strikes[max_ppem]
@tobywf
tobywf / config.yml
Created January 29, 2018 02:40
My SVGO configuration file
multipass: true
plugins:
- removeDoctype
- removeXMLProcInst
- removeComments
- removeMetadata
- removeXMLNS
- removeEditorsNSData
- cleanupAttrs
- minifyStyles
@tobywf
tobywf / make-test-repo.sh
Created January 25, 2018 15:18
A script to generate a test repo with two feature branches for the git merge vs git rebase tutorial
#!/bin/bash
set -eux
create_commit() {
git checkout "$1"
echo "$2" > src.txt
git commit -am "$2"
git log --graph --pretty=format:'%s %C(yellow)%d%Creset'
sleep 2 # make the commits distinctive in time
@tobywf
tobywf / clean_old_lambda_versions.py
Last active April 11, 2024 06:52
A quick script to remove old AWS Lambda function versions
from __future__ import absolute_import, print_function, unicode_literals
import boto3
def clean_old_lambda_versions():
client = boto3.client('lambda')
functions = client.list_functions()['Functions']
for function in functions:
versions = client.list_versions_by_function(FunctionName=function['FunctionArn'])['Versions']
for version in versions:
@tobywf
tobywf / generate-newline-csvs.py
Last active August 19, 2017 12:04
Generate CSV files with all permutations of common line endings
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
from itertools import product
import sys
try:
encoding = sys.argv[1]
except IndexError:
encoding = 'utf-8'
@tobywf
tobywf / unicode-csv-excel.py
Created August 19, 2017 10:42
Generate a UTF-8 comma separated value (CSV) file that Excel reads reliably (Python 3, obvs)
import csv
with open('test.csv', 'w', encoding='utf-8-sig', newline='') as fp:
writer = csv.writer(fp)
writer.writerow(['Row', 'Emoji'])
for i, emoji in enumerate(['🎅', '🤔', '😎']):
writer.writerow([str(i), emoji])
@tobywf
tobywf / unicode-csv-excel-legacy-python.py
Created August 19, 2017 10:40
Generate a UTF-8 comma separated value (CSV) file that Excel reads reliably (Legacy Python 2, shame)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from backports import csv
from io import open
with open('test.csv', 'w', encoding='utf-8-sig', newline='') as fp:
writer = csv.writer(fp)
writer.writerow(['Row', 'Emoji'])
for i, emoji in enumerate(['🎅', '🤔', '😎']):
writer.writerow([str(i), emoji])
@tobywf
tobywf / boto3-gzip.py
Last active October 18, 2023 17:32
GZIP compressing files for S3 uploads with boto3
from io import BytesIO
import gzip
import shutil
def upload_gzipped(bucket, key, fp, compressed_fp=None, content_type='text/plain'):
"""Compress and upload the contents from fp to S3.
If compressed_fp is None, the compression is performed in memory.
"""
@tobywf
tobywf / build-dvisvgm.sh
Last active March 30, 2023 02:29
Build dvisvgm and kpathsea on macOS
#!/bin/bash
set -xeuo pipefail
IFS=$'\n\t'
PREFIX="${1:-/usr/local/dvisvgm}"
TEX="$(kpsewhich -var SELFAUTOLOC)"
echo "$PREFIX, $TEX"
brew install automake freetype ghostscript potrace