Skip to content

Instantly share code, notes, and snippets.

View pirate's full-sized avatar
🗃️
Archiving all the things!

Nick Sweeting pirate

🗃️
Archiving all the things!
View GitHub Profile
@pirate
pirate / monkey_patch_python_traceback.py
Last active January 24, 2024 23:23
Monkey patch the Python traceback system to modify function names, line numbers, file locations, etc.
# This code is a verbatim excerpt from the Jinja2 library that I'm preserving in a Gist for posterity
# https://github.com/pallets/jinja/blob/5b498453b5898257b2287f14ef6c363799f1405a/jinja2/debug.py
# -*- coding: utf-8 -*-
"""
jinja2.debug
~~~~~~~~~~~~
Implements the debug interface for Jinja. This module does some pretty
ugly stuff with the Python traceback system in order to achieve tracebacks
@pirate
pirate / dns.sh
Last active December 28, 2023 15:00
Dynamic DNS updater script for DigitalOcean and CloudFlare (using bash, curl, and jq)
#!/usr/bin/env bash
# set -o xtrace
set -o errexit
set -o errtrace
set -o nounset
set -o pipefail
SCRIPTNAME="$0"
HELP_TEXT="
@pirate
pirate / pluginization_example.py
Last active December 18, 2023 10:18
Example of how to pluginize a complex app using a hooks system
"""
Example of a pluginized architecture breaking up a large app
with complex behavior (ArchiveBox), into a series of steps
that plugins can hook into.
(read from the bottom to top to get a quick overview)
"""
import re
import json
@pirate
pirate / parseURLParameters.js
Last active December 15, 2023 07:17
Parse URL query parameters in ES6
function getUrlParams(search) {
const hashes = search.slice(search.indexOf('?') + 1).split('&')
const params = {}
hashes.map(hash => {
const [key, val] = hash.split('=')
params[key] = decodeURIComponent(val)
})
return params
}
@pirate
pirate / alfred-clipboard.sh
Last active December 5, 2023 18:12
Script to manage searching, backing up, and collecting infinite clipboard history from the Alfred Clipboard History on macOS.
#!/usr/bin/env bash
# This is a script that provides infinite history to get around Alfred's 3-month limit.
# It works by regularly backing up and appending the items in the alfred db to a
# sqlite database in the user's home folder. It also provides search functionality.
# https://www.alfredforum.com/topic/10969-keep-clipboard-history-forever/?tab=comments#comment-68859
# https://www.reddit.com/r/Alfred/comments/cde29x/script_to_manage_searching_backing_up_and/
# Example Usage:
# alfred-clipboard.sh backup
@pirate
pirate / safe_number.py
Last active July 18, 2023 22:08
A SafeNumber type for Python that implements the fractions.Fraction interface with guards to prevent implicit operand type casting leading to a loss of precision.
# This implements a SafeNumber class which wraps Decimal and Fraction to warn
# when infix math or comparison operators may cause dangerous implicit type conversion.
#
# Implicit type conversion when using operators is sneaky with Decimal/Fraction:
# >>> Fraction(10) == 10.0000000000000001
# True
#
# But with SafeNumber, this throws an error to protect against this scenario:
# >>> SafeNumber(10) == 10.0000000000000001
# Traceback (most recent call last):
@pirate
pirate / asymptotic_progress_bar.py
Last active May 31, 2023 08:43
Animated CLI progress bar that fills up over N seconds, gracefully handling delays by filling slower and slower if the task exceeds the expected time. Good for when you don't know how long a task is going to take, but you still need it to A. feel fast in the avg case, and B. not freeze in the worst case (unknown total time).
#!/usr/bin/env python3
# asymptotic_progress_bar.py
# MIT License © 2021
#
# A pretty non-blocking python progress bar timer that fills up asymptotically until you stop it.
# Good for when you don't know how long a task is going to take (up to some max timeout),
# but you want it to feel fast and accurate / not stuck the whole time.
# ████████████████████ 0.9% (1/60sec)
# useful for animating e.g. file copy progress, download progress, package install progress, etc.
#
@pirate
pirate / compress_videos.sh
Created January 30, 2021 01:40
Re-encode a bunch of video files into x265 MP4 without losing EXIF metadata
#!/usr/bin/env bash
# Requires: ffmpeg, exiftool (install via apt/brew first)
# Usage:
# $ cd ~/Videos
# $ ./compress_videos.sh
# [+] Converting all .mp4 files in ~/Videos to .x265.mp4 files...
# - √ GP013838.mp4 (2.5GB) -> GP013838.x265.mp4 (142MB)
# - √ ...
# [√] Done converting all .mp4 files in $PWD.
@pirate
pirate / docker-compose.yml
Last active January 24, 2023 06:41
Example database container setups for PostgreSQL, MariaDB, Redis, Memcached, MongoDB, Neo4j, Hasura GraphQL, CockroachDB, and TiDB in Docker Compose
# Example database container setups for PostgreSQL, MariaDB, Redis, Memcached, MongoDB, Neo4j, Hasura GraphQL, CockroachDB, and TiDB in Docker Compose
# https://gist.github.com/pirate/1fafaa18a47254f388aa5c0f79f7d263
# Goes well with these docker-compose networking/ingress container examples:
# https://gist.github.com/pirate/1996d3ed6c5872b1b7afded250772f7c
version: '2.4'
services:
postgres:
@pirate
pirate / django_turbo_response.py
Last active November 21, 2022 21:12
An extended HTTPResponse class for Django 2.2 adding support for streaming partial template responses incrementally, preload headers, HTTP2 server push, CSP headers, running post-request callbacks, and more (fully typed).
"""
This is an extended HTTP response class for Django >=2.0 that adds lots of fancy features.
It's most useful when needing to accellerate slow view functions that return templates,
but it can be used anywhere where you need to return an HTTPResponse() or render(...).
It works by subclassing the builtin Django HttpResponse and StreamingHttpResponse,
and adding improvements in many areas, including functionality, speed, and security.
The most up-to-date version of this code can be found here: