Spring in Cambridge

March 26th, 2007

Photo: Walking along the banks of the Charles.

If I ever had to rethink my decision to choose Cambridge to live in, I should think that I would regret living anywhere else. Cambridge is truly a wonderful place, filled with so many interesting things to do and great for the people watchers in all of us. And then, there’s the Charles (an old foe of the British, but we don’t talk about that…), which has finally thawed and gives off a shimmer and glow in the mornings as one strolls along its banks. And I’ve been doing a lot of that lately. At least a couple of times a day, over the past few days, I’ve taken to doing a 5-10km walk, latte in hand (more than 15 shots some days at the moment), with only the most powerfully depressing of power ballads as my companion along the way…yes, I’m even listening to Roxette. Indeed. Yes, I know.

My favorite route at the moment is to head down Mass. Ave from Central, toward Boston (over Harvard Bridge), then make a left and head down toward the Charles MGH T-stop. Then, turn onto Charles St. and wind up at the Public Gardens. A quick walk and you’re on the Common, and then at the T-stop. One can either take a T back to Central or walk back via the Museum of Science and take another bridge, toward Kendall, and then onto Central. Either way, this is a very enjoyable morning/evening stroll. Even if you’re down in the dumps and feeling rotten about your inability to understand the most complex problem there is in this life. As with anything else one wants more than anything else in the world, one can but try :-)

Jon.

HOWTO: Spend a Sunday afternoon

March 25th, 2007

So I decided moping around the apartment listening to super depressing music was only going to be so useful and headed outside for some thinking time between me, myself, my latte and the Charles. $500 of impulsive purchases later and I can report feeling no happier, but I did manage to waste an afternoon. Oh and I discovered my iPod is currently incompatible with Nike+iPod (after buying it). I guess that’s something at least.

Track du jour: The Corrs – What Can I Do?

Jon.

Update: After faffing around (read: converting said iPod to HFS+, screw you, Apple, for not supporting VFAT formatted iPods under OSX – an obvious failing and also obviously intentional – I’m switching it back to VFAT when I get time, via dd/mkfs) and reflashing to firmware 1.3 I now have a working Nike+iPod. I don’t like Nike one bit but I needed cheering up, and so far (aside from problems with the sensor detection in areas of obvious interference) I’m reasonably happy with it.

Turkey, India, who do we pander to next?

March 13th, 2007

I was disturbed to read a story just now that Google will be handing over the IP addresses of people expressing opinions negative to some arbitrary standard held by the Mumbai police. Just like Turkey and their “offending Turkishness”, these folks want to go after people expressing negative views about India on social networking sites.

We live in such a free world.

Jon.

Uncle jcm…I’m now officially old

March 8th, 2007

So my sister finally gave birth last night to my nephew. Oscar Wrigley weighed in at around 8lbs, 8 ounces (if I heard that right over the phone – I’m sure there will be photos, and other updates from the new parents themselves). I’m very happy for them.

Anyway. This makes me officially old now. I can see it now – “crazy uncle Jon, who lives in America” – perhaps with a few interjections about me being a “traitorous dog” from my father. Still, I’ve got a bit of time to work on the “cool uncle” aspect.

Jon.

Playing with Python

March 8th, 2007

So I like nothing more than to spend the early hours of every morning looking at anaconda source[0] in the hopes of learning more about some of the more fun things one could to do with Python, if one felt so inclined. Actually, that’s partly true – I am trawling through the anaconda source at the moment of my own volition – everything from figuring out the pipe communication startup hacks in mini-wm/Anaconda to the isys library. Why? Because I work at a company that has heavy reliance on Python and feel I should better understand the language, and use it more in my own work as a result. Besides, I’m not a perl weenie, I should write fewer shell scripts, and I’m not particularly desperate to go near a Java compiler, having been subjected to that more than quite enough in college.

I actually quite like Python. I’ve written a bunch of scripts using it, but nothing huge and I tend to write very C-like imperative code in “OO” languages – even when I’ve played with pygtk in the past. Still, that’s just fine. One of the things I have been doing is reading up on the more interesting features of the language, best practices, and the like. I’m using the two standard O’Reilly books as a reference and the online p.o documentation. The point of the exercise being that I’m reasonably keen to know how I “should” approach writing Python code, as opposed to my hackish approach in the past. I want to understand the intricacies of classes in Python, pickling, the internal differences between pyc and pyo “optimised” bytecode and some of the more funky APIs – like the HAL/gtk bindings I’m going to be playing with some more.

Anyway. That’s all fairly boring. Not having looked at Python’s more funky features in a while, I wanted to dive right in and write some simple C/API plugin to play with Python Objects. This turns out to be a little harder these days than the (normally excellent, but in this case not so much) python documentation would have you believe. Here’s a simple test “library” I wrote to figure it out:

/*
 * pytest - playing with Python's C API.
 */

#include <python .h>

static PyObject *list_set(PyObject *list, PyObject *item);
void initpytest(void);

static PyMethodDef pytestModuleMethods[] = {
        { "list_set", (PyCFunction) list_set, METH_VARARGS, NULL },
        { NULL, NULL, 0, NULL }
};

void initpytest(void)
{
        PyObject *m;

        m = Py_InitModule("pytest", pytestModuleMethods);

}

static PyObject *list_set(PyObject *self, PyObject *args)
{
        PyObject *list, *value;
        int item;

        if (!PyArg_ParseTuple(args, "OiO", &list, &item, &value))
                return NULL;

        if (!PyList_Check(list))
                goto error;

        if (PyList_SetItem(list, item, value))
                goto error;

        Py_DECREF(value);
        return Py_None;

error:
        Py_XDECREF(value);
        return NULL;

}

You can compile this into a dynamically loadable (read: Python can use it) library on most Linux systems by driving the gcc front end, thus:

gcc -o pytest.so -g -shared -fPIC -Lpython -I/usr/include/python2.4 pytest.c

(this enables debugging, “-g”, and assumes a default 2.4 python installation, such as that on my Fedora Core 6 desktop box that I’m using today).

Once built, such libraries can be used very simply:

[jcm@perihelion pytest]$ python
Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytest
>>> foo=[1,2,3]
>>> print foo
[1, 2, 3]
>>> pytest.list_set(foo,0,2)
>>> print foo
[2, 2, 3]

It’s a simple example, pytest provides a single function that changes values in Python lists. Everything in Python is an object, and there is a need to be aware of reference-counting that’s happening underneath (well, sometimes you need to worry about it). Every C/API extension needs to define an init function, which educates the Python runtime about those functions it provides – in this case, the list_set function. These are defined in a NULL-terminated PyMethodDef array, and usually defined to require METH_VARARGS argument passing (I’m sure this was once different).

Actual functions, such as list_set work in PyObjects (everything’s an object) too, using PyArg_ParseTuple to pull out C-style arguments, according to a format string. They also need to use occasional calls to Py_DECREF (and it’s NULL-friendly wrapper counterpart, Py_XDECREF). Though this varies – for example, in the above, PyList_SetItem “steals” a reference to “list” and so I don’t need to worry about freeing that particular reference. The documentation tries to explain what needs tracking.

Anyway. This has been a random post. For those of you who are already die-hard Python programmers, I’m sure this is old-hat – and that’s appropriate to us now-officially-old uncle-types (my sister just gave birth last night). For me, it’s stuff I looked at too long ago but didn’t have a need for. Now that I’m looking at writing more funky stuff using Python, I’m working on becoming a Python nut, just like you :-)

Jon.

[0] The installer that was originally written by Red Hat (and used by a metric fucktonne of other people and projects needing a graphical Linux* installer not based YaST, Debian, or their own custom code). The thing that I (blindly, thanks to my previous Debianiteness/Ubuntu craziness that I’m recovering from over time) used to criticize because it wasn’t the d-i du jour. Actually, anaconda is a very powerful Linux installer that gets a lot of things right. And when you stop blindly agreeing with Debian rhetoric, this becomes abundantly more apparent (this isn’t an anti-Debian rant).

Anaconda’s source can probably best be described as something that seems to have evolved over time. There are top level classes (like Anaconda) and some OO concepts in there, but it’s still largely a tangled mess of functions/function pointers (method object references, whatever) that works out in the end. Not that I’m being critical – I’m sure I couldn’t do it any better – but it’s certainly useful to pick at :-)

* I think some people have used anaconda for non-RPM distros, but I’m not aware of anyone trying to install weird stuff like Open Slowaris with it. Random aside: Sun seem to have stopped holding up a new edition of Slowaris Internals. There’s a new edition in Borders, covering Open Solaris and Solaris 10. It’s a shame it’s a bazillion years too late (the last edition was on Solaris 7, and like, totally ruled, dude) but then, I expected nothing less than to have to wait almost a decade for an updated edition. At least they now give out source code to the OS – unlike the many times I asked for it as a student and was told the programme was temporarily suspended…a million years ago.

Turkish ban on YouTube

March 7th, 2007

Yet another reason why Turkey must not, under any circumstances be allowed to join the EU (there are many many more serious examples besides a single website, but this just helps to illustrate a general trend in that country): banning YouTube at the country level. While I have absolutely nothing against the country, until it can grow up and understand why freedom of speech is important, everything that can be (legally) done to stop them joining the EU, should and must be done.

Yes, other countries in Europe block websites and make certain online activities illegal, however actions such as those regularly taken by Turkey seem to go well beyond what Europeans would consider to be acceptable. As a European myself, I find the kinds of values being asserted in their actions are diametrically opposed to those that Europe should hold dear. Of course, I might be wrong. Maybe Europe would be best served by compromising on various fundamental principles just to get another member state.

Jon.

US/UK English translations

March 5th, 2007

So I’m thinking about putting up a proper webpage tracking some of the differences between US and UK English. Maybe a modified dict or something could be the appropriate solution. Until then, here’s a list (I’ll try to remember to update this article when I have new ideas):

  • Advert -> Commercial (Advertisement is not used for TV)
  • Aeroplane -> Airplane (obvious example there)
  • Bin -> Trash Can (more well known example there)
  • Cafetière -> French Press
  • Dressing Gown -> Bath Robe (don’t ever say Dressing Gown)
  • Gherkin -> Pickle (pickled cucumber, more generic than Gherkin too)
  • Glandular Fever -> Mono (full name (infectious) mononucleosis)
  • Hire Car -> Rental Car
  • Jumper -> Sweater (more well known example there)
  • Post -> Mail (more well known example there)
  • Spanner -> Wrench (don’t ever say “throw a spanner in the works”)
  • Trainers -> Running Shoes (or maybe Sneakers)
  • Trousers -> Pants (more well known example there)
  • Washing -> Laundry (though they’d guess)

Of course, I’m not addressing the pronounciation differences. There are obvious examples from popular culture such as Tomato/Tomarto, but then there are more subtle differences in words like vase (no “varse”), adver*tize*ment, and so on. Some of these are well known, others not. And then, of course, there’s a need to adjust general grammar rules for a US audience, too.

I generally modify my pronounciation, as well as my grammar and spelling, in business/consumer settings in order to avoid confusement/random giggling at what I’m saying. Though with friends, I often don’t bother to change how I say something (because it’s more amusing that way around) – but probably still consider it.

Jon.