Python Programming, news on the Voidspace Python Projects and all things techie.
Static Members on Classes
The .NET framework has static members on classes. These are effectively class attributes that behave like properties.
<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.
... def thing(self):
... print 'triggered'
<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).
__metaclass__ = meta_datetime
Of course by using a metaclass, you make your own code a bit more obscure . 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:
def __get__(self, instance, owner):
current = current_datetime()
new_datetime(2007, 10, 12, 18, 27, 57, 748366)
I think this is more readable than the metaclass solution.
|||It might also be hard to test as the class has a metaclass which uses the class... (In this example we could just use datetime.now(), but then new_datetime.current wouldn't return an instance of new_datetime).|
Resolver Open Source Project: CPython Extensions from IronPython
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.
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.
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.
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  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.
|||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.|
IronPython and Resolver at TechEd Barcelona
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.
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 .NETMahesh 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:
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.
Testing in IronPython in Action
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'.
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.
New Movable Python Demo, plus Other Randomations
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.
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.
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.
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 License.