Python Programming, news on the Voidspace Python Projects and all things techie.

Resolver is Hiring!

emoticon:exclaim Resolver is hiring! We are looking to hire three new developers, so if you fancy developing with IronPython in London (and working with a great team) then visit our jobs page.

In case you haven't heard of us before, we're developing a desktop application mainly aimed at the financial markets. Resolver is a Rapid Application Development Tool with a Spreadsheet Interface, and is written almost entirely in IronPython. We use agile development techniques, including Test Driven Development, pair programming, continuous integration and the like. At the moment we are a small company, with only six developers.

Experience with Python and agile development techniques are not a requirement, but certainly won't count against you...

We also have a Resolver News Page so you can keep track of what we're up to. Smile

Like this post? Digg it or Del.icio.us it.

Posted by Fuzzyman on 2007-08-17 14:12:19 | |

Categories: ,


Format Tracebacks from Current Exception

emoticon:music One of the interesting pieces of the Mini Silverlight Python IDE is the code that formats tracebacks from exceptions. These are printed to the debugging pane.

This code comes originally from Resolver and I have reused it with permission from my long suffering boss. We needed this because we were using format_tb from the traceback module. When there were many cells with errors then Resolver ground to a halt. When we came to look at the reason for this, we discovered that format_tb uses the linecache module to attempt to find the line of source code that each stack frame in the traceback refers to. As we were executing compiled code, this was doomed to failure. It caused a disk access for every directory in sys.path for every frame in the exception for every cell with an error.

The following code just extracts the core code needed to find line numbers and names for every frame in a traceback. Even though IronPython doesn't use Python stack frames (or code objects), IronPython reconstructs the necessary objects so that code like this will work.

import sys

def StackTraceFromCurrentException():
    traceback = sys.exc_info()[2]
    stackTrace = []
    while traceback is not None:
        frame = traceback.tb_frame
        lineno = traceback.tb_lineno
        code = frame.f_code
        filename = code.co_filename
        name = code.co_name
        stackTrace.append((filename, lineno, name))
        traceback = traceback.tb_next
    sys.exc_clear()
    return stackTrace


def formatLine(lineInfo):
    fileName, lineNo, name = lineInfo
    line = '  File "%s", line %s, in %s' % (fileName, lineNo, name)
    return line

def executeCode(code):
    if not code:
        print 'No code to execute.'
        return

    code = code.replace('\r\n', '\n') + '\n'
    context = {'__name__': '__main__'}
    try:
        exec code in context
    except Exception, e:
        print
        print "Traceback (most recent call last):"
        for line in StackTraceFromCurrentException():
            print formatLine(line)
        print e.__class__.__name__ + ": " + str(e)
        print

If you need to present exception tracebacks to the user, you might find this useful. The explicit call to sys.exc_clear() is unnecessary for CPython but is needed in IronPython to work around a bug.

Like this post? Digg it or Del.icio.us it.

Posted by Fuzzyman on 2007-08-15 00:04:05 | |

Categories: , ,


How Badly will IronMonkey Hurt Performance?

emoticon:mobile_world 'Someone' (I couldn't easily find a name, sorry) has written a blog post about the Mozilla project IronMonkey, which aims to port IronPython and the other DLR languages to run on the Tamarin JIT.

In How Badly will IronMonkey Hurt Performance? he declares:

I'm actually glad we're seeing this sort of a project go forward. Perhaps before we return to our sensibilities, we need to see a very evident failure. In terms of performance, I think this effort may just lead to such a disaster.

His thinking goes roughly along these lines:

  • Python is slow
  • IronPython is slower
  • Javascript is slow so the Tamarin implementation will probably be even slower
  • Therefore IronMonkey on top of Tamarin must be unbearably slow

It seems to me that just about all of these steps are wrong, and the blog didn't seem to be accepting comments, so I'm posting a response here.

My goodness! Which misconception to unpick first. Smile

First of all, amongst the dynamic languages Python is not generally considered slow. For example here is a quote from the Python main page from the creators of Youtube (which is almost entirely written in Python and serves 16 billion pages a month):

YouTube.com:

"Python is fast enough for our site and allows us to produce maintainable features in record times, with a minimum of developers," said Cuong Do, Software Architect.

A variant of Python called Stackless Python also drives the online game Eve, where performance is understandably critical.

As for IronPython, well... In several key areas it outperforms CPython. Overall they run at similar speeds, but because of the .NET JIT it is possible to have IronPython code run faster.

Large applications can be written in Python and IronPython. For example, the company I work for is developing a spreadsheet application with IronPython. Performance is comparable with other spreadsheet applications and we have over 100 000 lines of IronPython code in our application and test framework.) Every time we have had to improve performance we have managed to do so by refactoring our Python code and have never (yet) had to drop into C# for performance gains.

