Thursday, March 8, 2007

Andrzej on Test Driven Development

Test Driven Development helps me creating better software. TDD is not only about testing. It's more about designing and managing scope. I'll try to show what I mean by using a simple example based on tabbedimages application.

Tabbedimages is a simple image viewer. We are going to add drag and drop feature to it.
I start with a requirement, which I analyze and spike for a working solution. Based on the analysis I create a user story and an automated acceptance test. The acceptance test "drives" me when I'll add unit tests and the production code.
  • Requirement
    • tabbedimages is a simple image viewer.
    • We want to add a new feature to tabbedimages.
  • Title.
    • 'Drag&drop support'.
    • TIP: It's good to have a short title for a user story.
  • Analysis: let's list things to worry about:
    • Single image files
    • Multiple image files
    • Directories
    • Non-image files
    • Already open image.
  • Spiking
    • TIP: This part should give us better understanding of the problem
      • If we're sure how to implement the new feature we can skip this step.
    • tabbedimages is implemented using IronPython and Windows Forms.
      • The code is availalable here.
    • Google for 'drag and drop windowsforms' and see some code examples.
    • Check out the fresh version of tabbedimages.
    • Try to add the required feature to our code base (without tests).
    • Discover that Windows Forms has support for DragDropEffect.
      • Which displays a 'plus' sign if the thing that we're trying to drag is acceptable.
    • Add the DragDropEffect to the list of things to worry about.
  • User story:
    • Marten wants to drag and drop his images from his desktop to tabbedimages.
    • He starts tabbedimages.
    • He then drags the 'Faye001.jpg' file over the application.
    • The plus sign appears.
    • He drops it.
    • A new tab is created with a label saying 'Faye001.jpg'
    • 'She's cute' he thinks
    • Marten realizes that there are more Faye's pictures.
    • He drags 'Faye001.jpg' (again) and 'Faye002.jpg'.
    • He drops them.
    • Two new tabs are created.
    • He drags and drops readme.txt file.
    • The message box appears saying 'readme.txt doesn't appear to be a valid image file'
    • He quits tabbedimages.
  • Functional test:
    • Write the ideal code (DSL-like) that follows the user story steps:
    • marten.starts()
      he.asserts_number_of_tabs(0)
      he.drags('Faye001.jpg')
      assert shows_plus()
      he.drops('Faye001.jpg')
      he.asserts_number_of_tabs(1)
      he.asserts_tab_labels(['Faye001.jpg'])
      fayes_pictures = ['Faye001.jpg', 'Faye002.jpg']
      marten.drags_and_drops(fayes_pictures)
      marten.drops(fayes_pictures)
      he.asserts_number_of_tabs(3)
      he.asserts_tab_labels(['Faye001.jpg', Faye001.jpg', Faye002.jpg'])
      he.drags_and_drops('readme.txt')
      he.sees_message_box("readme.txt doesn't appear to be a valid image file")
      he.quits()
  • Implementation
    • Run the Functional Test (FT).
    • Whenever FT fails or you can think of any edge cases not covered by FT:
      • Write appropriate Unit Test that reflects the problem.
      • Add the implementation to fix the problem.
After the last phase the drag & drop feature should be ready to use. Of course, this example presented a very simple problem. Hopefully, the Test Driven Development as shown here should be easy to understand. In the near future, I'll try to write more about the implementation phase, including some refactoring techniques, so stay tuned!

Sunday, March 4, 2007

Doctests for showing snippets of code

From Public

  • djangosnippets.org shows many snippets of Python/Django code along with doctests.
  • Have a look at partitioning lists example.
  • This is what I think a good usage of doctests.
  • I'm not sure about using them *instead* of unittests.
  • Michael is also not sure
  • BTW, I find many of those Python code samples really pretty.
    • I couldn't find any with the string 'self' inside.
    • Maybe that's why I like them ;-)

def partition(thelist, n):
"""
Break a list into ``n`` pieces. The last list may be larger than the rest if
the list doesn't break cleanly. That is::

>>> l = range(10)

>>> partition(l, 2)
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]

>>> partition(l, 3)
[[0, 1, 2], [3, 4, 5], [6, 7, 8, 9]]

>>> partition(l, 4)
[[0, 1], [2, 3], [4, 5], [6, 7, 8, 9]]

>>> partition(l, 5)
[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]

"""
try:
n = int(n)
thelist = list(thelist)
except (ValueError, TypeError):
return [thelist]
p = len(thelist) / n
return [thelist[p*i:p*(i+1)] for i in range(n - 1)] + [thelist[p*(i+1):]]

Sunday, February 25, 2007

Writing Domain Specific Languages with Python

  • The process of writing DSL is the same in all languages.
    • You write an ideal spec.
    • Then, you try to fit the spec so that it's kind of valid syntax for the language you use.
  • An example of ideal domain language (well, maybe not so ideal, but quite good):

test '/Login':
GetReq(uname = 'a_user',
password = '')
expect_tmpl: 'login.tmpl'
expect_tmpl_has: 'errorMsg'
expect_tmpl_data: { 'uname':'a_user',
'missing_fields' : ['Password']}
expect_session_lacks: 'uid'
  • Now the same but as a valid Python syntax:

test_serv('/Login',
GetReq(uname = 'a_user',
password = ''),
expect_tmpl = 'login.tmpl',
expect_tmpl_has = 'errorMsg',
expect_tmpl_data = { 'uname':'a_user',
'missing_fields' : ['Password']},
expect_session_lacks = 'uid')

  • Useful tricks inclue:
    • keyword args (expect_tmpl_data=...)
    • unbounded arg lists (*args)
    • operator overloading __add__(self, other_form)
    • reflection via hasattr, getattr, vars and friends
  • Thanks to Dan Milstein for his talk at PyCon about Little Languages in Python.

IronPython and Windows Forms talk

The presentation in html version

TabbedImages application

  • We got some good feedback after the presentation.
  • To my surprise there were many people already playing with IronPython.
  • What's even more surprising, there were some people that have already used Windows Forms.
  • Watch the tabbedimages application, I'm going to extend it with some interesting features.
  • If you browse the sources for it, you'll find some simple tests in the MainTest.py.
  • They exist because I'm afraid I'm not able to not do Test Driven Development.

Saturday, February 24, 2007

PyCon 2007

The PyCon conference is really great.
So far I was at the following talks:
  • Internationalization
  • Parsing revisited
  • Developing desktop applications with Dabo
    • I didn't know that creating applications with wxPython is such a pain.
    • The Dabo core is simply a layer above the wxPython.
    • The application they used on the presentation was an image viewer.
    • The API was really nice and in many cases reminded me a simplified version of WinForms API (where you uses strings instead of overused in .Net enumerations).
    • wxPython (so Dabo as well) works on all important platforms (Windows, Linux, MacOS)
  • WSGI - an introduction
    • Because there are many different Python web frameworks, WSGI goal is to be able to integrate different layers of different applications.
    • I'm still not sure how it could be used in the Ruby world and if there is any need for that.
  • Writing parsers and compilers with PLY
    • Recently, I'm working quite a lot with parsers and PLY is the main tool we use.
    • It's very good, very efficient, very elegant
    • You declare your grammar rules using docstrings
  • Creating the WhatWhat project with TurboGears
    • WhatWhat is a very simple project tracking tool that was implemented in 4 days using TurboGears.
    • It seems that frameworks like TurboGears are becoming quite easy to learn and use.
    • Not sure if as easy as Ruby on Rails, though.
    • I'm still not convinced what could be the reason for me not to use Rails in web applications :)
  • pyweek: making games in 7 days
  • Creating games with Pygame on the GP2X
  • SQLAlchemy
    • Looks like a very powerful ORM tool for Python.
    • Reminds me of Hibernate.
    • There is a tool called Elixir which is a layer on top of SQLAlchemy and is more similar to the Ruby ActiveRecord.
    • Migrations are only available as extensions.
    • However, they are different than Ruby migrations. I didn't quite understand what is really the difference. I was told that Ruby migrations are dangerous because they do too many things for you (!?).
    • It seems to me the above is one of the fundamental differences between Python and Ruby programmers.
  • IronPython: Present and future
    • That was definitely the best talk so far, IMO.
    • Jim is doing a great work in Microsoft.
    • My name even appeared at his slide :-)
    • He wasn't able to read my name, though ;-)
    • What's so difficult with my name? - Andrzej Krzywda
    • There were screenshots of Michael's tutorial on IronPython and Windows Forms.
    • Seo was mentioned, of course. He is the author of FePY - the Comunnity Edition of IronPython.
    • The reason of this special edition is that MS can't easily apply patches that are sent by non-MS people.
    • IronPython is supported by the MS Robotics project.
    • Jim presented a small IP script where he used some MS Gaming API to create a simple 3d game.
    • There is a growing support for IronPython in ASP.NET
  • Embedding Little Languages in Python
    • Little Languages is what the rest of the world calls Domain Specific Languages.
    • The talk was about how to use Python to create such a DSL.
    • The idea is the same as always with DSL:
      • Firstly, you think of an ideal specification.
      • You try to use the language features to fit to the spec.
    • Python features that are helpful:
      • Keyword args
      • * args
      • getattr, hasattr
    • The speaker used _ convention in his code examples.
    • I also prefer it, but it doesn't seem to be so popular in the Python community (it is in Ruby community).

Friday, January 19, 2007

Groovy and SwingBuilder

In my comparison of IronPython, Groovy and JRuby I used a "naked" Swing API to create a GUI application using Groovy. Groovy comes with a nice library called SwingBuilder that simplifies a lot of work with Swing. I couldn't find any documentation for SwingBuilder but the source code is pretty straight-forward. In my opinion, the best thing in the SwingBuilder version is the way you declare that a form has a button:

def frame = swing.frame(defaultCloseOperation: WC.EXIT_ON_CLOSE) {
button('OK', actionPerformed: click)
}

You can pass a closure to the frame method.
I really like this way. It allows you to create your GUI in a more declarative way.

As was suggested by Martin C. Martin and Dierk Koenig (many thanks!) on the Groovy-user list, the code might look something like the following:

import javax.swing.WindowConstants as WC

click = { event ->
event.source.text = "Pressed!"
}

def swing = new groovy.swing.SwingBuilder()
def frame = swing.frame(
size: [200, 200],
defaultCloseOperation: WC.EXIT_ON_CLOSE,
title: 'Hello' ) {
button('OK', actionPerformed: click)
}

frame.show()

Thursday, January 11, 2007

Iron Python on Balloons

Have you ever wondered how to create a simple application that uses balloons to notify about some events? Below is the result of my spike on how I could use IronPython and Windows Forms to create such thing.



Basically, it should:

  1. show an icon in the system tray

  2. NotifyIcon is a class that allows you to display an icon in the system tray.

  3. display a tooltip every n milliseconds

  4. It was easily achieved by using the notifyIcon.ShowBalloonTip method and the Timer (and its Tick event) class.

  5. have a context menu that allows you to exit the application

  6. The ContextMenu class.


Just paste the code to the Main.py file, prepare a test.ico file and run it with:
ipy Main.py

import clr

clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Drawing import Icon
from System.Windows.Forms import (Application, ContextMenu,
MenuItem, NotifyIcon, Timer)


class Main(object):

def __init__(self):
self.initNotifyIcon()
timer = Timer()
timer.Interval = 60000
timer.Tick += self.onTick
timer.Start()


def initNotifyIcon(self):
self.notifyIcon = NotifyIcon()
self.notifyIcon.Icon = Icon("test.ico")
self.notifyIcon.Visible = True
self.notifyIcon.ContextMenu = self.initContextMenu()


def onTick(self, sender, event):
self.notifyIcon.BalloonTipTitle = "Hello, I'm IronPython"
self.notifyIcon.BalloonTipText = "Who are you?"
self.notifyIcon.ShowBalloonTip(1000)


def initContextMenu(self):
contextMenu = ContextMenu()
exitMenuItem = MenuItem("Exit")
exitMenuItem.Click += self.onExit
contextMenu.MenuItems.Add(exitMenuItem)
return contextMenu


def onExit(self, sender, event):
self.notifyIcon.Visible = False
Application.Exit()


if __name__ == "__main__":
main = Main()
Application.Run()

Update:Marcin shows how to achieve the same with Groovy.