Skip to content

Instantly share code, notes, and snippets.

@kgaughan
kgaughan / dyck.py
Created February 4, 2019 18:42
Testing for Dyck words. Inspired by: http://raganwald.com/2018/11/14/dyck-joke.html
#!/usr/bin/env python3
def is_dyck_word(word):
i = 0
for ch in word:
if ch == '[':
i += 1
elif ch == ']':
i -= 1
@kgaughan
kgaughan / jslib.js
Created January 22, 2019 23:28
Some old JS code from an older version of my site. The timestamp is April 2006, but it probably dates from earlier. Looks to be a way to make smart blockquotes.
function addEvent(obj, type, fn) {
// Mozilla/W3C listeners?
if (obj.addEventListener) {
obj.addEventListener(type, fn, false);
return true;
}
// IE-style listeners?
if (obj.attachEvent) {
return obj.attachEvent('on' + type, fn);
@kgaughan
kgaughan / locking.py
Created January 21, 2019 16:04
Treating a script as its own mutex lock.
import contextlib
import fcntl
import time
class MutexException(Exception):
"""
Failed to lock a file.
"""
@kgaughan
kgaughan / gist:b0ed6852f681be576f61aa0d0bf01314
Last active December 12, 2017 14:33
Get A records for a given domain name
get_nss () {
dig +aaonly @$(dig +short $(echo $1 | cut -f2- -d.) NS | head -n1) $1 NS | \
grep -v '^;' | \
awk '$4 == "NS" {print substr($5, 1, length($5) - 1)}' | sort -u
}
fqdn=google.com
dig +short @$(get_nss $fqdn | head -n1) $fqdn A
@kgaughan
kgaughan / clicfgtest.py
Created September 8, 2017 14:05
Providing default arguments to argparse.ArgumentParser with a config file.
#!/usr/bin/env python
from __future__ import print_function
import argparse
import json
import sys
def process_args():
parser = argparse.ArgumentParser()
@kgaughan
kgaughan / bounce.py
Created August 13, 2017 13:49
Playing with some basic pygame: a simple full-screen bouncing box.
#/usr/bin/env python
import pygame
from pygame.locals import *
BOX_WIDTH = 100
BOX_HEIGHT = 100
BLACK = (0, 0, 0)
% python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):
... pass
...
>>> a = A()
>>> a.__class__.__dict__ is a.__class__.__dict__
False
@kgaughan
kgaughan / gist:f009b6a91bfb480f7d1aa4035913668b
Created July 4, 2017 10:52
Nope-ing the fuck out of here!
% sudo pkg upgrade
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
Checking for upgrades (178 candidates): 100%
Processing candidates (178 candidates): 100%
Checking integrity... done (2 conflicting)
- postgresql95-client-9.5.7_1 conflicts with postgresql93-client-9.3.17_1 on /usr/local/bin/clusterdb
- postgresql95-client-9.5.7_1 conflicts with postgresql93-client-9.3.17_1 on /usr/local/bin/clusterdb
Checking integrity... done (0 conflicting)
@kgaughan
kgaughan / gist:82a191702081bcb35d2dc41080b9a861
Created May 12, 2017 14:16
I know, this shouldn't bother me, but I resent seeing a chain of this kind of crud in my process list. Use 'exec', FFS!
root 1677 0.0 0.0 108188 1412 ? S 02:33 0:00 /bin/bash -c ulimit -S -c 0 >/dev/null 2>&1 ; /usr/sbin/rabbitmq-server
root 1679 0.0 0.0 108188 1480 ? S 02:33 0:00 /bin/sh /usr/sbin/rabbitmq-server
root 1699 0.0 0.0 145024 1608 ? S 02:33 0:00 su rabbitmq -s /bin/sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 1704 0.0 1.4 581156 30128 ? Ssl 02:33 0:11 /usr/lib64/erlang/erts-5.8.5/bin/beam -W w -K true -A30 -P 1048576 --
@kgaughan
kgaughan / gist:a6a31918ccf1baaa6d3f14abdf803bef
Last active December 15, 2016 18:50
Search algorithm outline

I've an idea for a merge sort variant that's been rattling around my head for a while.

The idea is that sorting is broken up into two phase: run identification and merging. This has some similarities to Timsort, but it's not quite the same thing.

In the run identification phase, runs are identified and recorded. There is some deperturbation to try an identify the longest runs possible near the potential end of runs where if the next element, x[i], is less than x[i-1] and x[i-2], it is swapped with x[i-1].

If no run longer than N can be identified since the last identified run by M elements, those M elements are sorted using insertion sort to create a run.

When we have a list of runs, we start merging adjacent pairs of runs, starting with the smallest adjacent pairs.