Sunday, November 30, 2008

Python - a really useful tool

Being largely from the Java world, I hadn't noticed how wide spread the deployment of Python has come. Apparently it is now a standard part of Unix installation (well at least on all Unix boxes I have access to). It can also be run in the same manner as a standard bash/bourne/... Unix script, but still give you the power of a full blown OO languange with a fair number of functional features thrown in. In the future I'm certainly intending to start using it for those more complicated scripting tasks, like extract data from log files, if only to try and solve them in a more functional way.

Below are a few examples. These are largely to remind me of some key aspects that might be useful next time I have to write some scriptable task.

Line counting the functional way

#!/usr/bin/python
import sys

fp = open(sys.argv[1])
print reduce(lambda count,row: count+1, fp, 0)
Executing Unix commands and piping the results
#!/usr/bin/python
from subprocess import call, Popen, PIPE

out = Popen("wc", stdin = PIPE, stdout = PIPE).communicate(Popen("ls", stdout = PIPE).communicate(None)[0]);
print out[0]
The start of a find command
from re import match
from os import walk
from os.path import join

for root, dirs, files in walk('.'):
    print "\n".join([join(root, filename) for filename in files if match('.*\.py', filename)])