Django TestCase Comedy

Here's a little snippet that'll entertain the vast throngs of people who are interested in both Rock Paper Scissors strategy and Django unit testing.

def test_desperate_futility_of_avalanche(self):
        for futile_attempt in range(100):
            Round.objects.create(game=self.normal_game,
                                 initiator_choice=self.rock,
                                 opponent_choice=self.rock)
        self.failUnlessEqual(self.normal_game.result(), UNRESOLVED)

For the uninitiated:

Implementing Partials in Python

A colleague of mine was wistfully pining for Haskell-style currying in Python. This sounded like an interesting syntactic feature, so I decided to see if I could toss together something that would at least emulate the behavior of returning functions when a function is called with too few arguments. I've always admired the flexibility of python, so it seemed like an interesting challenge to prove that.

Using a decorator seemed like a bit of a copout due to the additional syntax required, but was otherwise ideal, as it made checking arguments fairly trivial.

def partial(f):
    def wrapper(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except TypeError, t:
            return lambda *_args,**_kwargs:f(*(args+_args), **(dict(kwargs.items()+_kwargs.items())))
    return wrapper

Continue reading "Implementing Partials in Python"