#!/usr/bin/env python # Copyright Harry Mangalam # I explicitly place this work into the Public Domain # you can do anything you like with this code. # # This is a tutorial skeleton python script to process STDIN line by line with some # extras thrown in to make it easier to debug and some examples as to how to # add commandline options etc. # following triple quoted stanza is the __doc__ string or 'docstring' - it is the # optional (but very useful) text associated with each function or class in python # that describes what it does. Many Python utilities depend on the docstring to provide # help or at least an indication of what the associated code does. """ This script: - allows the user to assign some values to variables using commandline options - processes a file line by line, - breaks the file into values based on defined input delimiter (default ) - does a trivial calculation on the values extracted based on the values extracted - outputs the transformed data.separated by a defined delimter (defaults to ) Usage: linebyline.py [options] < file or some other cmd -> | linebyline.py [options] Options: -c / --comments ...Comments pass thru untouched, else they are stripped -h / --help .......Print this message and exit. -d / --debug.......Turn on debugging of script -i / --id='input delimiter character, string' -o / --od='output delimiter character, string' --var1=float --var2=float --var3=float -p / --prefix='string to add to beginning of character field' -s / --suffix='string to add to end of character field' """ import sys, string, fileinput, os, getopt, errno, time # see the showmedo.com videos for more on iPython # http://showmedo.com/videos/series?name=PythonIPythonSeries # uncomment the 2 lines below to enable iPython debugging. #from IPython.Shell import IPShellEmbed #dbg = IPShellEmbed() # and insert 'dbg()' where you want to break. # following function uses the docstring above as the help message # prefixed by an optional message as dmeo'ed below and provides the # supplied exit code on exit to test the exit condition (normal (0) # or error (nonzero - see try/except stanza below) def usage(code, msg=''): if msg: print >> sys.stderr, msg print >> sys.stderr, __doc__ sys.exit(code) # main function def main(): # Set up the defaults and variables ID = "\t" # default input delim is a TAB OD = "\t" # ditto output PREFIX = "" # default is NO prefix SUFFIX = "" # ditto suffix VAR1 = VAR2 = VAR3 = 1 # init all VARs to 1 DEBUG = 0 # off by default COMMENTS = 0 # default it to strip comments. # for some explanations of what the following translates into, try: # http://www.linuxjournal.com/article/3946 # the following try/except stanza processes the commandline options. try: opts, args = getopt.getopt(sys.argv[1:], 'cdhi:o:p:s:', ['comments', 'debug', 'help', 'id=', 'od=', 'var1=', 'var2=', 'var3=', 'prefix=', 'suffix=']) except getopt.GetoptError: # print help information and exit: usage(1, "\nThere was an error specifying an option flag. The correct usage is:") for opt, arg in opts: if opt in ('-h', '--help'): usage(0) elif opt in ('-c', '--comments'): COMMENTS = 1 elif opt in ('-i', '--id'): ID = arg elif opt in ('-o', '--od'): OD = arg elif opt in ('--var1'): VAR1 = float(arg) # numeric args have to be 'floated' elif opt in ('--var2'): VAR2 = float(arg) elif opt in ('--var3'): VAR3 = float(arg) elif opt in ('-p', '--prefix'): PREFIX = arg elif opt in ('-s', '--suffix'): SUFFIX = arg elif opt in ('-d', '--debug'): DEBUG = 1 linecnt = 0 # line count for debugging while 1: # read stdin effectively line by line line = sys.stdin.readline()[:-1] # the subscript [:-1] trims the newline character string.strip(line) # strip it of leading and trailing spaces linecnt = linecnt + 1 if line and line[0] != "#": # if a non-comment line #dbg() ttlflds = line.split(ID) nbrflds = len(ttlflds) # get the number of fields detected if DEBUG: print "Line %d %d fields: " % (linecnt, nbrflds) output = "" for fld in ttlflds: try: fld = float(fld) * VAR1 * VAR2 * VAR3 + VAR2 output = output + "%f" % (fld) + OD except: fld = PREFIX + fld + SUFFIX output = output + fld + OD print output if DEBUG: time.sleep(3) # this just slows things down for debugging elif COMMENTS: print line # passthru comments # now that all the code has been processed, start up main(). if __name__ == '__main__': main()