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 / type-safe-object-functions.ts
Created December 2, 2024 00:08
Type-safe definitions of `Object.keys`, `Object.entries`, and `Object.values`: `objectKeys`, `objectEntries`, and `objectValues`
/**
* ObjectType matches any "regular" object.
*
* For example:
* > {} satisfies ObjectType
* > { a: 1, b: 2 } satisfies ObjectType
* > [] satisfies ObjectType
* > null satisfies ObjectType
* TypeError: 'null' does not satisfy 'ObjectType'.
* > undefined satisfies ObjectType
/**
* Catches any exception which might be raised by a promise and returns a
* tuple of [result, error], where either the result or the error will be
* undefined:
*
* let [res, error] = await maybe(someApi.get(...))
* if (err) {
* return `Oh no there was an error: ${err}`
* }
* console.log('The result:', res)
@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 / mkca.sh
Created April 19, 2012 15:27
Tools for creating private certificate authorities
#!/bin/bash
set -e
nodes="-nodes"
if [[ "$1" == "-des" ]]; then
nodes=""
shift
fi
if [[ ! "$1" || ! "$2" || "$1" =~ "^-" ]]; then
@wolever
wolever / settings.py
Created January 10, 2017 18:36
Disable migrations while testing Django
# A quick but effective hack for disabling migrations while running Django tests.
# Put this in settings.py below the existing MIGRATION_MODULES definition, if that exists:
if "test" in sys.argv[1:]:
class DisableMigrations(object):
def __contains__(self, item):
return True
def __getitem__(self, item):
return "notmigrations"
@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 / _README.rst
Last active June 26, 2024 07:06
Django testing things. To be put in a `testing` subdirectory of the project root.
  1. Put these files in yourapp/testing/
  2. Install:
  3. Run tests with python manage.py test, or test specific files or directories with python manage.py test myapp/tests/test_models.py
  1. Test cases which use the database must subclass MyAppTestCase, but other test can use test functions, generators, and everything else supported by nose: https://nose.readthedocs.org/en/latest/writing_tests.html
@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 / 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):