Not that I did too much in it, but Perl was my scripting language of choice. I've now replaced it with Python. I find it easier to concisely express what I want, and what I produce is far more readable. My latest task was generating Gnuplot graphs for a bunch of .csv data files in a directory after doing a simple average on the data. It turned out much cleaner than I expected:
As an intro to the language, I found Dive Into Python helpful.
Yes, I realize this is of interest to about two or three of you.
Code: Select all
Further than that, I'll actually consider it over C++ for my next significant programming project. (I'm tempted to port what I have over to Python, as I know some of it would be easier to express. I don't because I've yet to find a parsing framework in Python that I like, I am nominally concerned about runtime performance, and it doesn't seem like a wise use of my time.)#! /usr/bin/env python
import os, csv, Numeric, Gnuplot
def pow2_range(start, finish):
for i in range(start, finish):
yield 2**i
def set_options(graph, points):
graph('set datafile separator ","')
graph('set xlabel "unroll factor"')
graph('set ylabel "execution time (s)"')
graph('set xtics (' + points + ')')
graph('set xrange [0:130]')
def main():
points = ','.join(['"%s" %s' % (str(i), str(i)) for i in pow2_range(1,8)])
colors = ['-1', '1', '3', '-1', '1', '3']
multi = Gnuplot.Gnuplot()
set_options(multi, points)
for color, file in zip(colors, sorted([f for f in os.listdir('.') if f.endswith('.csv') and not f.endswith('_avg.csv')])):
graph = Gnuplot.Gnuplot()
graph.title(file.replace('.csv', '').replace('_', ' '))
set_options(graph, points)
avg_nm = file.replace('.csv', '_avg.csv')
avg_f = open(avg_nm, 'wb')
avg = csv.writer(avg_f)
for row in csv.reader(open(file, 'rb')):
avg.writerow([str(int(row[0])), str(Numeric.average([float(i) for i in row[1:]]))])
avg_f.flush()
graph.plot(Gnuplot.File(avg_nm, with='linespoints'))
graph.hardcopy(file.replace('.csv', '.ps'), color=True)
multi.replot(Gnuplot.File(avg_nm, with='linespoints lt ' + color))
multi.hardcopy('all.ps', color=True)
if __name__ == "__main__":
main()
As an intro to the language, I found Dive Into Python helpful.
Yes, I realize this is of interest to about two or three of you.