Saturday, July 18, 2009

PyDev 1.4.7 on Eclipse Galileo

WARNING:

The content of this blog is obsolete now, as newer PyDev version 1.5 has been released. Please look at my latest blog post on this.

I begin to have more time to explore the Eclipse Galileo (3.5) again. This time I want to set it up for my Python development, using the PyDev plugin. Current PyDev plugin version is 1.4.7

Assuming you already have a working  installation of Galileo, and have opened a workspace.

The update site for PyDev is http://pydev.sourceforge.net/updates

In Galileo, you need choose: Help > Install New Software...

[caption id="attachment_605" align="aligncenter" width="313" caption="Adding the PyDev Plugin to Eclipse Galileo"]Adding the PyDev Plugin to Eclipse Galileo[/caption]

The "Install" dialog will show up.

eclipse-update-02

Click on the [Add] button at the top right of dialog. You should see this "Add Site" dialog.

eclipse-update-03Type in the data.

Name: Py Dev Update Site
Location: http://www.fabioz.com/pydev/

eclipse-update-04

Click [OK] button.
eclipse-update-05

Tick the checkbox "PyDev for Eclipse" under "PyDev". Click [Next >] button.
eclipse-update-06

Click [Next >].

eclipse-update-07

Click the radio button "I accept the terms of the license agreement" when you agree. Click [Finish] button.

You will see the progress bar.

eclipse-update-08

On completion of the installation process, Eclipse will ask you whether you want to restart.

Answer by clicking the [Yes] button.

Done.

Thursday, July 9, 2009

Python's Lazy Evaluation on Exception Handling

I tried a simple code like this:

[sourcecode lang="python"]
try:
while True:
print('yipee')
except KeyboardInterrupt:
print('w00t')
[/sourcecode]

It runs successfully on command line interface, and keeps printing "yipee" until the user press Ctrl-C. After that I did a typo, which turns out to mistype the "KeyboardInterrupt" as "KeybaordInterrupt".

[sourcecode lang="python"]
try:
while True:
print('yipee')
except KeybaordInterrupt:
print('w00t')
[/sourcecode]

To my surprise, it still runs well, it keeps printing "yipee" to the screen. It's just that
when I press Ctrl-C, Python threw this error:

[sourcecode lang="shell"]
Traceback (most recent call last):
File '<stdin>', line 3, in <module>
KeyboardInterrupt
[/sourcecode]

During handling of the above exception, another exception occurred:

[sourcecode lang="shell"]
Traceback (most recent call last):
File '<stdin>', line 4, in <module>
NameError: name 'KeybaordInterrupt' is not defined
[/sourcecode]

I looked at the documentation of Python 3.1 which says:
The try statement works as follows.

First, the try clause (the statement(s) between the try and except keywords) is executed.

  • If no exception occurs, the except clause is skipped and execution of the try statement is finished.

  • If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement.

  • If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.


The exception handling in Python works in a lazy manner, they will only validate the except clauses just before processing the exception!