Python decorators, SRP, and testability

Posted by Mike on Jun 4th, 2010

On the SRP:
For those unfamiliar with the Single Responsibility Principle (SRP), it states that there should never be more than one reason for a class to change.
That is to say: do one thing, do it well.
Decorators (not to be confused with the decorator pattern) can add behaviors or side-effects to a method, and this can [...]


Batch convert images to sepia tone with python

Posted by Mike on Jan 13th, 2010

The Python Imaging Library (PIL) offers easy photo manipulation from python scripts. There’s some handy sample code on effbot.org that demonstrates how to alter an image’s palette to generate a sepia tone effect. It first desaturates the image, then applies a new palette based on a linear ramp.
I’ve cleanup up that sample code [...]


VIM Tip of the Day: leave python comments indented, don’t put cursor at beginning of the line

Posted by Mike on May 15th, 2009

If you’re ever been annoyed by VIM throwing your cursor to the left margin when you type “#” in a python file, there’s a simple remedy. It’s the fault of “smartindent” in VIM, which in reality isn’t all that smart. If you want to leave smartindent on but fix the weird hash behavior, [...]


Notes from PyCon 2009

Posted by Mike on Mar 29th, 2009

Just wrapping up a fantastic few days at PyCon Chicago 2009. I went with a group of about 10 guys from AGI, and we managed to have a little bit of fun in between some great mind-enriching talks.
Here are some notes I jotted down while at the conference. It’s a bit overwhelming, but [...]


I used to play sports. Then I realized you can buy trophies. Now I’m good at everything.

Posted by Mike on Feb 19th, 2009

I stumbled across “Dammit I’m Mad” by Demetri Martin, a rumored-to-be 224 word palindrome poem.
I was impressed, but needed to check the accuracy of such a statement. So I dialed up a python session.

import re
awesomedrome = """Dammit I’m mad.
Evil is a deed as I live.
God, am I reviled? I rise, my bed [...]


Java is a Bad Ex

Posted by Mike on Feb 17th, 2009

Although I’ve been enamored with Python for the last 13 or so months, I’ve often dreamt longingly for days of Java glory. Things from the past seem so desirable at times — IDEs with good code completion and integrated debugging, staticly-typed compiler hand-holding, mature application servers and deployment tools. There was never much [...]


PyCon 2009

Posted by Mike on Feb 15th, 2009

I’m going to PyCon 2009! I’ll be there with a few other guys from work. We’re flying in Wednesday night, taking a tutorial on Thursday, and trying to have a little fun throughout the weekend.


Including external data with your unit tests

Posted by Mike on Jan 13th, 2009

We generally preach that you should not use real data in your unit tests. You should instead mock said data out in an effort to lesson potential side-effects, including degrading test performance and adversely affecting live data.
Sometimes, you’d like to test actual file handling in a unit test. If you do, you should [...]


Python multiline regex

Posted by Mike on Jan 13th, 2009

After pulling most of my hair out, I realized a simple mistake in my Python regular expression usage. I had been trying to search a simple HTML document for some text, which happened to be broken across a newline such as:

<div>I’m some
[...]


Tamper-proof python dictionary

Posted by Mike on Sep 22nd, 2008

Sometimes the need arises to pass a python list, tuple, dictionary, etc. from one page to another. If you do not have a session service to hold it, the object may need to pass through a HTTP request (in cookie or get/post param), opening the possibility of tampering. Without some precautions, a serious [...]


Next »

Categories