Tuesday Jun 03, 2008

Python bites

I wrote my first Python program over the weekend. I very foolishly ran out space in my root filesystem on my cobalt qube. This is v. stupid and had two causes.

  • I have installed an application in the root disk volume. While this is not unusual, it has a database and its log files in the same file system. An error caused the log files to grow and burst the file system. [ So RFE the app dude, the install should offer locations for the database and logs.].
  • The OS very sensibly mailed me quite a lot that I had a problem, but it mailed it to the administrator account on the qube and while I can access this mail account remotely, I didn't; I was busy.

So I decided to write a script that mailed me on my phone (via SMS) should this happen again. (I also need to move the data files and logs to sensible places).

I would normally do this in ksh, but the qube doesn't have this, so I started in bash. I quickly discovered that my version of bash doesn't have associative (or any) arrays. It does have the string handling facilities of the ksh, but I couldn't find them; I had forgotten the syntax.

    line="/root=OK"; state=${line##\*=}; echo $state
    $ OK

actually works. NB I tried the arrays on my virtual box ubuntu 7 build, which I now use as a terminal host for the qube; I get them inside a re-sizable x-window. The arrays seem to work, but not associative arrays, so its another nail in the Qube's coffin; the Qube's bash has no arrays at all.

When I say I've written it, its not yet finished, but what I have done shows me that its a very powerful and economic language. Given that this sort of script, 90% of the code is string handling and enviromental discovery, with one command at the end of the script doing the work. I actually only use the UNIX 'df' utility and 'mail' program. I invoked the mail program via


where 'despatchmail' is an external shell script and it means that I can publish the program without stating the destination e-mail addresses. It could also invoke mail directly if I choose. I provide the df reply via a pipe to the program. (I did this because a coding example was more easily available it could be done in a number of ways.)

I am particularly impressed with python's dictionary feature and created one to hold the previous state, one to hold the current state and one to hold the utilisations. I can then use the file system mount point name as the retrieval key for all three arrays. e.g.

   >>>states={};      # states is an empty dictionary
   >>>states[s[:s.index('=')]]=s[s.index('=') + 1:]
     # assigns the state clause to the dictionary, the filesystem name is the index.
   >>>print states
   >>>{'/root': 'OK'}

and the real one looks like

    {'var': 'OK', 'home': 'OK', 'root': 'OK'}

and the utilisations from the df are held as

    {'var': 9, 'home': 38, 'root': 77}

N.B. The utilisation values are held as integers and its now easy enough to write a test such that if a directories utilisation is above a threshold, then set the status code to something else

    for keys in states.keys():
        if utilisations[keys] > threshold:

the first line ensures the tests are performed for each value pair in the states dictionary object.

So I was pleased to find a decent problem to test the language out on. When I have finished it, I might publish it in full. It might be useful to others, and you might be able to point out where my COBOL trained brain is still using tricks I learned 25 years ago. I know the parser is very powerful, and hence a line of code can perform a number of function, which means that what I would expect to take several lines can usually be done in one.

tags: technology programming language python linux utility

Saturday Mar 15, 2008

Are the English giving up with foreign languages?

Earlier this week, the Guardian reported that Cambridge University had finally dropped the requirement that undergraduate students have a language GCSE (16+). I remarked that I thought it a shame, and that the english education system should teach foreign languages, but it was pointed out to me that the national curriculum no longer mandates a language at GCSE and so Cambridge's previous policy would in future conflict with their and the government's goal of opening Oxbridge up to more state sector applicants. It seems to be a fact that english schools find it harder to get higher grades in languages than other subjects and that the pressure of the league table places has led a number of them to drop languages very rapidly.

Its yet another example of allowing the difficult to fall out of the education system.

Back in December, when I visited the EU Parliament building. I was taken aback by the number of languages spoken in the EU, since the translation booths are situated around the hall, and it is a very physical demonstration of europe's linguistic diversity. There 23 official languages. I have had it pointed out several times in my recent travels that the ubiquity of English means that I don't have to worry, to which I have two replies. The first is that, when I was at school, no-one had any idea of whether the EU was going to work or not nor how English would become so pervasive. I was offered the opportunity to learn both French and German, which I did with varying degrees of success. Secondly, its very rude to assume that others will learn your language, particularly if you are in their their country. I wish I could do better, but it seems the UK's educators disagree.

The map below is off the European Union and references the EU membership page.


Countries of the EU


On the EU membership page itself, the map has an HTML image map which displays the languages by country as you hover over each country. Interesting how such an old technology has such descriptive power. I wonder if I could have used an <IFRAME> to include this on the blog, although including other peoples material without permission in a way that is not clearly hyperlinked is not very good manners.


Thursday Dec 20, 2007

Republican Democracay

We then walked over to the European Quarter, as we wanted to visit the debating chamber of the European Parliament. We went via the Place Jourdain, where we bought some 'frittes' from Maison Antoinne. This is a stall which has an arrangement with the surrounding bars so you can eat indoors and warm up with your drink of choice, coffee or schnapps. The parliament building permits visits to the debating chamber, as they should;, we pay for it. So we went there and had a look round.


The EU parliament chamber.


Outside the the building there are plaques and flags for each of the member states, together with a statue of a women holding the Euro symbol. The buildings are contemporary and stark contrast to the Hapsburg grandeur of the Upper Town. In the park beside the parliament building is a section of the Berlin Wall, with its graffiti presumably untouched from when it was in Berlin. One of the wipeout games we play at home is 'countries of the EU', this is fun because its changing so rapidly and because of the various states that countries can occupy. States can be in negotiation to join, members, euro zone members or Schengen treaty signatories. So when we entered the chamber, hanging above the chamber are the the translation booths, and while there are 27 member states, there are 23 official languages, so one booth for each language really brought home the linguistic diversity of today's European Union.





« July 2016