Skip to content

Instantly share code, notes, and snippets.

@agriffis
agriffis / lazy_collection.py
Last active August 29, 2015 14:20
LazyCollectionMixin
class LazyCollectionMixin(object):
__len__ = new_method_proxy(len)
__contains__ = new_method_proxy(operator.contains)
def __iter__(self):
# Don't define this as
# __iter__ = new_method_proxy(iter)
# because that creates a method which doesn't call yield,
# so it executes setup immediately.
if self._wrapped is empty:
@agriffis
agriffis / gist:9798319
Created March 27, 2014 01:51
Test script for signal handling in heroku-buildpack-pgbouncer
#!/bin/bash
psmgr=/tmp/waiting-fifo
rm -f $psmgr
mkfifo $psmgr
pids=( $$ )
(
echo one-start
@agriffis
agriffis / gist:8240913
Last active January 2, 2016 02:59
Chrome vs. Firefox
from django.core.cache import get_cache
class KeyedCache(object):
"""
Proxied cache object to allow objects to implement their own keying,
for example a Django model can implement per-row caching with:
def cache_key(self, key):
return '{}.{}.{}'.format(self.__class__.__name__, self.pk, key)
@agriffis
agriffis / wait_for_it.bash
Last active December 16, 2015 14:59
wait_for_it.bash 30m deploy
#!/bin/bash
sleep_for=$1
shift
(
"$@"
status=$?
if [[ $status == 0 ]]; then
@agriffis
agriffis / stdout.py
Created April 22, 2013 21:07
Patch Python's sys.stdout and sys.stderr to honor locale variables regardless of tty, and fall back to UTF-8 instead of ASCII in the absence of more information.
"""
Patch Python's sys.stdout and sys.stderr to encode to UTF-8
The short story is that sys.stdout.encoding is set based on the environment
variable LC_CTYPE (or LANG/LC_ALL) but only when stdout.isatty().
Otherwise those variables are ignored and stdout has no encoding, and it
CANNOT be set.
The long story is here (especially in the comments):
http://drj11.wordpress.com/2007/05/14/python-how-is-sysstdoutencoding-chosen/
#!/bin/bash
#
# gpgsig, a little script to sign your gpg/pgp keys
#
# Copyright 2006,2013 Aron Griffis <agriffis@n01se net>
# Released under the GNU GPL v2
#
# Based loosely on ideas Damien Chrisment's gpgsig, but written from scratch to
# be simpler and just do what I need. Simply:
#
@agriffis
agriffis / urls.py
Created February 28, 2013 13:41
Force django admin to SSL
def walk_url_tree(urls):
"""
Generator to walk a tree consisting of RegexURLPattern (with callback)
and RegexURLResolver (with nested url_patterns).
"""
for u in urls:
if hasattr(u, 'url_patterns'):
for u2 in walk_url_tree(u.url_patterns):
yield u2
else:
@agriffis
agriffis / walk_url_tree.py
Last active December 14, 2015 07:59
URL tree walkers, recursive and non-recursive.
def walk_url_tree(urls):
"""
Generator to walk a tree consisting of RegexURLPattern (with callback)
and RegexURLResolver (with nested url_patterns).
Recursive depth-first.
"""
for u in urls:
if hasattr(u, 'url_patterns'):
for u2 in walk_url_tree(u.url_patterns):
@agriffis
agriffis / reghex.py
Last active March 27, 2018 05:41
reghex checker
#!/usr/bin/env python3
#
# reghex.py -- checker for reghex puzzle
#
# Input on stdin in the following format:
#
# XXXXXXX
# XXXXXXXX
# XXXXXXXXX
# XXXXXXXXXX