An eye-opener

I’ve long been a fan of Python and similar technical solutions which sit easily within an agile toolchain. I’ve always been in favour of Open Source solutions (not least because they’re generally gratis!). But my professional work is and always has been almost exclusively on Windows platforms. My laptop dual-boots WinXP/Kubuntu, but it’s the WinXP which gets most of the airplay. That’s basically because I’m pragmatic: I’m far more productive under Windows than I am under Linux, and part of the reason for that is Python. Having spent some 5 years earning my living as a C/C++ programmer, I haven’t written a serious line of either since I picked up Python some seven years ago.

The upshot of all this is that I tend to be something of a champion of Python running under Windows. I try to pick up Win-specific questions on the Python, Python-tutor and Python-win32 mailing lists. And I try to produce helpful web pages, modules and examples to show that you can get things done using Python under Win32, in spite of the *nix-oriented swerve of many of its developers and advocates.

But in spite of being a Python advocate, I’ve never really felt that strongly about the various Microsoft technologies and languages which people sometimes grumble about on mailing lists and blogs. Until the other day, when I attended a course concerning differences between SQL Server 2000 & 2005 (which I use professionally). I felt like I was wading through treacle. It was a real eye-opener for me, so used to the freedom which Python gives me.

There were two things which hit me the most: poorly-designed syntax; and the Visual Studio environment and languages. The SQL Server development team have added a few (not many in fact) new features to the core product, so naturally some new syntax has had to be devised to cope with this. Now I’ve seen many a hot-headed thread grow to a ripe old age on the Python mailing lists concerning proposed syntax changes. But at least people care, and there’s a sense that it *matters* what the language looks like at the end. In SQL Server they seem to have thrown around new and existing keywords, brackets, commas and other bits of punctuation with gay abandon and no consideration for the poor buggers who’ll have to use them later.

Try setting up an HTTP Endpoint around a stored procedure. And now close that session down and do it again without looking at the syntax. Impossible. And don’t get me started on the *ridiculous* Notification Services which require two new databases (yes, databases) and three XML files. (And that’s another thing: the plague of XML…)

But the thing which really made me miss Python was having to write extensions using C# within the Visual Whatever environment. So many lines and so much disk activity for so little result. I have never understood so well why people refer to Python and its neighbours as “agile”. It really seemed like being able to breathe again in comparison to being stifled by technology.

os.remove… or not

There was a post on python-list yesterday which had someone asking whether, under Win32, an os.remove could return (without an exception) without having removed the file. The OP was experiencing an exception in the following circumstances:

import os
os.remove ("blah.txt")
if os.path.isfile ("blah.txt"):
  raise RuntimeError, "blah.txt still exists"

The initial reaction was that a race condition was the most likely bet, and the OP said that it was possible but unlikely. Eventually, Roger Upole pointed out that it is possible for a delete to succeed without the file actually disappearing — if another process has the file open with FILE_SHARE_DELETE. In that situation the DeleteFile call succeeds but the file won’t actually disappear until the process with the share/delete handle closes the file! It turned out that TortoiseSVN’s cache mechanism was sometimes creating such a handle which meant that the check failed.

Amazing what you learn.

Essential Firefox & Thunderbird extensions

I use Firefox & Thunderbird as my main browser and mail client (respectively!) They’re both useful enough out of the box, but loads of people have published even more useful extensions which “enhance the user experience” as the blurb goes. I have a number of other addons installed, but my essentials are:

Thunderbird

  • Nostalgy - forget about the folder window; just a few keystrokes will do everything you need.
  • Correct Identity - I serve a number of email addresses (from completely different domains) through one mailbox. This extension makes a good guess as to which hat I should be wearing to reply to an email, based on the “To” address.

Firefox

  • Sage - no matter what other RSS reader I try, I keep coming back to Sage’s simplicity. It lacks a couple of things I might like, such as the ability to vary the refresh interval by feed, but it does what it does simply and well.
  • IE Tab - indispensable if you’re developing web pages or just trying to view the ones out there.
  • CSS Viewer - not as powerful as Firebug, but very handy for a quick check as to what’s going on with your page’s styling.

sqlalchemy & IronPython — two to watch

Michael Bayer, the indefatigable author of Myghty, sqlalchemy and Mako is moving sqlalchemy forward apace. He’s recently proposed what I see as a sensible simplification of the varied query/select methods which sqlalchemy offers. I’ve been following sqlalchemy for a while. The trouble is that I’m first and foremost a SQL developer and first and foremost a Python developer. Trying to marry the two is something I find difficult. If I were a SQL dev who dabbled in Python, I’d just throw SQL statements around and do things with the results. If I were a Pythonista who needed some data, I’d use sqlalchemy (or SQLObject, or whatever) and be happy. Because I’m both I want the best of both worlds. Maybe sqlalchemy’s about to offer it!

In other news, Michael Foord (aka Fuzzyman) has started a wiki for IronPython recipes. It even got a mention on O’Reilly’s Windows DevCenter. (It’s only a shame they can’t spell Centre correctly ;). I keep meaning to have a look at IronPython, not least because it’s obviously the way Python is going to go on Windows at some point in the future. Better be on the ball before the game starts. (If that’s the metaphor I want).

Local Libraries

I’ve been visiting libraries all my life. I still have tickets to about 10 libraries (or, these days, county library groups) around the UK. Wherever I move to live or to work, one of the first things I do is to find the nearest libraries. The purpose of this post wasn’t really to encourage the continued support of library services by local authorities, nor to bemoan the pitifully small uptake among the youngsters I deal with from week to week at the club I run, nor to enter the ever-controversial debate as to whether libraries should be filling themselves with DVDs, computers, computer games and so on. So I won’t do that.

Instead I wanted to muse briefly on the effect of a small library. Where I grew up, in Merton in Southwest London, our nearest library was known (by us) as “Lower Morden Lane Library” although I discovered years later that its official title was “Morden Park”. It was one of those small places, maybe originally a family house with the ground floor knocked through to create one big space, including a slightly offset space at the back where the children’s books were kept. It was cosy and manageable for a child. We were allowed to cycle there along the back roads and the librarians — mostly women, as it happened — were friendly and familiar.

Later, we “graduated” to the bigger borough libraries at Wimbledon and Morden, although neither of those was huge compared to, say, Croydon Central library. I grew up, moved away and got used to bigger libraries. Not so long ago, in a cost-cutting exercise, Merton closed a number of branch libraries including Merton Park Library. So it goes.

Living now in Ealing, in West London, I’ve frequented the Central Library in the shopping centre for some years. It’s relatively big, built on two levels and the staff are friendly and helpful when asked. For the next few months, they’re refurbishing and the library is temporarily housed in a small space at the base of one of the Ealing Council buildings. They’ve managed to fit in a few computer terminals, a rack of DVDs, a small reference section including periodicals, and a restricted selection of books. And the funny thing is, it feels more friendly, more approachable. The staff are the same, the books are the same (although fewer). Even the book shelves are the same. The place is mildly grotty and (today, at least) too hot.

I think what makes the difference is that, with fewer books, you feel you own the place. By which I mean that you get to know quite quickly what’s on the shelves where, and what’s new. It’s not as though the selection was infinite previously, but in this setup I can (almost) stand in one place and see it all. It’s as though I were a child again, feeling happy and relaxed inside my local branch library, the librarians chatting away about their kids and keeping an eye on things. I only hope more people find it as appealing.

Active Directory problems

Someone contacted me the other day to say that he was trying to use my active_directory module to backend a Trac authentication plugin. His code worked fine in a test harness, but running against the standalone tracd server, it crashed Python.

Now, as I pointed out to him, it takes usually two things to crash Python: an extension module; and threading. We definitely had the first (the win32com module) but not evidently the second. After some toing-and-froing, it became clear that tracd did indeed use a threading mixin and that, if this was removed, the problem went away.

As it stands now (I’m waiting to hear back from the OP) the problem is almost certainly down to the face that the module-level convenience functions work against a module global, which is not therefore thread-safe. That, the programmer can — and should — work around by instantiating his own AD object within the thread. The second thing, though, which I need to rectify in the module itself is that it uses a global ADO connection. This must have seemed like a good idea at the time, but in retrospect is bound to give problems of this sort.

In addition, of course, the programmer will need to do the CoInitialize / CoUninitialize dance to let COM know that we’re threading and that it needs to do its stuff.

WMI Problems

I recently (and about time too!) put together a tutorial for my WMI module. Naturally enough, I thought I’d tested all the examples. Until Alexander Ludogovski pointed out a flaw in the method example. The example I had (now changed) looped over processes with the name “notepad.exe” and terminated each one. At least that was the idea. As Alexander pointed out, however, what it actually did was to kill the first and bomb out on a error with the second. Unsure of what was going on, I replaced it with a different example (also now changed), this one to restart all autostart services which had stopped. That also failed in a similar way.

For now, since the purpose of the exercise was to illustrate the use of WMI methods, I’ve put an example in place which stops one named service. But I need to track down the cause of the other problems which don’t occur, by the way, if you use “raw” WMI. I’m fairly sure I know where to look, but that’s not necessarily going to make it easy.

A little happiness…

Sometimes the world needs a little happiness. There was an article in today’s Metro (throwaway daily paper to be found on the floor of any tube train in London). No link, I’m afraid, but the gist of it was that a young man, wanting to propose to his girlfriend in style, had asked the local cinema (in Rochdale, I seem to remember) if they’d put up on screen some appropriate words of his. They went one better, filmed him saying “I’m madly in love with you etc. etc.” and put it on before Pirates 3 which he’d taken her to see. When she saw this, she turned round to him and saw him on one knee, ring in hand. Rest of cinema on feet, applauding. Sometimes you need a happy ending!

P.S. What made the story stand out for me was not so much the young man’s romantic gesture, but the fact that the cinema was willing not only to agree to his modest request, but to improve on the idea. I hope the staff were all there watching to see their good work pay off.

The Children of Húrin

The guys at MercatorNet have been good enough to send me a copy of the “new” Tolkien book, The Children of Húrin, with a view to my reviewing it for them. I must admit I approached it with some trepidation. While I am a Tolkien fan, having read the LotR literally dozens of times, I’ve never been very good at working out the people and places of the Silmarillion (from which this story comes) nor the different flavours of elf you encounter.

I’ve not finished the book yet but it’s not so bad once you get into it. It does smack very much of the older epics, which is very clearly JRR’s bread-and-butter: I can’t help feeling that LotR was a kind of modern-day epic spin-off for those who couldn’t take the original! What is bewildering is the number of times Túrin (Húrin’s son, the main character) changes his identity; he hardly seems to last three pages before he blunders out of one alliance and into another, each time taking an ironic name like “The One Who is Lost” or “The Hand of the Black Sword” or something.

I should be finished by the end of the today and onto the review. In short, if anyone’s looking for a LotR prequel, they’re going to be mostly disappointed. But it is interesting to read in fuller form the kind of thing which JRR Tolkien loved to do. I’m a fan of the man as much as of his work: I’ve probably read (or at least browsed) The Letters of JRR Tolkien more often than LotR and the attitudes and actions of the people in any of his writing surely reflect something of his character and beliefs when he wrote them down.

OpenId

Assuming I’ve got things right, erstwhile commenters can use their OpenId identities to, well, identify themselves. I always find this a great convenience when I’m visiting other peoples’ sites, so why not extend the courtesy?

UPDATE: I’ve changed the theme and lost the OpenId comments login; I’ll try to get it back as soon as.