Getting Branch Coverage In Nose

Just a few days ago Ned Batchelder announced experimental branch coverage support in coverage.py. I have been waiting a long time for this. I had a few false starts myself so I know how much time Ned put into it. Thanks Ned!

I changed the coverage plugin included with nose to interact better with coverage.py.

You can find my changes here: http://code.google.com/r/dstanekcom-python-nose/source/list.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

Hacking On A super Alternative

Python’s super built-in has always bugged me. I’m talking about something much more superficial than James Knight’s thoughts on it being harmful. I’m talking out its signature. I dislike the fact that it requires the current class and instance to be passed in. For example:

class SubClass(BaseClass):
def method(self, arg, this=that):
    super(SubClass, self).method(arg, this)

To avoid doing an real work I hacked together an alternative implementation. I was able to get something that appears to work correctly, but I can’t guarantee it. With my new super the code would look like:

from better_super import super

class SubClass(BaseClass):
def method(self, arg, this=that):
    super().method(arg, this)

This should not be used in any real production code. There are likely bugs lurking in corner cases and strange failure modes. It’s also four times slower than the built-in super.

It was still interesting to explore the implementation and I learned a bit about frames in the process.

My hacky implementation:

better_super.py

import inspect as _inspect from __builtin__ import super as _builtin_super def _what_class_am_i_in(mro, func_name, func_code): for c in mro: if getattr(c, func_name).func_code is func_code: return c return None def super(): frame = _inspect.currentframe().f_back arg_info = _inspect.getargvalues(frame) self = arg_info[3][arg_info[0][0]] mro = _inspect.getmro(self.__class__) method_name = frame.f_code.co_name code_obj = frame.f_code cls = _what_class_am_i_in(mro, method_name, code_obj) return _builtin_super(cls, self)

I’d love to hear about other ways to implement this. Have you tried yet?

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

PyOhio 2009 Call For Proposals

The PyOhio Call for Proposals has been issued!

PyOhio

PyOhio 2009 takes place July 25-26, 2009 at the Ohio State University in Columbus, Ohio. Much like a mini-PyCon, it includes scheduled talks, tutorials, Lightning Talks, Open Spaces, and room for your own unique ideas. If you can make it to Ohio this summer, please consider participating.


PyOhio 2009, the second annual Python programming mini-conference for Ohio and surrounding areas, will take place Saturday-Sunday, July 25-26, 2009 at the Ohio State University in Columbus, Ohio. A variety of activities are planned, including tutorials, scheduled talks, Lightning Talks, and Open Spaces.

PyOhio invites all interested people to submit proposals for scheduled talks and tutorials. PyOhio will accept abstracts on any topics of interest to Python programmers.

Standard presentations are expected to last 40 minutes with a 10 minute question-and-answer period. Other talk formats will also be considered, however; please indicate your preferred format in your proposal. Hands-on tutorial sessions are also welcomed. Tutorial instructors should indicate the expected length

PyOhio is especially interested in hosting a Beginners’ Track for those new to Python or new to programming in general. If your proposal would be suitable for inclusion in the Beginners’ Track, please indicate so. Organizers will work with speakers and instructors in the Beginners’ Track to help them coordinate their talks/tutorials into a smooth, coherent learning curve for new Python users.

All proposals should include abstracts no longer than 500 words in length. Abstracts must include the title, summary of the presentation, the expertise level targeted, and a brief description of the area of Python programming it relates to.

All proposals should be emailed to cfp@pyohio.org for review. Please submit proposals by May 15, 2009. Accepted speakers will be notified by June 1.

You can read more about the conference at http://pyohio.org

If you have questions about proposals, please email cfp@pyohio.org. You can also contact the PyOhio organizers at pyohio-organizers@python.org.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

I’m Planning a PyCon 2010 Talk

During Andrew Kuchlings’s talk on How to Give a Python Talk he mentioned the top rated PyCon talks over the years. I was extremely surprised to see the talk Mike Pirnat and I gave in 2006 on the list. The video for Python Can Survive In The Enterprise is available here if you missed it.

I’m now motivated to do a talk next year. I have a large number of commitments so I’m starting the preparation early.

Possible Topics:

  1. Python Dependency Injection and snake-guice
  2. Virtual Hosting with WSGI - applications on multiple domains in the same app server instance

All feedback is welcome. I’m open to talk suggestions. Is there something that I do or work on that may make an interesting talk?

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

Simple Random Number Generator For #codemash

Each #CodeMash attendee’s badge had a unique integer that was used in a raffle to give away prizes. Unfortunately random.org was used to pick the set of lucky winners.

Unsurprisingly the sequences of random numbers generated by random.org contained duplicates. Several numbers like 123 were called 3 or 4 times. I would have liked each number to be called only once. Python to the rescue.

To get a randomized list of all attendees:

import random, sys
all_attendees = range(550)
random.shuffle(all_attendees)

When run this script will print a random number each time you press enter until the range is exhausted. CTRL-D will let you exit early if all the prizes are gone. The range is 0-549.

import os
import random
all_attendees = range(550)
random.shuffle(all_attendees)
for attendee in all_attendees:
    try:
        raw_input()
    except EOFError:
        os.exit(0)
    print attendee
print '\nAll attendee numbers have been exhausted.'

Replace 550 with the actual number of attendees.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

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.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

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.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

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.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

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…

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

Turning Off Import Warnings In Python

This works for all warnings.

http://docs.python.org/lib/warning-filter.html

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

Next Page →