Article Help - Dependency Injection in Python
I believe dependency injection can improve the design and quality of Python applications. It seems this idea is somewhat controversial. My previous post invoked a wide variety of responses. (It was the first time I got email resembling hate mail
It seems that a longer and more detailed explanation may be helpful.
I am working on an article describing the benefits of dependency injection to Python applications. If you have reason to believe otherwise I would really like to hear from you. I want to make sure that the article addresses the community’s thoughts. If you don’t know what dependency injection is or just want to say that Python is not Java don’t bother.
I look forward to reading your comments! Go ahead and comment on this post or email me at dstanek [at] dstanek [dot] com. Thanks in advance.
Coding Lessons Can Be Learned From Writing
No really, it’s true. James Devlin wrote an interesting article about it. He illustrates how the lessons from The Elements of Style by William Strunk and E.B. White apply to software development. Mitch Wheat also wrote a similar article showing a few concise examples.
If nothing else these new perspectives should give you something to think about.
A figleaf Text Coverage Report
figleaf is an indispensable tool for calculating code coverage. I use it to ensure my unit tests are testing a significant portion my code. It includes several reports, but is missing the one I want most - a simple text report.
I wrote figleaf2txt to generate a text report similar to the one generated by Ned Batchelder’s coverage.py. Running figleaf2txt on coverage data will print a simple report to the terminal.
Additionally I wrote a nose plugin to run the report immediately after a test run. Again just like coverage.py. You simply run nose --with-figleafreport to see the report on your terminal.
All work has been done in a Bazaar branch that started with figleaf version 0.6.1. Your can see my progress and participate in development by creating your own branch from mine.
My code works, but could stand a little clean up. There are lots of other small things I would like to fix up when I get the spare time, but for now my changes work good enough for me. Maybe these changes will eventually make it into the official distribution.
snake-guice Binder Options
snake-guice provides a simple DSL to wire up an application’s dependencies. The current version implements this with chained method calls like google-guice. An example from one of the unit tests:
binder.bind(ch.Person)\
.with_annotation('evil')\
.to(ch.EvilPerson)\
.in_scope(scopes.CherryPyRequest)
After using this in a couple of trivial apps I am not so sure I like it. Long chains of method calls are usually regarded as a code smell and it just feels strange in Python. As a replacement syntax I was thinking something more like:
binder.bind(ch.Person,
annotated_with='evil',
to=ch.EvilPerson,
in_scope=scopes.CherryPyRequest)
The new syntax does feel better, but I still feel that something is missing. Either way I like the fact that both examples are in Python. That is a pretty strong requirement here.
The BlackBerry Curve As A Bluetooth Modem On Ubuntu
It turned out to be extremely easy to use the BlackBerry Curve’s tethered modem support from Ubuntu. This allows the device’s dial-up networking capabilities to be used to access the internet. This will work with either a Bluetooth or USB connection. The instruction for using Bluetooth can be found here.
Unfortunately my BlackBerry is a corporate phone and using the tethered modem costs an extra $15 per month. So I’m out of luck at least for now.
Northeastern Ohio Tech Book Club
A new book club for Northeastern Ohio techies has been formed. The idea, first proposed on Twitter by Corey Haines, is to get together on a regular basis to discuss and debate some book. If your a techie, you like to read and your in the area then consider joining. Details are still TBD.
Our first book will be Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin. Our first meeting will be soon!
ANN: snake-guice Preview - A Dependency Injection Framework
snake-guice is a dependency injection framework for Python. It has been heavily inspired by google-guice.
snake-guice is still in development and will not be ready to be used in production environments for a few weeks. I’m putting this out there in hopes of getting constructive feedback. Unfortunately the documentation is non-existent so the best place to understand how to use snake-guice is from the unit tests. The API tests are the best examples.
If you don’t already know about dependency injection or how it helps then this project may not be for you just yet.
More to come…
Turning Off Import Warnings In Python
This works for all warnings.
http://docs.python.org/lib/warning-filter.html
I’m A CrackBerry Addict
Last year when my manager mentioned getting a BlackBerry I really didn’t like the idea. I didn’t want to have it be a drain on my productivity. It also seems like a ball-and-chain. Now my calendar is so hectic that I just went ahead and got one.
I now have a shiny, new 8330 and I couldn’t be happier. Now I know why they call it a CrackBerry. I find myself reading my Gmail, Google News or whatever in what was previously wasted time. I’m no longer bored while standing in line!
Hopefully this new addiction won’t become problematic.
Tip: Use string literals instead of the pass statement?
The pass statement is simply used where the Python syntax requires a statement, but the application doesn’t need any logic. Typically it is accompanied by a comment explaining why it is there. A simple example:
my custom exception
class MyException(Exception): pass if condition: pass # we don’t need this right now - testing
More often than not I will use a triple quoted string in its place. This makes it more self documenting and looks cleaner to me. My preferred usage:
class MyException(Exception):
"""my custom exception"""
if condition:
"we don't need this right now - testing"
What do you prefer?




