Hunting the Shmoo

Screencasts and blog posts on workflow, productivity, tools, Mozilla and whatever else tickles my fancy.

When would you use a Python mixin?

That’s not a rhetorical question. I’d like to know in which scenarios a mixin in python really is the best option. I can’t seem to think of any, but maybe I’m not thinking outside the box enough.

The basic idea of a mixin is to create a small re-usable class that can “plug-in” to other larger classes. From the wikipedia definition, a mixin is a way to compose classes together without using inheritance. The problem is unlike ruby, python mixins are a purely conceptual construct. Python mixins are inheritance (the only difference is that the class name usually contains ‘Mixin’). It is up to the developer to remember this, and to manually avoid all of the common pitfalls of multiple inheritance. This kind of defeats the whole purpose of the mixin in the first place. What’s more is that most people use python mixins improperly.

Read more →

Python Logging - Format a message without printing it

I came across an odd python problem the other day. I wanted to format a log message without printing it with python’s logging module. But I couldn’t find any examples or others who wanted to do the same thing.

You might ask why I would want to do this. In my case, I was using sys.stdout.write() to display the progress of a download as a percentage. Python’s logging module automatically appends a newline to your message and there didn’t seem to be a way to change this without a subclass. Anytime you want to overwrite something on the previous line with python logging, you will run into trouble.

Read more →