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

Static Members on Classes

emoticon:cross The .NET framework has static members on classes. These are effectively class attributes that behave like properties.

For example, the DateTime class (actually a .NET structure) has a static member Now that returns the current time. Not only is this very useful, but it makes code that uses it very clean.

>>> from System import DateTime
>>> DateTime.Now
<System.DateTime object at 0x... [12/10/2007 10:24:49]>

I was thinking about how you could provide the same API from Python. We could implement this behaviour on an instance by using a property. However, when you access a property on the class you just get a reference to the property object.

>>> class Something(object):
...     @property
...     def thing(self):
...         print 'triggered'
>>> Something.thing
<property object at 0x009D6FA8>

So, to do this from Python we need a property on the class of the class - which is a metaclass.

Below is an example that provides a 'current' property on the Python datetime class. (I didn't use now as this is already a class method that does the same thing).

from datetime import datetime

class meta_datetime(type):

    def current(self):

class new_datetime(datetime):

    __metaclass__ = meta_datetime

>>> new_datetime.current
2007-10-12 10:28:12.015000

Nice. Smile

Of course by using a metaclass, you make your own code a bit more obscure [1]. You have to weigh up the balance between keeping your own code readable (by others), and presenting the nicest possible API.


A commentor on my blog (called Frank Benkstein I think), suggests the following improvement using the descriptor protocol:

from datetime import datetime

class current_datetime(object):
    def __get__(self, instance, owner):

class new_datetime(datetime):
    current = current_datetime()

>>> new_datetime.current
new_datetime(2007, 10, 12, 18, 27, 57, 748366)

I think this is more readable than the metaclass solution. Smile

[1]It might also be hard to test as the class has a metaclass which uses the class... (In this example we could just use, but then new_datetime.current wouldn't return an instance of new_datetime).

Like this post? Digg it or it.

Posted by Fuzzyman on 2007-10-12 10:49:32 | |

Categories: , ,

Resolver Open Source Project: CPython Extensions from IronPython

emoticon:drive Yesterday we had a London Python meetup at Thoughtworks (free beer and Pizza - thanks guys), organised by Simon Brunning. It was nice to catch up with people again, including Remi Delon who I haven't seen for way too long. He promises exciting new changes at Webfaction (extra processes and memory on all hosting accounts, new website etc) as soon as his business partner gets back from holiday and they can roll it out!

More exciting was the talk that my boss Giles gave about Resolver Systems. We've found interest in Resolver has come from an unexpectedly diverse range of industries, some from Financial Services (including our current customers), but also interest from genetics companies, and even people doing oil exploration and games development! A lot of them are using Python for working with data, and feel the pain of Excel a great deal. For them, Resolver seems like a ready made solution, but it means that Resolver playing well with CPython is becoming important far more quickly than we expected.

Giles has announced that Resolver will begin work on an Open Source project to get some CPython extensions working seamlessly with IronPython. We may well start with some part of the Scipy Suite. It will be a .NET integration layer allowing you to use the existing Windows CPython extensions (so it is not a port to C#), and Python code that uses these extensions will run unchanged on CPython and IronPython.

The hope is that after doing the work to get a couple of extensions working, we will basically have created a general solution, but we're not promising that yet. Smile

We think that as well as providing business value to us, this is a great way for Resolver to contribute back to the Python community. We don't have a timescale, but it is important to us. And yes, we do want your help. Very Happy

We know that it is a big job, but we also know that it isn't impossible. Seo has already done some work (basic proof of concept) on a 'ctypes-reflector' that can call into CPython extensions from .NET. This is also essentially a 'solved problem', but from the other way round, with Python.NET.

Like this post? Digg it or it.

Posted by Fuzzyman on 2007-10-11 11:45:52 | |

Categories: , , ,

iPhone Irony

emoticon:eyeballz Well, having been very good and decided that I didn't really need an iPhone I went ahead and got a new contract with a Nokia 6300. This replaces a Symbian based N70 which I was never happy with [1] and never used as anything more than a simple phone.

The 6300 is a mobile phone - plain and simple. It is slim with enough weight to feel solid and has a nice clean and bright user interface. For anyone who just wants a phone I can recommend it. My new contract is £12 a month and includes more minutes and texts than I will use - so I was pretty happy with it.

But... oh irony of sweet ironies, someone went and gave me an iPhone! It didn't have the 1.1.1 firmware, so I've been able to activate it, but haven't yet unlocked it. I agree in principle with everything Mark Pilgrim said about the iPhone, but...

This thing is frikkin awesome. It simply doesn't compare to any mobile device I've ever used. I can totally understand why people are prepared to invest hours hacking them. It really is game changing for phones and PDAs. The UI is very different to all the other platforms I've used, with countless little touches in that make it fun and easy to use.

I've only played with it over wifi, and beyond what I needed to activate I haven't installed anything, but the things I'm already impressed with include:

  • The YouTube integration is fantastic
  • The gmail client is very nice
  • The google maps integration is impressive
  • Google reader on safari works fine
  • Google calendar via safari also works fine
  • The way you zoom in and out to view parts of a web page in safari is genius
  • Flicking through albums in the iPod is just plain sweet

Unfortunately my music collection is now ten gb and the iPhone only holds eight, but it's not like I need that much at any one time... (The camera is a bit above average for a phone camera - i.e. not brilliant, but you have a bigger screen to view it on.)

I don't have a data deal on my new contract, but I do have a data-only SIM that I use with a 3G modem when I commute. This SIM supports GPRS and EDGE so I'm hoping it will work with the iPhone. This means that I will still carry two phones (both slender beauties), but can ditch the iPod which is a step in the right direction. My main use for a PDA was as a calendar and a book reader. I stopped using a PDA because getting Windows Mobile based PDAs to sync with google calendar was difficult / impossible. The iPhone solves that problem, but I wonder if I will be able to use it as an ebook reader?

By the way, the tutorial followed to activate (and hopefully soon unlock) was: How to Unlock your iPhone.


Now unlocked and working fine with my data-only T-mobile SIM. My Three network SIM doesn't work with the iPhone though.

To get data across the phone network I needed to set the correct APN for T-Mobile UK in the network settings.

Oh, and because the touch screen works by electrical conductivity you can't use your fingernails for more accurate touching.

[1]Slow and clunky user interface and without a touch screen or keyboard it is painful to use for anything other than making calls or sending texts. In fact the extra complexity makes it worse to use for these things than simpler Nokia phones (where normally I like their user interface). To make it worse, if the camera cover slides down in your pocket (and it does) then the keypad becomes unlocked in your pocket and will make random calls / go on the internet / generally screw you over.

Like this post? Digg it or it.

Posted by Fuzzyman on 2007-10-11 11:15:15 | |

Categories: ,

IronPython and Resolver at TechEd Barcelona

emoticon:black_hat November 5 - 9th in Barcelona is (yet another) Microsoft developer event: TechEd Europe.

I'll be demonstrating Resolver as a powerful application written with IronPython. Lucky the IronPython team are part of the Microsoft development team and not the Office team. Wink

I'll be speaking along with Mahesh Prakriya and Martin Maly, both initimately involved with IronPython. Our session is:

WEB305 - "IronPython" and Dynamic Languages on .NET
Mahesh Prakriya , Martin Maly , Michael Foord
"IronPython" is the codename for a new implementation of the Python programming language on the .NET Framework. IronPython is fast - in fact, up to 1.8 times faster than Python - 2.4 on the standard pystone benchmark. It supports an interactive interpreter with fully dynamic compilation as well as static compilation to produce pre-compiled executables. It's well integrated with the rest of the framework and makes all .NET libraries easily available to Python programmers. This session shows how IronPython brings the power of .NET to Python and the power of Python to .NET. At OSCON 2004, the first public release of IronPython was announced. This session demonstrates the latest IronPython version in a range of situations from using GUI frameworks to driving Microsoft Office applications to working with a variety of external libraries. We also discuss other scripting languages on .NET.

IronPython also features in the following sessions:

WEB307 - Developing Data Driven Applications Using the New Dynamic Data Controls in ASP.NET
Shanku Niyogi
ASP.NET dynamic data controls are part of a powerful, rich new framework that lets you create data driven ASP.NET applications very easily. ASP.NET dynamic data controls do this by automatically discovering the schema at runtime, deriving behavior from the database and finally creating an ASP.NET page. Anything that can be inferred from the schema works with almost no user effort. If needed, the page can then be further customized either by using static languages such as Microsoft Visual C# or Visual Basic .NET or dynamic languages such as IronPython, Visual Basic, Jscript, etc. In this talk, we show you how to build rich, database driven Web applications from scratch, such as TaskList along with other demos.
WEB315 - Silverlight, ASP.NET and Web Services in IronPython & IronRuby
Mahesh Prakriya
After attending this talk you will learn how to use dynamic languages (such as IronPython, Jscript, VBx and IronRuby) in Silverlight and The overall dynamic languages initiative and Dynamic Language Runtime (DLR) will also be covered. and Silverlight are two great example hosts of DLR and the languages listed above are implemented on DLR. We will first demo Python and Ruby in Silverlight, then Python in and will end with webservices demos.
WEB08-IS - Building Languages With The Dynamic Language Runtime
Martin Maly
Dynamic languages such as Python, JavaScript, and Ruby are popular and powerful. After all, they pretty much run the web. The Dynamic Language Runtime (DLR) is a new software platform for building dynamic languages. Since it is built on top of .NET, it gives you access to a wide variety of libraries and tools. In this talk, you will experience the magic of building a dynamic language using the DLR. Starting with a simple parser; we will build the key pieces of the compiler and plug it into the DLR. If you are interested in compiler technology or dynamic languages, this talk is definitely for you.

Now my guess is that not many of my regular readers will be at TechEd, but if you'll be there then let me know and we'll grab a beer. There are lots of other sessions of course, and quite a few of them look interesting. I'll hopefully be attending sessions on functional and concurrent programming with .NET, working with database APIs (OLE DB, ODBC and Entity), introduction to the reflection API and a few more. Smile

Like this post? Digg it or it.

Posted by Fuzzyman on 2007-10-09 16:55:24 | |

Categories: , ,

Testing in IronPython in Action

emoticon:python I've nearly finished writing the chapter on testing in IronPython in Action (just the summary to go) and I'm pretty pleased with it.

In this chapter I managed to cover:

  • Some discussion of what unit testing is and why test
  • Setting up a test framework for an application using unittest, and writing tests with it
  • Mock objects
  • Monkey patching
  • A useful test pattern involving a Listener class
  • Functional testing a GUI application (including writing a User Story)
  • For the functional testing we need to use .NET threads for synchronously and asynchronously interacting with the GUI thread

I particularly like the functional test section, as it makes the example application 'dance'. Smile

In a few weeks this chapter (along with the next three chapters) goes for its first review. After that I will probably make all the source code available for download, so that even if you don't by the book (shock and horror) you still get to play with the examples.

Like this post? Digg it or it.

Posted by Fuzzyman on 2007-10-08 14:25:52 | |

Categories: ,

New Movable Python Demo, plus Other Randomations

emoticon:globepage I've had quite a few visitors to the Movable Python pages from this link:

The Movable Python demo version was out of date, so I've posted an updated one. The new free trial is good until May 2008.

A couple of months or so back I promised that 40% of all purchases would be split between the PSF and the OLPC project. Plus, once sales reached £500 Movable Python would be Open Sourced.

It's well over half the way towards that goal (around 65% actually) - so if you want to see Movable Python Open Source, you know what to do. Smile

Oh, interesting link. Sam Ruby on why he thinks Python (and Perl!) has competent language designers (it is possible to specify source code encoding).

Interesting link number two. The OpenMoko project is an open cell phone platform. It is a great idea, and not too shabby an implementation either, but the specs of the first version were a little-too-raw for my tastes. Some guy with a great domain name, has posted the specs of the first version side by side with the next version (at one point slated for availability this month): The Open Source iPhone. The specs look very tasty, and there is a promise of basic functionality in the software. If it actually works as a phone (which isn't promised for Neo, the currently phone), then I think I will go for it. Mono works on OpenMoko, so both Python and IronPython should run (and through GTK# with IronPython it might be easier to create user interfaces).

Interesting link number three. A post by why on My Complete List Of Substitute Names For The Maneuver We Now Know To Be Monkeypatching. Could this really be a Ruby programmer acknowledging that rampant patching is not perhaps always the best of practises. Wink

Interesting link number four. Developer versions of The Chumby are now available to order. Unfortunately only for shipping in the US, so I have an invite (they cost $180!) if anyone wants one. The Chumby is a soft toy with a programmable computer type widgety display thing and wifi built in. Widgets are written in Flash, but it runs Python, so it could be fun.

Like this post? Digg it or it.

Posted by Fuzzyman on 2007-10-08 13:34:28 | |

Categories: , , ,

Hosted by Webfaction