Wednesday June 04, 2008 |
Python bitesI 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.
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 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 os.system('./despatchmail') 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 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(): 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 (2008-06-04 03:23:02.0) Permalink Comments:
Post a Comment: Comments are closed for this entry. |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||