Skip to content

Instantly share code, notes, and snippets.

@agriffis
agriffis / gist:9798319
Created Mar 27, 2014
Test script for signal handling in heroku-buildpack-pgbouncer
View gist:9798319
#!/bin/bash
psmgr=/tmp/waiting-fifo
rm -f $psmgr
mkfifo $psmgr
pids=( $$ )
(
echo one-start
@agriffis
agriffis / lazy_collection.py
Last active Aug 29, 2015
LazyCollectionMixin
View lazy_collection.py
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 Jul 18, 2015
verify_weebly_install_params
View gist:bae5c8c86e57cd3bf070
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
View gist:1343386
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 Feb 23, 2012
MTI traceback with explicit pk
View models.py
>>> 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)
View pysearch.py
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 Mar 26, 2012
sitdown (or standup)
View gist:2206093
#!/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 Apr 1, 2012
sitdown (or standup) with nvidia proprietary driver
View xorg.conf
#!/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 Dec 14, 2015
URL tree walkers, recursive and non-recursive.
View walk_url_tree.py
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 Feb 28, 2013
Force django admin to SSL
View urls.py
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: