Laptop diaries XII, Python

I wrote my first Python program over the weekend. The program determines if the file system is in jeopardy of bursting and sends a text. I have copied this in full to this blog as while the implementation technology is defunct, the problem remains real, although IFTTT are doing a good job of solving it. How to perform a mail to text is not documented here. It’s mainly about using associative arrays and dictionaries. This was written on a Cobalt Qube; I very foolishly ran out space in my root filesystem. 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. N.B. 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-sizeable 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 environmental 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
>>> s="/root=OK";
>>> 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.


Originally posted on my sun/oracle blog, republished here in July 2016.


Comments are closed.