IronRuby is still young, but the creators of JRuby have seen a similar thing with their Java implementation of Ruby. Overall performance is similar, but some parts run faster. That dynamic languages are slow is something usually said by those who don't use them!

Onto:

"past JavaScript implementations have never performed very well, so it's quite possible that the future implementations will suffer from performance issues"

Interpreted Javascript generally is slow (although it may be 'fast enough' in many cases). IronPython running inside the Silverlight VM will typically perform operations a couple of orders of magnitude faster than interpreted Javascript. Performance is one of the reasons that Mozilla are switching to Tamarin, and it looks like the performance benefits they will get from this JIT compiler are going to be impressive.

This VM is not as fast as the .NET VM - but then the Tamarin core binary weighs in at around a slender 200kb. IronPython running on Tamarin may well be slower than IronPython on .NET - but I still expect it to be very usable. Declarations of doom and disaster are definitely premature!

None of this is to say that IronMonkey is a guaranteed success of course, but if anyone is capable then Seo is. Smile

(Of course another point is that Silverlight already proves that Python running in a VM in a web browser needn't be slow - which kind of helped Mozilla decide to go for it I guess...)

Like this post? Digg it or Del.icio.us it.

Posted by Fuzzyman on 2007-08-14 22:55:32 | |

Categories: , ,


Python Bits

emoticon:scanner A few bits of interesting Python news.

A 'proof of concept' Python to native x86 compiler, called Compyler has been released and is available for download. It is Windows only (not by intention), not being developed any more, and due to inefficient refcount system runs quite slowly.

However, it will compile PyStone and can generate '.pyd' Python extension files. Perhaps more importantly it at last provides an answer to the incessant questions on comp.lang.python as to whether Python can be compiled to native binaries. Wink

The new Python documentation system based on ReStructured Text is nearly ready to launch. Expect an announcement soon.

Work on Python 2.6 continues apace. As promised this version will no longer support Windows 98. This is a shame as I know of several schools running Windows 98 as it is a very cheap OS. However, Windows 2.4 / 2.5 will continue to work and there is no-one able or willing to maintain the Windows 98 support.

Like this post? Digg it or Del.icio.us it.

Posted by Fuzzyman on 2007-08-14 22:54:37 | |

Categories:


Silverlight IronPython Example: Experimental 'IDE'

emoticon:eggs I've put together a simple IronPython Silverlight project. It's an experimental mini-IDE that has a Python editor window and lets you execute arbitrary Python code in the browser.

Silverlight IronPython IDE

You can also download the project: IronPython Silverlight IDE Project.

Note

If you have multiple tabs open or multiple extensions installed this can be unstable with Firefox I'm afraid. Silverlight 1.1 is still in alpha, so don't be too harsh. Smile

If you serve this project from Apache then don't forget to set it to serve Python files as source rather than running them. This will save you some of the headbanging that I just went through!

The project is pretty much a barebones IronPython Silverlight project which should get you started if you want to experiment. It uses the EditArea syntax highlighting code editor.

  • It requires Silverlight 1.1 Alpha Refresh
  • Pressing the 'Execute' button, or focussing outside the code editor after a code change will cause it to be executed
  • print statements go to the debugging pane
  • There is a DOM element with the id 'experimental'. You can manipulate this from IronPython code (see the example code that the code editor has on startup)
  • You can import from the CoreCLR but I haven't made the Python standard library available
  • Exceptions (stacktraces) should be reported correctly (but are sent to the debugging pane)
  • It's only 90 lines of Python, so it is very simple!
  • AFAIK Firefox only works at all with Silverlight if the files are served rather than fetched from the filesystem

I haven't explained all the details yet, nor provided the source code to the little bit of C# that lets the javascript call the Python code. This will all be posted after my PyCon and Mix UK presentations.

Comments, bug reports and code contributions welcomed. Smile

Like this post? Digg it or Del.icio.us it.

Posted by Fuzzyman on 2007-08-12 19:23:46 | |

Categories: , , ,


Accessing the Web with IronPython

emoticon:python A while ago Andrzej blogged on How to download a web page using IronPython. It is easy enough (using System.Net.WebClient) but many people were unimpressed with the fact that it seemed to take several lines of code more than using urllib2 and CPython.

Well, it turns out that for the simple cases there is an easier method:

from System.Net import WebClient
content = WebClient().DownloadString("http://google.com")
print content

I've updated Download a web page in the IronPython Cookbook.

I've also added a new page (from an example posted to the IronPython mailing list by Dino Viehland): Using Python Functions from C#. It shows how to create and use Python function objects from C#, using the IronPython 2 hosting API.

Like this post? Digg it or Del.icio.us it.

Posted by Fuzzyman on 2007-08-12 15:39:21 | |

Categories: ,


Response(s) to Mac First Impressions

emoticon:target Wow. I had a lot of comments and emails to my First Impressions of the Apple Mac. Thanks to all those who responded, I got a lot of useful advice.

The first thing learned is that Mac users are keen to welcome new members into the community. Here are the specific answers to my questions:

  • Several people recommended Adium as a chat client for Mac OS X.
  • The missing '#' key seems to be a UK localization issue and not a general Mac thing.
  • Escape does cancel some dialogs, but Command-C or Command-+ works for others.
  • The Mac equivalent of the Windows task manager is Activity Monitor, found in Applications/Utilities.
  • No anti-virus software needed, although I do have it installed on my Parallels Vista image. Smile
  • There is a context menu 'create new file' utility, called Nufile.

As for the maximize vs. zoom issue. This page explains the Mac philosophy. I personally agree with Jeff Attwood and prefer to work with a single maximized window rather than manage multiple windows on the same screen. Not a big issue though.

Several of you recommended Macports over Fink for package management as it is generally more up to date. A couple of you recommended neither! I'm going to give Macports a try. The first step in getting it working is downloading the latest version of XCode Tools - a 1 gigabyte download!

Several of you recommended Quicksilver with great fervour. I already have this (but omitted to mention it), and also a Windoze 'equivalent' called Launchy. I don't use either a great deal, I don't think I 'get' what is so revolutionary with them yet but I will persist a while.

I will also need a mail client for the Mac. I guess I will go for Thunderbird. Smile

Like this post? Digg it or Del.icio.us it.

Posted by Fuzzyman on 2007-08-12 15:37:55 | |

Categories:


2 ** 5 + 1

emoticon:computer Today is my 2 ** 5 + 1 birthday. Hooray for me. Smile

Actually I've already reached the age where the perennial reminder of inexorably impending obsolescence is no longer something to make a fuss of, but oh well.

Just so that this blog entry is worth reading worth reading for my techie audience, I assume you have seen this:

Where did that come from? I haven't been following this closely, but all of a sudden it turns out that SCO never owned the rights to UNIX that they claimed. As a result their whole claim is proved hogwash and they owe Novell a lot of money for blackmailing all those companies to buy Linux licenses that they never had a right to sell! Very Happy

Another commentator points out that if UNIX is prior art for Linux, then Microsoft's claims that Linux violates unspecified patents are almost certainly invalid - as are those patents in the first place. No wonder Microsoft isn't telling us which patents are involved...

For another tale of a big company being exposed treating little companies badly, read a tale from Australian Publishing: Bookstore chains puts the screws on small publishers. No happy ending yet but, as you will see, maybe it's not needed.

Now I have a tax return to do. As I haven't paid National Insurance on my income from advertising on Voidspace and selling software I have to ensure I made a loss! Surprised

Like this post? Digg it or Del.icio.us it.

Posted by Fuzzyman on 2007-08-12 15:36:27 | |

Categories: ,


Hosted by Webfaction

Counter...