Python Programming, news on the Voidspace Python Projects and all things techie.
IronPython & Windows Forms, Part II
This article has moved.
You can find the whole tutorial series at IronPython & Windows Forms.
IronPython & Windows Forms, Part I
This article has moved.
You can find the whole tutorial series at IronPython & Windows Forms.
The Dreaded Lambda
Ok, the argument is over and Guido has decided to keep lambda in Python 3k.
Until I started working at Resolver I hardly ever used lambda, and agreed that in general they were less readable than named functions.
At Resolver we use lambdas fairly regularly. In several situations I now think they are preferable to the alternatives.
Example usages include :
- For sorting lists of tuples using the second index of each tuple to sort. This uses the new key keyword to the sort method of lists .old_list = [(3,1), (2, 2), (1, 3)]
new_list = sorted(old_list, key=lambda x: x)
- We have various operations that need to perform similar operations on data. We do this by passing in simple functions.new_data = process(data_Set, lambda x: x+1)
new_data2 = process(data_Set, lambda x: x-1)
- This last example is the most convincing. The easiest way of creating a read only property on a class that can be modified from within the class, is as follows :class SomeClass(object):
def __init__(self, value):
self.__value = value
def update(self, value):
self.__value = value
value = property(lambda self: self.__value)
Defining a property with a getter and no setter makes it read only. Creating a method for the getter is more verbose, and less readable.
|||It's really nice that IronPython is a 2.4 implementation. That means we don't even have to think about supporting older versions of Python and can use all the 2.4 language features.|
Towards a Faster Python
They're already making suggestions on Python-Dev that only result in moderate slowdowns in most cases.
This leads me neatly into an alternative way of speeding up Python that I have been thinking about. It was triggered by reading Dan Sugalski  blogging about a fast virtual machine called Tornado that he is working on.
Python is slow because of it's dynamic late binding and the need for type checking and method lookups on most operations .
For some intensive operations (like number crunching) you want more speed than Python can offer. Currently the way round this is to write C extensions for your bottlenecks. This means yucky things like having to do your own memory management.
Wouldn't it be nice to plug a nice fast virtual machine into Python (probably as an extension), where you would sacrifice some of the dynamic niceness of Python for speed enhancements, but not have to do memory management ?
The big downside to this is that for your fast bits, you are no longer writing Python.
So here's an idea. Firstly a couple of caveats.
- I'm several parsecs away from being a computer science expert, so I'm posting this idea partly so that you can pick holes in it and tell me why it's not a good idea.
- Secondly some of what I'm suggesting is addressed by RPython from the PyPy Project. At the end of this idea I will try to remember to summarize (based on my hazy understanding) how what I'm suggesting differs from RPython.
How about a virtual machine, that is statically typed and with simple data-types, where the uncompiled language is a subset of Python ? I say uncompiled, because presumably it would have a compile phase generating an intermediate language (byte-code).
With the right constraints on the language it should be possible to make it pretty damn fast. For example you would lose the magic methods, in fact you would probably lose objects altogether, and lose most of your built-in methods for your basic datatypes.
The goal is to have a Python subset, that is entirely valid Python, but that can run very fast. You would have to cast data-types on calls from (C)Python to VM and back again, which would be slowish, but that is pretty much the case when using the C-API anyway.
This means that you would have a procedural language using Python syntax. It wouldn't have a lot of the benefits of Python, but would be highly optimised for running intensive algorithms quickly. Because it is a subset of Python, the code will run with the normal Python interpreter for testing. (or even without the virtual machine at all on platforms without the VM.)
You can get static typing from type inferencing. I believe that OCaml is a statically typed language which does its typing based on type inferencing. You don't need to declare the type of variables; the compiler infers them. Brett Cannon has also written a Type Inferencer for Python. His conclusion was, that it doesn't provide any speed benefit because of the extreme dynamic nature of Python.
Specifically, he also said that the ability of a module (namespace) to change the type of names in another module makes it virtually impossible to know anything about the types of objects until runtime.
Remembering that in our 'Python subset' we can impose whatever constraints we choose, I would suggest that the compiler could rewrite imports as preprocessor statements and run code in a single namespace. If enclosed namespaces can be implemented without killing type inferencing, then great; but it may not be possible.
Anyway, is there any reason why this shouldn't be possible or desirable ?
Having finally played with grammars and parsers at work, I'd quite like to write a compiler . It even occurs to me that .NET might be a decent target VM. Even if you wanted to implement your own VM, with no I/O or OS layers needed it could be fairly minimal (relatively speaking).
Desirable or not, I don't think I have time for this at the moment. sigh
So how is this different to RPython ? Hmmm... now I think about it, maybe not so different. RPython can compile to C, but maybe there will be other ways of using it too. I expect that RPython also doesn't go as far as I've suggested, for example I don't think it's statically typed.
Another nice idea for the heap of them I have in a corner somewhere.
If there is a reasonable reference "small language compiler" implemented for .NET then a colleague and I may try implementing something like this for IronPython.
Right, anyone know a fast, lightweight and cross platform virtual machine suitable for language hosting  ?
|||Dan is the ex lead developer of Parrot, the virtual machine for Perl 6.|
|||I think that's a fair, if concise, explanation.|
|||In fact I've wanted to for a long time, but now at least I have some understanding of parsers and language grammars.|
|||LLVM looks interesting, but probably not suitable. I had heard that PyPy were working on an 'LLVM backend', but I thought LLVM was a type of grammar !|
Windows Annoyance #14736
So you're using a Windows PC with a network drive mapped to drive letter H, which is pretty normal.
You plugin a USB card reader, which has several slots for reading different types of memory cards.
Windows automatically maps each card slot to a different drive letter, starting at the first free letter. If you have a single hard drive and a single CD/DVD device, then the first free drive letter will be E.
If your card reader has several slots, then one of the slots will be assigned the letter H and thus be unreachable because it conflicts with the network drive. sigh
Why doesn't Winblows just assign a different letter ?
Luckily I can remember how to fix this one. Go to the control panel and open up the Computer Management interface.
Then do :
Storage -> Disk Management Find the drive in question. Right Click -> Change Drive Letter & Paths -> Change...
Then you can choose a new drive letter... sigh
We are starting to extend the plugin protocol to hook into RSS generation. This will allow us to create all sorts of interesting things like a podcast manager plugin and a tagging plugin.
Rogue Trooper - the Game
I've got several projects overdue for updates and lots to do. I have a few free hours, so what's the obvious course of action ? Yep, you got it, time to look for a distraction.
Rogue Trooper is the blue skinned genetic infantry man from Nu-Earth. Betrayed by the Southers who created him in a laboratory, he goes on a mission to uncover the truth and avenge his fallen comrades.
For more background on Rogue, you could do worse than check out :
Having been a fan of Rogue trooper since his days in 2000AD, my first concern was how badly they had mangled the Rogue storyline and atmosphere in making the game.
Before I tell you about the game, I'll let you in on a secret. I don't play many computer games. Since my college days I have had a great fondness for descendants of the game Doom game, so the only games I have time for are first person shooter style games. Rogue Trooper is a third person shooter (which I'd never heard of), more about that later.
To see the screenshots I've taken of the game in action (including screenshots of a couple of the cinematic sequences), see :
My first impression was simply to be blown away. More seasoned game players may be used to cinematic intro sequences, but I'm not. The game opens with the start of the story as a computer generated scene. It's fantastic quality, with awesome graphics and sets just the right mood.
These scenes continue through the game as you progress, gradually telling more of the story. You don't need to know anything about the history of Rogue trooper to understand what is going on. I'm not enough of a geek to tell you how true to the original storyline it is, but it's close enough. It keeps the dark apocalyptic tone, with the lone warrior pitted against the corporate monster. Classic cyberpunk, complete with environmental devastation and futuristic weaponry.
The game is a third person shooter, rather than a first person. What this means in practise is that for most of the action your view hovers just outside the body of rogue. Occasionally this is annoying (with the view pointing not quite where you'd like it), but most of the time it works very well.
The game starts you off with a nice big gun; so you have none of the frustration of having to play for half an hour before being able to do real damage, which I've had with other games. It takes you through the gameplay teaching you how to use the weapons and special moves. I found this very useful, but if it irritates you you can switch it off.
After shooting a few bad dudes I was starting to wonder about the performance of the game. Its reaction time was a bit sluggish and it was starting to annoy me. I was doing ok with only one or two enemies at a time, but I knew with more baddies rushing into me I was going to struggle. Suddenly I twigged.
My computer is hardly the hottest games machine around. It's an AMD XP 3000+ (1gig of memory) with a 128mb Nvidia graphics card . Not only that, but I was running some processes in the background. I turned the screen resolution down to 800x600 and the level of detail to medium. Not only could I barely tell the difference in quality, but the game performance improved dramatically.
I've now been playing the game for a couple of hours. I've gone through a couple of levels and unlocked several cinematic sequences. I've also got to the point where I now have all of Rogue's buddies installed as bio-chips in my weaponry. I'm still learning about the game though, and am really starting to enjoy the shoot-em mayhem.
The game has lots of nice touches. Rather than randomly picking up discarded ammunition (like most fps games) you recover salvage and manufacture replacements, with a bit of help from an electronic friend. This means you are responsible for ensuring your ammo box is full and have more flexibility over which weaponry to use. As you progress through the game you have more and more weapons to use (hey, just like Doom I like the shotgun). You also get to use heavier fixed weaponry and mines at various different stages through the game.
There is an array of special moves to use, but I still can't get the hang of sticking to a wall and shooting. Oh well.
In the early parts of the game you spend some of the time working as a team with the other troopers, but it's not long before you're on your own. Each level has several ways through, so even on completing a level you get the feeling it's worth going back to see what you missed.
On the negative side, the game is only saved at checkpoints. At a couple of difficult points it was very annoying to have to go over and over the same points just to beat the bit I was stuck on. Luckily checkpoints are fairly frequent.
Anyway, the story develops well, but I can't tell you how it ends yet. I'm really enjoying the game, which is well written with no major bloopers in gameplay. As I may have mentioned before, the cinema scenes are fantastic. The game has enough blast-em-up fury to be fun, but encourages enough tactics and thinking to hold (my) interest. Highly recommended.
|||Don't ask me which one.|
This is version 4.3.2alpha, and has a couple of bugfixes and minor enhancements. These are mainly related to the new unrepr mode.
- Fixed bug where '\n' terminated files could have the last '\n' removed when writing.
- Bugfix in unrepr mode, it couldn't handle '#' in strings. (Thanks to Philippe Normand for the report.)
- As a consequence of this fix, ConfigObj doesn't now keep inline comments in unrepr mode. This is because the parser in the compiler package loses the comments.
- Error messages are now more useful. They tell you the number of parsing errors and the line number of the first error.
- Line numbers in exceptions now start at 1, not 0.
- Errors in unrepr mode are now handled the same way as in the normal mode. The errors stored will be an UnreprError.
There has been some interesting ConfigObj activity recently :
ConfigObj is like a human-readable simple pickle...
Elisa is a new open media center system to watch, listen, manage and share video, file and music. The box is working under Linux (and later under windows). It uses Gstreamer, Python and OpenGL.
The BioCede Complete Windows Development Environment recommends ConfigObj.
This is a rough draft of a doc about what tools I have selected for a 'complete' development environment using Python on the Windows platform.
urllib2 Tutorial Updated
My tutorial on using urllib2 to fetch webpages is a very popular page. In April it had 1287 page views.
As a result it has been edited by John J Lee and greatly improved.
|||It is in Subversion, but the HOWTOs haven't been integrated into the build process yet.|
Object Oriented Programming - in French
Did you know that the French acronym for Object Oriented Programming is POO ?
Nor did I until Gerard Labadie translated my Object Oriented Programming with Python into French :
If you can read French (which I can't), then enjoy.
Web 2.0 All Over the Place
New Web 2.0 websites and services are springing up all over the place. I've looked at a few to see which are really useful to me.
I already use Gmail for archiving  emails and useful information, I also use Del.icio.us for keeping my bookmarks. When using computers from multiple locations, the online model is definitely useful (and becoming usable).
For reading blogs I use Planet, which although it is an online aggregator, runs on my own server (without AJAX !) so I don't think it counts.
I've also recently been looking at online backup. Online storage seems to be a good solution to the offsite backup problem, but until recently has been too expensive for me.
First of all is Jungle Disk. Their website provides an interesting metric showing (allegedly) that the Amazon S3 service is miles cheaper than the competition. I've heard other people say good things about it as well.
Jungle Disk runs a local WebDAV server, which lets you access the S3 storage as a remote hard drive. Jungle Drive seems heavily beta. Although it works from work, I can't access it at home. When it works it caches data locally, and uploads in the background .
However, if I use it in conjunction with NetDrive it works fine. NetDrive maps the local WebDAV server to a drive letter, so I can access it normally.
Oh, and by the way; I recently posted an entry to my other blog, which readers of this one may find interesting :
It's about the future of computers, particularly online technology and mobile devices.
|||And because it seems to have better searching capabilities than even desktop email clients.|
|||Currently down and intermittently slow, Pingoat looks like a good alternative.|
|||Does this count as Web 2.0 ?|
|||I'm currently getting an upload of ten KB per second though, which isn't mindbogglingly impressive.|
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 License.