Skip to content

Instantly share code, notes, and snippets.


David Wolever wolever

  • Sheertex
  • Montreal, QC
  • Twitter @wolever
View GitHub Profile
wolever /
Last active Aug 29, 2015
Fun with Python scopes
global_var = 42
def outer_func():
outer_var = 21
un_used_outer_var = 16
def inner_func():
inner_var = 7
def inner_inner_func():
inner_inner_var = 3
# note: the outer_var must be explicitly referenced, otherwise it won't
Python 2:
$ python-2.7
e: Exception()
Python 3:
$ python3.3
Traceback (most recent call last):
File "", line 9, in <module>
wolever /
Created Mar 3, 2014
Apparently Python doesn't execute the `else` block of a `try/except/else` block if there is a return in the `try`.
Python's ``else:`` block is *only* executed when the ``try:`` block does not reutrn:
$ python
Running function, returning in 'try:' block: False
in 'else' block
in 'finally' block
Running function, returning in 'try:' block: True
in 'finally' block

Keybase proof

I hereby claim:

  • I am wolever on github.
  • I am wolever ( on keybase.
  • I have a public key whose fingerprint is B5A9 A97F CE49 06AF 8E13 DE44 43D2 86AB B230 230D

To claim this, I am signing this object:

wolever /
Created Mar 14, 2014
Small shell function to create Python packages
mkpkg() {
if [[ -z "$1" ]]; then
echo "mkpkg: $0 PKG_DIR"
return 1
if [[ ! -d "$1" ]]; then
mkdir "$1" || return $?
touch "$1/"
wolever /
Created Apr 6, 2014
Shows how to render a Django template using a custom template tag library.
from django.conf import settings as s
from django.template import Parser, Lexer, Context, Library, StringOrigin
lib = Library()
def parse_template(library, origin, template_string):
from django.template.debug import DebugLexer, DebugParser
lexer_class, parser_class = DebugLexer, DebugParser
wolever /
Last active Aug 29, 2015
What I'm currently calling the "Notice Board pattern". Is this already a thing?
The Notice Board pattern: returns the most recent value associated with a key,
or blocks waiting for a new value if the current value has already been seen.
History is not stored; only the most recent value is available.
The physical analogy is a notice board like you might see at a train station:
when you arrive at the station you can glance at the board to check the current
departure time for your train, or you can stand around watching the board
waiting for the inevitable message that your train has been delayed.
wolever /
Created Sep 12, 2014
vs: shell functions to transparently call hg or git, whichever is appropriate
vs() {
# Guesses which version control system is correct for the current
# directory, then executes it with "${@}":
# $ cd hg_repo/
# $ vs version
# Mercurial Distributed SCM
# ...
# $ cd ../git_repo
# $ vs version
# git version
wolever /
Last active Aug 29, 2015
Monkeypatch Django 1.5's QuerySet adding a .first() method compatible with 1.6
def monkeypatch_queryset_first():
""" Monkeypatch QuerySet adding a `.first()` method which is compatible
with Django 1.6's `.first()`. """
from django.db.models.query import QuerySet
if hasattr(QuerySet, "first"):
import warnings
warnings.warn("QuerySet.first is already defined! "
"Monkey patch should be removed.")
def first(self):
wolever /
Created Sep 14, 2014
Monkey patch for logging.LogRecord.getMessage so it will never, ever raise an exception
def monkeypatch_logging_getMessage():
""" Monkey patch logging.LogRecord.getMessage so it will never, ever raise an exception. """
from unstdlib import to_str
from logging import LogRecord
oldGetMessage = LogRecord.getMessage
def getMessage(self):
return oldGetMessage(self)
except Exception as e: