Archive for python

Setting up your IDE for web2py development

Recently I have started developing an application using the excellent Python web framework web2py. Web2py comes with its own a web-based integrated development environment that makes it easy to write small web based applications from within your browser. However once you start developing something bigger you will want the convenience of a real IDE. Code completion, multiple open files and a debugger will all make the experience more comfortable. However due to web2py’s mode of operation your IDE will require some additional help from you to allow its full powers to blossom.

Web2py’s mode of operation

Contrary to other Python web frameworks, such a Django, web2py doesn’t use Python’s import statement to load your code. Instead it executes your code using Python’s built in function execfile and supplies it with a prepared environment that contains a number of global objects that are central to web2py’s programming model (e.g. request, response, session objects, HTML helper functions, etc). This also means that your IDE has no knowledge of these objects and functions unless you explicitly tell it about them.

Informing your IDE about web2py’s prepared environment

Even though I use Eclipse/PyDev, the trick to let any Python aware IDE know about these objects is the same. In fact I got the idea from the How-To Using Wing IDE with web2py.

From the IDE’s point of view there are two types of files that it requires additional information about:

  • models
  • controllers

Simply adding import statements for the global objects and functions web2py is going to provide us with at runtime might cause unforeseen problems. Hence we need to import them conditionally.

For models adding the following snippet of code at the top will suffice:

if 0:
    from gluon.sql import *
    from gluon.validators import *

For controllers the following will do:

if 0:
    from gluon.globals import *
    from gluon.html import *
    from gluon.http import *
    from gluon.sqlhtml import SQLFORM, SQLTABLE, form_factory
    session = Session()
    request = Request()
    response = Response()

How does this work? The conditional 0 will always evaluate to False. This means that at run-time the statements following the if statement will never be executed. It’s dead code and hence will never interfere with whatever web2py has setup for us in its prepared environment.

It’s different for the IDE. The Python parser that’s part of your preferred Python IDE only parses your code. It does not evaluate it as Python does. As such it has to take the possibility into account that it might be executed at some point in time and that other code might depend on it. It has to read in the imports and consequently has all the information needed for code completion. Your IDE is now aware of web2py’s prepared environment

Comments (8)

OS X 10.5.6 update broke Eclipse shortcuts

This evening I installed the PyDev plugin for Eclipse to write a little Python code. Seemed to go well until I noticed that some of my keyboard shortcuts didn’t work anymore in Eclipse. Most notably copy and paste (CMD-C and CMD-V). As this was working before I figured the PyDev plugin to be the culprit. However, in a version of Eclipse without the PyDev plugin the issue was still present. 

Some Googling turned up a post on StackOverflow titled Eclipse keyboard shortcuts broken in OSX 10.5.6. Turns out that the OS X update to 10.5.6 I did a couple of days ago broke the keyboard short cuts for Eclipse. But only if you’re using Dvorak keyboard layout. Guess what I am using? Right, Dvorak!

I knew I should have waited a little longer with the update. I normally do. Not sure why I didn’t this time. Sincerely hope Apple comes out with a fix quickly.

Leave a Comment

Devoxx ‘08: The Good & The Bad

This year I attended Devoxx; the largest Java conference in Europe. I had high expectations as I previously had heard many good things about JavaPolis (Devoxx’ former name). However after three days of attending presentations I returned home with mixed feelings. First the good.

The Good

A conference of this size is bound to give you a good feeling of what is going on in the Java world. It might be my pick of the presentations but I got the feeling that nothing shockingly new was happening (except for arguably JavaFX, but more on that later). Instead the focus seemed to be on consolidating that what is working for the Java developers and addressing real issues Java developers are dealing with.

For instance in Thursday’s keynote Joshua Bloch talked about enums and generics and how to use them effectively. A good thing as I have seen many situations where developers should have used enums, but opted for a more conventional solution instead. As for generics, if things get more complicated then ArrayList<String> I have to slow down and be really careful about what I do. Any tips on using generics effectively is always welcome.

A good presentation was John Ferguson Smart’s presentation on “Behavior Driven Development in Java with easyb”. Again nothing shockingly new, but it did clearly point out problems many developers have with Test Driven Development; what to test? By focussing on the required behavior that needs to be implemented and using a testing framework that allows this to be expressed easily, writing meaningful tests should be a lot easier for many developers.

There was a strong focus on running other languages then Java on the JVM. Bill Venners presented on the “Feel of Scala” though I didn’t attend that one as it had been scheduled at the same time as the presentation on easyb. Nor did I attend Charles Nutter’s and Thomas Enebo’s presentation on JRuby. However I did attend Jim Baker’s and Tobias Ivarsson’s presentation on Jython and Brian Goetz’ “Towards a Dynamic VM”. Both were great.

I was thrilled to hear Jython is alive and kicking. With all the focus on JRuby, Scala, and more recently Clojure in blogosphere, one might have gotten the impression that Jython was all but dead. Instead a release that’s compatible with Python 2.5 is imminent and they demonstrated Django running on Jython.

Dynamic languages on the JVM are already quite fast compared to their relatives written in C. However with the enhancements planned for the JVM this should improve significantly in the future. Brian Goetz’ presentation was fairly technical and as such one of the better ones. It made me well aware of all the work the JVM has to do in running dynamic languages. Knowing this made me appreciate the current speed of the dynamic language on the JVM even more.

Another thing worth mentioning was the organization of the conference. They did a great job. From registration, sending my badge to my home address, lunch, coffee, snacks, drinks, announcements. Everything was organized very well allowing me to focus on what I had came for, attending presentations and talking to people.

The Bad

Even though there were a fair number of good presentations some were really awful. Most notably IBM’s keynote presentation on Java and RFID. IBM is a large multinational. Devoxx is a huge conference with 3300 attendees. You would have figured they would have made an effort in delivering an interesting keynote. Looking at the result they did not. It was embarrassingly amateurish and clumsy.

They same holds for many other presentations. Some had potentially interesting content, but due to the way it was presented it was difficult to follow, or outright incomprehensible. I do realize it is easy for me to criticize people sitting behind my keyboard and writing a blog entry. After all I did not have to stand in front of a huge room filled with anything between 60 to 1000 people. But even so, more presenters should have done significantly better for a conference of this size. 

And last JavaFX. Not necessarily bad as the presentations on it that I did attend were well executed and interesting. However I failed to see JavaFX’ relevance for enterprise Java developers. I might be wrong, but I would expect most of the attendees to fall into this class of developers. Sun however seemed to target a different class of developers. Nice graphics, animations, sound and video is all great. However I as an enterprise Java developer am more interested in how I can construct forms, the kind of controls that are available, communication with back end systems, etc. None of that was addressed. A pity and a missed opportunity for Sun.

Leave a Comment