Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am tobywf on github.
  • I am tobywf (https://keybase.io/tobywf) on keybase.
  • I have a public key whose fingerprint is 90A8 0C6B 788D 1574 F349 AA29 B0BF 051F 9B20 2065

To claim this, I am signing this object:

@tobywf
tobywf / meldmerge.rb
Last active October 14, 2016 01:32
Homebrew cask for yousseb's Meld.dmg
cask 'meldmerge' do
version 'osx-9'
sha256 '324e096e0253e8ad4237f64a90cdda200fe427db8cf7ebc78143fc98e2d33ebc'
url "https://github.com/yousseb/meld/releases/download/#{version}/meldmerge.dmg"
name 'Meld'
homepage 'https://yousseb.github.io/meld/'
app 'Meld.app'
binary "#{appdir}/Meld.app/Contents/MacOS/Meld", target: 'meld'
@tobywf
tobywf / rounding.py
Created April 9, 2017 16:24
Decimal rounding to the nearest multiple (plus unit tests)
from decimal import Decimal, ROUND_CEILING, ROUND_FLOOR
ONE = Decimal(1)
def round_up(number, quantum):
"""Round a decimal number up to the nearest multiple of quantum"""
return (number / quantum).quantize(ONE, rounding=ROUND_CEILING) * quantum
@tobywf
tobywf / build-dvisvgm.sh
Last active April 26, 2024 10:14
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
@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 / 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 / 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 / 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 / clean_old_lambda_versions.py
Last active June 10, 2024 14:15
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 / 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