Skip to content

Instantly share code, notes, and snippets.

Avatar

David Wolever wolever

  • Sheertex
  • Montreal, QC
  • Twitter @wolever
View GitHub Profile
@wolever
wolever / scopes.py
Last active Aug 29, 2015
Fun with Python scopes
View scopes.py
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
View finallyfun.py
"""
Python 2:
$ python-2.7 finallyfun.py
e: Exception()
Python 3:
$ python3.3 finallyfun.py
Traceback (most recent call last):
File "finallyfun.py", line 9, in <module>
foo()
@wolever
wolever / else_block_example.py
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`.
View else_block_example.py
"""
Python's ``else:`` block is *only* executed when the ``try:`` block does not reutrn:
$ python else_block_example.py
Running function, returning in 'try:' block: False
in 'else' block
in 'finally' block
Running function, returning in 'try:' block: True
in 'finally' block
"""
View keybase.md

Keybase proof

I hereby claim:

  • I am wolever on github.
  • I am wolever (https://keybase.io/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
wolever / mkpkg.sh
Created Mar 14, 2014
Small shell function to create Python packages
View mkpkg.sh
mkpkg() {
if [[ -z "$1" ]]; then
echo "mkpkg: $0 PKG_DIR"
return 1
fi
if [[ ! -d "$1" ]]; then
mkdir "$1" || return $?
fi
touch "$1/__init__.py"
}
@wolever
wolever / django_template_custom_render.py
Created Apr 6, 2014
Shows how to render a Django template using a custom template tag library.
View django_template_custom_render.py
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):
if s.TEMPLATE_DEBUG:
from django.template.debug import DebugLexer, DebugParser
lexer_class, parser_class = DebugLexer, DebugParser
else:
@wolever
wolever / notice_board.py
Last active Aug 29, 2015
What I'm currently calling the "Notice Board pattern". Is this already a thing?
View notice_board.py
"""
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
wolever / vs.sh
Created Sep 12, 2014
vs: shell functions to transparently call hg or git, whichever is appropriate
View vs.sh
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 1.8.1.1
@wolever
wolever / monkeypatch_queryset.py
Last active Aug 29, 2015
Monkeypatch Django 1.5's QuerySet adding a .first() method compatible with 1.6
View monkeypatch_queryset.py
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.")
return
def first(self):
@wolever
wolever / monkypatch_logging.py
Created Sep 14, 2014
Monkey patch for logging.LogRecord.getMessage so it will never, ever raise an exception
View monkypatch_logging.py
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):
try:
return oldGetMessage(self)
except Exception as e: