Skip to content

Instantly share code, notes, and snippets.

import json
import re
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@evansd
evansd / loopback-fs.sh
Last active August 29, 2015 14:05
Create loopback filesystem
# Create a loop-mounted filesystem to allow a very high inode count
# without having to reformat entire partition
# Allocate a 1.5TB file without actually having to write 1.5TB of data
fallocate -l 1500G /path/to/filesystem.img
# Make an ext4 filesystem in this file with:
# (-i) One inode per 1024 bytes
# (-F) Force creation of the filesystem even though the
# file is not a block special device
"""
Extends WhiteNoise so that it always re-checks the mtimes of files before serving
(and updates Content-Length headers as well).
Possibly useful if you are using WhiteNoise in development and want to modify files
without restarting the server. (Note: this won't pick up added/deleted files, you'll
still need a restart for that.)
Use the Django setting `WHITENOISE_RECHECK_MTIMES` to enable.
import hashlib
import json
class HashWrapper(object):
"""
Wraps a hashlib object to make it file-like (i.e. give it a `write` method)
"""
def __init__(self, hashobj=None):
@evansd
evansd / create-startssl-cert-bundle.sh
Last active August 29, 2015 14:08
Read a SSL certificate issued by StartSSL and bundle intermediate certificates into it so it works everywhere
#!/bin/bash
set -eo pipefail
cert_file="$1"
if [ -z "$cert_file" ]; then
echo "Usage: create-startssl-cert-bundle CERTIFICATE_FILE" >&2
echo >&2
echo "Bundles StartSSL's intermediate certs and writes combined certificate to stdout" >&2
exit 1
def limit_with_cte(queryset, limit=None):
if limit is None:
return queryset
sql, params = queryset.query.sql_with_params()
cte_query = 'WITH cte_table_a4eb61 AS ({}) '\
'SELECT * FROM cte_table_a4eb61 LIMIT {:d}'.format(
sql, int(limit))
return queryset.raw(cte_query, params)
#!/bin/bash
# Work around bug in OpenSSH whereby the "ControlMaster" process that gets
# forked off fails to close stderr, causing Python's subprocess to hang forever
# waiting for EOF:
# https://bugzilla.mindrot.org/show_bug.cgi?id=1988
# Created named pipe to pass stderr through
stderr_pipe="$(mktemp -u)"
mkfifo "$stderr_pipe"
# Keep reading from stderr pipe and writing it to actual stderr
@evansd
evansd / aggregate_data.py
Created February 16, 2015 22:22
Experimenting with MOT data
#!/usr/bin/env python
import sys
import json
from collections import defaultdict
if __name__ == '__main__':
data = defaultdict(
lambda: defaultdict(
lambda: defaultdict(
from Queue import Queue
import sys
from threading import Thread
END_OF_STREAM = object()
class QueueWithException(Queue):
# make a quick 30-second gif screencast
alias gifcast='byzanz-record -v -c -d 30 --delay 5 screencast.gif'