Archive for the ‘General’ Category

Turkey, India, who do we pander to next?

Tuesday, 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

Thursday, 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

Thursday, 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

Wednesday, 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

Monday, 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.

Something came in the mail…

Wednesday, February 28th, 2007

I wasn’t expecting this to turn up for a while longer, so it was surprising when the UPS guy turned up today with a bunch of copies of my book.

Jon.

HOWTO: Write a successful US TV commercial dialogue

Sunday, February 25th, 2007

We’ve all seen them. Shallow, annoying, pointless TV commercials for products of limited or no value. Sometimes these commercials can leave you feeling you’re getting a deal when in fact they want to take you for a ride. But now there’s something different. And it’s called Jon’s US TV commercial guide. That’s right. Thanks to its patented technology and spaceage construction, Jon’s US TV commercial guide brings benefits never before available at any price. Jon’s US TV commercial guide is not available in stores but you can get all of these benefits and more by calling today!

“I tried Jon’s US TV commercial guide and was amazed. I’d tried other HOWTOs, but this was different. It really worked!”

“Hi, I’m doctor Bob Bobson from the center for Bobson research. I’ve been a family doctor for many years and I’ve seen first hand how many HOWTO guides can leave people drowsy and discontented. But Jon’s US TV commercial guide is the first guide in its class to really work. My patients agree, Jon’s US TV commercial guide really does work. And it’s that simple.”

“My doctor told me that Jon’s US TV commercial guide isn’t for everyone. It isn’t for people with kidney or liver problems, history of heart attack or high blood pressure. She also said I shouldn’t take it if I’ve been standing on my head or am a humorless drone, as these can lead to a successful lawsuit for everyday commonsense aversion”.

Jon’s US TV Commercial guide isn’t available in stores, but you can get it risk free in your own home for just ten payments of $19.95! That’s right! Ten payments of $19.95! People ask how we’re able to offer such value at such a great price. The secret’s in the patented technology harnessing the latest scientifically proven breakthrough in spaceage construction. In fact, we’re so confident in this product that we’re offering it to you risk free for 30 days! That’s right! Risk free in your own home for 30 days!

But wait! Call now and we’ll double your order. That’s right! We’ll double your order at no extra cost to you! Just pay additional shipping. Two Jon’s US TV Commercial guides for the price of one and a 30 day risk free trial in your own home! This offer isn’t available in stores, but you can take advantage of it right now – risk free – for just ten payments of $19.95!

But wait! Call in the next 5 minutes and we’ll shove it up your ass, no questions asked! That’s right! We’ll shove it up your ass, no questions asked! Call now!

Jon.