John McCollum

Book Review: Django 1.1 Testing and Debugging

A wealth of tools are available to debug and test Django applications, but knowing when and how to use these resources can intimidate the new user. Django 1.1 Testing and Debugging, by Karen M. Tracey, aims to walk the user through the process of creating a web application from scratch, ensuring that the resulting code is bug-free and ready for production.In a way, Django makes it deceptively easy to write a dynamic web application. With a few lines of code, you can have an fully functional application up and running in a short space of time, and complex applications take less time than ever to develop. Inevitably, though, bugs will creep in to the development process, and the professional developer will want to make sure that their application is as bug-free as possible before launching.

The book opens with a simple question: “How do you know when code you have written is working as intended?” The answer, of course, is that you test it. But if you’re not a cowboy coder, you’ll want to leverage the full power of Django’s automated testing framework for best results. In the course of this book, the author develops a full web application, from start to finish, and describes how each section would be tested and debugged. Read More…

Posted in Django, Python at June 3rd, 2010. No Comments.

The easiest way to implement __iter__() for a python object

If you want to iterate over an object in Python, the simplest (and most Pythonic?) way is to use a generator. A nice simple example to demonstrate the utility and power of this technique:


class Foo(object):
    def __init__(self):
        self.mylist = [1,2,3,4,5]
    
    def __iter__(self):
        for i in self.mylist:
            yield i

You can then do:


>>> f = Foo()
>>> for i in f:
       print i
1
2
3
4
5

Concise, clear, and functional. :)

Posted in Python at May 27th, 2010. No Comments.

Another reason to love Python

Last night, I was doing some code kata at codingbat.com when I was presented with the following problem:

Given a string and a non-negative int n,
we'll say that the front of the string is the
first 3 chars, or whatever is there
if the string is less than length 3.
Return n copies of the front;

front_times('Chocolate', 2) → 'ChoCho'
front_times('Chocolate', 3) → 'ChoChoCho'
front_times('Abc', 3) → 'AbcAbcAbc'

Pretty straightforward, right? The solution from the site was the following:
def front_times(str, n):
  front_len = 3
  if front_len > len(str):
    front_len = len(str)
  front = str[:front_len]

  result = ""
  for i in range(n):
    result = result + front
  return result

I went for a totally different solution though:
def front_times(str, n):
   return n*(''.join([x for x in list(str[:3])]))

Is it something I’d use in production? Nope. It’s less readable and less pythonic, in my opinion, than the longer answer. (Although some would argue that you don’t get more pythonic than list comprehensions.)

Did it make me smile? Hell yes. Although ultimately, the most pythonic is probably:

def front_times(str, n):
   return n*(str[:3])

Posted in Python, web development at March 25th, 2010. No Comments.

Twitter Reddit Flickr LinkedIn Stack Overflow Github Email Ne RSS