Instantly share code, notes, and snippets.

View git-bclean
# STOP! Before going any further, think: are you going to regret the decision
# to write this script?
# Deciding to write this in bash was not one of my better decisions.
# --
IFS="`printf "\n\t"`"
set -eu
from django.utils import timezone
from django.db import models as m
from django.db.models.base import ModelBase
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.fields.related import SingleRelatedObjectDescriptor
from model_utils.managers import PassThroughManagerMixin
class DeletableQuerySetMixin(object):
A demonstration of a subtle bug when a context manager uses
`return` in a `finally` block around a `yield`.
See comments on last line for an explanation.
Take-away: be very, very careful about using `return` in a
`finally` block.
View vimrc.vim
Plug 'python-rope/ropevim'
let g:ropevim_guess_project = 1
let g:ropevim_goto_def_newwin = 'vnew'
let ropevim_vim_completion=1
let ropevim_extended_complete=1
autocmd FileType python setlocal omnifunc=RopeCompleteFunc
map <c-c>i :RopeAutoImport<CR>
map <c-c>fo :RopeFindOccurrences<CR>
" <C-c>rr RopeRename
View gevent-in-production.rst
  • I've been using gevent in production for about 6 years, and I've only encountered a handful of issues:
  • All of my experience using gevent is in fully monkeypatched mode. It's certainky possible to use outside of monkeypatch mode, but I don't know anything about that.
  • See the and files for my implementation of monkeypatching.
  • The file contains a couple things:
Add this function to your app's (or similar) to prevent Django
from ever asking if you're sure you'd like to delete your test database.
def _patch_django_create_test_db():
from django.db.backends.base.creation import BaseDatabaseCreation
old_create_test_db = BaseDatabaseCreation._create_test_db
def _create_test_db(self, verbosity, autoclobber, *args, **kwargs):
return old_create_test_db(self, verbosity, True, *args, **kwargs)
View git-work-life
#!/usr/bin/env python
from __future__ import division
$ curl -O
$ chmod +x git-work-life
$ sudo mv git-work-life /usr/local/bin
View rebuild-virtualenv
set -eu
if [[ -z "${1-}" ]]; then
exit 1
View lolmutex.js
* A LocalStorage-based mutex. Guarantees¹ that only one browser window will be
* able to execute the code inside the callback at a time.
* Based on the Alur and Taubenfeld fast lock
* (
* with an added timeout to ensure there will be eventual progress in the event
* that a window is closed in the middle of the callback.
* 1. The guarantee assumes that the algorithm is correctly implemented.
The correct set of steps to monkeypatch:
- gevent into threading, socket, etc
- PyOpenSSL with gevent (via gevent_openssl)
- PyOpenSSL to be compatible with
- urllib3 and requests.packages.urllib3 to use PyOpenSSL, which
lets them connect to servers using SNI
import logging