Skip to content

Instantly share code, notes, and snippets.

@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 / 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:bae5c8c86e57cd3bf070
Created July 18, 2015 15:33
verify_weebly_install_params
import hashlib
import hmac
def verify_weebly_install_params(params, secret):
"""
Verifies the hmac of params on the Weebly install URL.
"""
# https://dev.weebly.com/oauth2.html
@agriffis
agriffis / gist:1343386
Created November 6, 2011 19:58
isplit
def isplit(patt, s, flags=None):
"""Return a generator that behaves similarly to re.split, with the
following differences:
1. It's a generator, not a list.
2. Zero-width separators work properly
(see http://bugs.python.org/issue3262)
3. The sequence always includes the separators, similar to calling
@agriffis
agriffis / models.py
Created February 23, 2012 19:01
MTI traceback with explicit pk
>>> from bar.models import Bar
>>> b=Bar()
>>> b.pk='abcdef'
>>> b.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/aron/.virtualenvs/pp/lib/python2.7/site-packages/django/db/models/base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/aron/.virtualenvs/pp/lib/python2.7/site-packages/django/db/models/base.py", line 553, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
@agriffis
agriffis / pysearch.py
Created March 1, 2012 17:43
pysearch
import operator, os, re, urllib
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
__all__ = ['PySearch']
anchors, lower_anchors = None, None
def get_anchors():
global anchors, lower_anchors
if anchors is None:
@agriffis
agriffis / gist:2206093
Created March 26, 2012 15:48
sitdown (or standup)
#!/bin/bash
hdmi=HDMI-1
dvi=DVI-D-1
case /$0 in
*/sitdown) xrandr --output $hdmi --auto ; xrandr --output $dvi --off ;;
*/standup) xrandr --output $dvi --auto ; xrandr --output $hdmi --off ;;
esac
@agriffis
agriffis / xorg.conf
Created April 1, 2012 20:25
sitdown (or standup) with nvidia proprietary driver
#!/bin/bash
if glxinfo | grep -q 'NVIDIA Corporation'; then
case /$0 in
*/sitdown) xrandr -s 1 ;;
*/standup) xrandr -s 0 ;;
esac
else
hdmi=HDMI-1
dvi=DVI-D-1
@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 / 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: