Archive for November, 2009

Usefulness of itertools.cycle & re.sub

(… or at least the concept). I wanted to process a piece of plain text which would include conventional double-quote marks in such a way that they became HTML smart quote characters (&ldquot; &rdquot;). I was prepared to adopt a naive algorithm which assumed that alternate quotes would always match up, something which obviously wouldn’t work for single quotes. I toyed with various ways of splitting the text up and joining it back together until I came across the slick combination of itertools.cycle and re.sub:

import itertools
import re

quotes = itertools.cycle (['&ldquot;', '&rdquot;'])
def sub (match):
  return quotes.next ()

text = 'The "quick" brown "fox" jumps over the "lazy" dog.'
print re.sub ('"', sub, text)

Obviously my itertools.cycle could trivially be written as: while 1: yield '..'; yield '...', but why reinvent the wheel?

Update: Tom Lynn points out that this can be done with a straightforward regex:

text = re.sub(r’”([^”]*)”‘, r’&ldquot;\1&rdquot;’, text)

Poppy Day again…

It was quite a while before I realised that using Poppies to commemorate Remembrance Sunday (and the fallen of wars in general) wasn’t universal. I don’t know if it’s practised anywhere outside the UK, in fact. But here it’s pretty much the only one of the many collections-for-good-causes which has widespread appeal, partly because it’s a tradition which long precedes pretty much every other save-the-something collection. Partly, I think, because we’re all human and who can argue with collecting money for such a cause?

As it happens I haven’t seen many poppy-sellers around where I work in Camden Town. But I knew that today — Saturday before Remembrance Sunday — the local cadets would be out in force (and in fatigues) around Ealing where I live, armed with Poppies and collecting tins. And they didn’t disappoint. I am, for several reasons, not a great fan of cadets and school OTC outfits. But, at least where I live, this is pretty much the only example of seeing groups of young people on the street doing something for the good of others. The Scout movement has reinvented itself as a training scheme, as far as I can see; and in any case, it’s been years since anyone would have been seen dead on the street in a Scout uniform.

So I bought my poppy from a young man in army outfit who looked a little lonely at the side of the main Shopping Centre square. And I hope you’ll all do the same.

London Python Dojo: that was interesting…

Thursday night saw the third London Python Dojo, which involved us completing a — very simple — noughts & crosses game, with a random computer player which seemed have a winning streak a lot of the time!

Why interesting? Because, for whatever combination of reasons, there were fewer people there: about 10 of us, compared to something around 25 the last couple of times. One obvious factor was that it was November the 5th, so many people would have been with their families at fireworks displays. It might be that the attraction had palled, but I doubt if that accounts for everyone. However it came about, we had a reduced number.

That meant two things, I think: that everyone had a go at piloting (or at least co-piloting; I don’t thing Peter actually coded at all); and that everyone was engaged to a greater extent, just because there were fewer people. (Possibly also because those who did come were slightly keener…). I had a great time, and the audience participation, which was certainly there as I made a mull of trying to get clever with an itertools.cycle, was more — how shall I put it? — manageable. It also meant that there were fewer people still at the pub, and we had a quiet corner to ourselves, which I find more pleasant than shouting at someone in an awkward corner of a noisy and crowded room. (But then, I’m not really a pub person).

Apart from small bits and pieces, most of the time around the pub table was spent debating a proposal of Jon’s that Security bugs are always logically more important than any other class of bugs, because whatever effect another bug can be caused can just as well be caused by someone breaking into your system, making that happen, and then doing whatever they were going to do anyway (sending your credit card details to the Russian mafia, etc…).

As Nicholas has already posted, we discussed various ideas around the future of this particular meetup. We definitely all agreed that it should *have* a future, and more or less coalesced around the idea of a regular, but varying, lineup which might include Dojos of different types, but would also have more conventional talks / lightning talks etc. The idea also would be to keep it in the first week of the month, thus alternating with the Pyssup which is in the third week, but varying there’s always some day *someone* can never make. (For me, it’s Wednesday). It was also agreed that “official” communications would happen on the Python-UK list as everyone can easily see that; not everyone has Twitter / Wave / the-latest-and-greatest-thing.

Personally, I think Fry-IT are very generous in not only allowing us to use their offices, but also providing beer & pizzas. It makes it much easier to talk to people before, after (and even during). If you’re in or around London in the first week of December, why not turn up? Watch the python-uk mailing list for info.