Skip to content

Instantly share code, notes, and snippets.

View wolever's full-sized avatar

David Wolever wolever

View GitHub Profile
@wolever
wolever / deletablemodel.py
Created December 5, 2017 19:04
A Django model base class that hides models instead of deleting them.
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):
@wolever
wolever / return-in-finally.py
Last active September 1, 2017 23:42
Confusing bug I was hit with today: a `return` in a `finally` that ate an exception thrown from a generator
"""
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.
"""
@wolever
wolever / vimrc.vim
Created August 24, 2017 19:35
My rope-vim configuration
" https://github.com/python-rope/ropevim
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
@wolever
wolever / gevent-in-production.rst
Last active July 10, 2018 21:03
Point-form notes about using gevent in production
  • I've been using gevent in production for about 6 years, and I've only encountered a handful of issues:
    • In gevent pre-1.0 there were issues with the DNS resolver
    • I've had a couple of issues with gevent-openssl, which I've fixed by using a custom version: mjs/gevent_openssl#14 and mjs/gevent_openssl#12
    • Celery hangs when gevent is monkeypatched. I haven't figured out why, and this might have been fixed in newer versions of celery (the one I'm using is fairly old).
  • 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 monkeypatches.py and gevent_.py files for my implementation of monkeypatching.
  • The gevent_.py file contains a couple things:
@wolever
wolever / patch_django_create_test_db.py
Last active April 13, 2017 04:19
Prevent Django from asking if you're sure you'd like to delete your test database.
"""
Add this function to your app's __init__.py (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)
@wolever
wolever / git-work-life
Last active July 10, 2019 01:05
Generates bar charts from your git repo to quantify your work-life balance
#!/usr/bin/env python
from __future__ import division
"""
Installation
============
$ curl -O https://gist.githubusercontent.com/wolever/2a15db70f8cb255d753b2cdbb8a718ce/raw/git-work-life
$ chmod +x git-work-life
$ sudo mv git-work-life /usr/local/bin
@wolever
wolever / rebuild-virtualenv
Last active April 11, 2017 18:57
A totally untested script for upgrading a virtualenv
#!/bin/bash
set -eu
if [[ -z "${1-}" ]]; then
echo "USAGE: $0 VIRTUAL_ENV_DIR"
exit 1
fi
venv="$1"
@wolever
wolever / lolmutex.js
Created February 28, 2017 23:24
A LocalStorage-based mutex. Guarantees¹ that only one browser window will be able to execute a callback at a time.
/**
* 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
* (http://www.cs.rochester.edu/research/synchronization/pseudocode/fastlock.html)
* 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.
@wolever
wolever / urllib3_requests_pyopenssl_sni_fix.py
Created February 23, 2017 21:14
Correctly monkeypatch gevent, PyOpenSSL, and urllib3 to all play well together and support SNI.
"""
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
@wolever
wolever / settings.py
Created January 10, 2017 18:36
Disable migrations while testing Django
# A quick but effective hack for disabling migrations while running Django tests.
# Put this in settings.py below the existing MIGRATION_MODULES definition, if that exists:
if "test" in sys.argv[1:]:
class DisableMigrations(object):
def __contains__(self, item):
return True
def __getitem__(self, item):
return "notmigrations"