Skip to content

Instantly share code, notes, and snippets.

View wolever's full-sized avatar

David Wolever wolever

View GitHub Profile
@wolever
wolever / watchdog.py
Created December 2, 2016 23:57
A simple watchdog for long-running Python processes
"""
A simple watchdog for long running processes which may stall for some reason or
another.
If the main thread hasn't logged progress (by updating
``self.last_progress_time``) in WATCHDOG_HARD_KILL_TIMEOUT, the watchdog
thread will log an error containing the stack trace of all currently running
threads then use ``kill -9`` to kill the main process.
Assumes that a process monitor like supervisor or systemd will then restart
@wolever
wolever / chkhost
Created October 23, 2012 00:26
OpenVPN watchdog which will restart the OpenVPN daemon if it decides to go away
#!/bin/bash
IFS="`printf "\n\t"`"
set -eu
cd "`dirname "$0"`"
outfile="/dev/null"
if [[ "${1-}" == "-v" ]]; then
outfile="/dev/stdout"
@wolever
wolever / fix-mouse.lua
Created November 7, 2019 18:17
Hammerspoon config to use mouse button 3 for scrolling, remap button 4 to middle button
-- HANDLE SCROLLING WITH MOUSE BUTTON PRESSED
local scrollMouseButton = 3
local deferred = false
overrideOtherMouseDown = hs.eventtap.new({ hs.eventtap.event.types.otherMouseDown }, function(e)
-- print(hs.eventtap.event.properties['mouseEventButtonNumber'])
local mouseButton = e:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber'])
if mouseButton == scrollMouseButton then
deferred = true
return true
@wolever
wolever / patch_objects_get.py
Created November 16, 2010 21:08
Patch `objects.get` so its DoesNotExist errors are more helpful
from functools import wraps
def patch_objects_get(cls):
""" Patch 'cls.objects.get' so its DoesNotExist errors will be more
helpful.
Usage:
>>> @patch_objects_get
... class MyModel(m.Model):
@wolever
wolever / lolmutex.js
Created February 28, 2017 23:24
A LocalStorage-based mutex. Guarantees¹ that only one browser window will be able to execute a callback at a time.
/**
* A LocalStorage-based mutex. Guarantees¹ that only one browser window will be
* able to execute the code inside the callback at a time.
*
* Based on the Alur and Taubenfeld fast lock
* (http://www.cs.rochester.edu/research/synchronization/pseudocode/fastlock.html)
* with an added timeout to ensure there will be eventual progress in the event
* that a window is closed in the middle of the callback.
*
* 1. The guarantee assumes that the algorithm is correctly implemented.
@wolever
wolever / ssh_control_socket.sh
Created October 3, 2011 06:50
Bash script to start and background an SSH ControlMaster to speed up SSH in the script
# Call `setup_ssh_socket` to setup the control socket (function will return once
# the socket is ready to go), and `ssh_target` will connect using the control socket.
# Assumes TARGET_HOST variable is set.
# The connection is automatically closed when the script exists.
# TARGET_HOST="wolever.net"
# setup_ssh_control_socket
# ssh_target "hostname"
debug() {
echo "DEBUG: $*"
@wolever
wolever / example.py
Created February 18, 2012 06:31
A persistent Queue implementation in Python which focuses on durability over throughput
from pqueue import PersistentQueue
q1 = PersistentQueue("/tmp/queue_storage_dir")
q1.put("1")
q1.put("2")
q1.put("3")
q1.close()
q2 = PersistentQueue("/tmp/queue_storage_dir")
while not q2.empty():
@wolever
wolever / fractional-indexing-with-floats.rst
Last active October 21, 2023 17:53
Analysis of floating point fractional indexing

Fractional indexing is a technique for assigning a rank to ordered items, where an item's position can be changed - or a new item introduced - with only one rank value update.

For example, given the items:

{ name: "a", rank: 1 }
{ name: "c", rank: 2 }

With a naive rank ordering scheme, introducing "b" in between "a" and "c" would

@wolever
wolever / reverseadmin.py
Last active June 21, 2023 06:48 — forked from mzbyszewska/reverseadmin.py
Forked from https://gist.github.com/mzbyszewska/8b6afc312b024832aa85 , updated to work with Django 1.8
'''
adminreverse from here http://djangosnippets.org/snippets/2032/
changed for working with ForeignKeys
'''
'''
reverseadmin
============
Module that makes django admin handle OneToOneFields in a better way.
A common use case for one-to-one relationships is to "embed" a model
inside another one. For example, a Person may have multiple foreign
@wolever
wolever / histogram.sql
Last active April 19, 2023 20:28
Functions to create and draw histograms with PostgreSQL.
-- Functions to create and draw histograms with PostgreSQL.
--
-- psql# WITH email_lengths AS (
-- -# SELECT length(email) AS length
-- -# FROM auth_user
-- -# LIMIT 100
-- -# )
-- -# SELECT * FROM show_histogram((SELECT histogram(length, 0, 32, 6) FROM email_lengths))
-- bucket | range | count | bar | cumbar | cumsum | cumpct
-- --------+-------------------------------------+-------+--------------------------------+--------------------------------+--------+------------------------