Skip to content

Instantly share code, notes, and snippets.

View wolever's full-sized avatar

David Wolever wolever

View GitHub Profile
@wolever
wolever / scopes.py
Last active August 29, 2015 13:56
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 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 March 3, 2014 20:35
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 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
"""

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 March 14, 2014 20:30
Small shell function to create Python packages
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 April 6, 2014 21:47
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):
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 August 29, 2015 14:04
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
wolever / vs.sh
Created September 12, 2014 22:48
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 1.8.1.1
@wolever
wolever / monkypatch_logging.py
Created September 14, 2014 21:21
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):
try:
return oldGetMessage(self)
except Exception as e:

Full-stack Software Developer with a flair for the fasionable

Are you a passionate web developer who wears skinny jeans, checks Foursquare before heading out, and builds sleek, sylish apps? We are looking for you!

Who we are

We are Second Funnel - a small, growing startup making the web more visually interesting and relevant. We believe advertising currently sucks, but can be made awesome. Or at least less terrible. How? Through visuals that peek curiousity and lead to interesting, engaging content (think Pinterest meets marketing).