Writing Tests that Fail Well

Writing better tests is a lifelong pursuit, apparently.

One thing I've been noticing lately, especially while doing code reviews, is that we are usually thinking about how do I know the code worked.

Great! But we are usually not thinking ahead about what am I going to do when the …

more ...

Finding Which Superclass Provides a Method

Here's a quick hack to help when you're lost in a too-large Python inheritance tree. What class provides the most specific implementation of a method or attribute?

def find(cls, name):
    for sup in cls.mro():
        if name in sup.__dict__:
            return sup.__name__
    return None

def find_all_vars(cls):
    for …
more ...

Thread Locals in Python: Mostly easy

Thread locals are an interesting idea: a way to give each thread its own storage, useful for global state that you don't want to share between threads.

The obvious caveat is that threadlocals are still effectively global (for the current thread), and like all global state, should be treated with …

more ...

urllib2 gotcha

Does this return all the data?

urllib2.urlopen(url).read()

The answer appears to be "not always".

http://stackoverflow.com/questions/7174927/when-does-socket-recvrecv-size-return https://github.com/CVL-dev/feedparser/issues/1

I can't seem to get a definitive answer here, nor a reproducible demonstration of it failing.

Does anybody know for …

more ...


My Emacs setup

Some notes on my emacs setup. Maybe this should go in README for my dotemacs.

Python

Which mode?

There is python-mode.el, and python.el. I have settled on python-mode from the elpa repositories.

Rope?

I tried ropemacs, and really like it when it works. For simple refactorings it can …

more ...


settings.py considered evil

Django's settings.py is evil.

Why? Because in terms of security, it is a bomb waiting to go off. Typically your database settings go in there, and potentially other sensitive stuff (salts, service API keys, what have you).

By encouraging you to put a settings.py file in your project's …

more ...

Python string split weirdness

Consider:

>>> ''.split()
[]

That seems okay... split an empty string on whitespace and you get an empty list, sure, why not.

Except that in every other case, if you split a string on a separator that it doesn't contain, you get a list containing the original string - even if the original …

more ...

Context Manager + Decorator

(Updated 2018 with a more modern Python 3 tip)

I always forget how to write a class that can be used as both a decorator and a context manager. It's a handy trick, but I don't do it often enough to remember, so, here it is.

In Python 2, you …

more ...

map vs. iter

What's the difference between these?

>>> zipped = zip(iter1, iter2)
>>> mapped = map(None, iter1, iter2)

The answer turns out to be depend on whether iter1 and iter2 are the same length. Pretty subtle: zipped is truncated to the length of the shortest of (iter1, iter2). mapped is the same length as …

more ...

pdbpp rocks!

A coworker (hi Will) just introduced me to pdbpp.

As a longtime pdb user, I must say this is SOOO GOOD. Two features alone are worth it: syntax highlighting, and the ll command which prints the entire function around the current line.

One wrinkle is that at work we still …

more ...

Django is not an application framework

Django as it is in the wild is not a web software framework. It is a web site framework.

(disclaimer re. complaining: http://commandcenter.blogspot.com/2011/12/esmereldas-imagination.html )

What does this mean and why?

For some things, I love Django. It hits a particular sweet spot really well …

more ...

Mock configuration tip

A little tip for working with the mock library. Let's say you need to set up some child attributes, like for example you're mocking up a django.contrib.auth.User, and you want it to be a logged in user:

>>> user = mock.Mock()
>>> user.username = 'Bob'
>>> user.is_authenticated.return_value = True …
more ...

Python packaging: Where Pip and Virtualenv fall short

I love the combination of virtualenv and pip requirements files. This is a great 95% solution to the problem of deploying and distributing known-good sets of python packages, safely (semi-)isolated from other python environments on the same system.

The --no-site-packages option is great because it makes your installation isolated …

more ...

Python dictionary trick: d3 = d1 + d2

Want to merge two dictionaries into a new third dictionary without modifying either of the originals? Easy:

>>> dict3 = dict(dict1, **dict2)

Keys in dict2 would override matching keys in dict1. And, as usual, mutable values would be preserved by reference...

Personally I've always wished that dicts would define __add__ to …

more ...