Tag Archives: python

How to set session variables in Django unit tests.

This was super non-obvious, and I lifted code from a couple of different places. If you’ve got Django view code that gets & sets session values, you’ll want to test it properly, and the standard “self.client” from Django’s TestCase doesn’t … Continue reading

Posted in General | Tagged , , , | Leave a comment

git pull says “You are not currently on a branch…”

Was working through some git error messages generated by pip installs of some Python code, and found that the issue was caused by this error: $ git pull You are not currently on a branch, so I cannot use any … Continue reading

Posted in General | Tagged , , , | 2 Comments

Breaking out of the middle of a try block in Python

I had some code where I wanted to break out of a try block in Python. The code looked something like this: try: print “First” if some_condition: # What do I put here to break out of the try block? … Continue reading

Posted in General | Tagged , , , , | 7 Comments

pymacs, ropemacs, and virtualenv, all at the same time.

So, you’re developing for Python, and you want to use rope and ropemacs and pymacs, but it’s totally busted when you use your virtualenv. In addition, the packaged versions of pymacs+ropemacs for ubuntu 10.10 (and 11.04) are also kind of … Continue reading

Posted in General | Tagged , , , , , , | Leave a comment

An extremely simple config pattern for Python

So, you’re developing a Python application. And, you have needs for “production” versus “development” configs. If you’re using a web framework, you might have some support for doing this, but what about your other modules that you’d like to configure … Continue reading

Posted in General | Tagged , , , , , | Leave a comment

Extremely simple XML to Python converter

There are a ton of different ways you can encode XML, but for the style that looks like this: <xml> <name>Bob</name> … You can use this nice little function to convert it to a native Python object.  I’m using this … Continue reading

Posted in General | Tagged , , , , | Leave a comment

Sometimes Python is bat-guano insane

Try this on for size: import pickle d = {‘a’: 1} s1 = pickle.dumps(d) s2 = unicode(s1) assert(s1 == s2) d1 = pickle.loads(s1) d2 = pickle.loads(s2) assert(d1 == d2) So, what do you think happens here? Are s1 and s2 … Continue reading

Posted in General | Tagged , , , | 2 Comments

A dict and an object, all in one.

I’ve been struggling with data modeling decisions for my MongoDB interface layer.  Should results from the DB look like a dict, or like an object?  Like both?  What are the advantages and disadvantages of each approach?  I’ve got my own … Continue reading

Posted in General | Tagged , , | Leave a comment

methodpickle: A Python library for pickling function invocations.

Have you ever wanted to say “I’d like to call this function, but later”? I’ve seen people using Celery for this purpose, and it’s very well respected, but the setup is far from easy.  It has several fairly large dependencies, … Continue reading

Posted in General | Tagged , , , | 2 Comments

Trivial function-based and class-based Python decorators.

Here’s a trivial function-based decorator: def wrap(method): def call(*args, **kwargs): print “calling wrapped method” return method(*args, **kwargs) return call @wrap def some_function(arbitrary, arguments=None): print “%s %s” % (arbitrary, arguments) And here’s a trivial class-based decorator: class Wrap(object): def __init__(self, method): … Continue reading

Posted in General | Tagged , | Leave a comment