[Tutor] Script to generate statements

Steven D'Aprano steve at pearwood.info
Sat Mar 16 04:25:29 CET 2013


On 16/03/13 06:42, Charles Leviton wrote:


> This is the script I came up with...Would you critique it and let me know
> how I could have done it better?
>
> #create a series of bind statements
> fo = open('i:/text/jclout.txt', 'w')
> fi = open('i:/text/bindjclfirstpart.txt','rU')
> fibindjclvar = open('i:/text/bindjclvariable.txt','rU')
> filistofdbrms= open('i:/text/bindjcldbrmlist.txt','rU')


The comment appears to be irrelevant and/or misleading, although maybe that is just because I don't understand your terminology. You say you create a "series of bind statements", but you don't seem to do anything of the sort. You actually open four files instead.


> varlines =[]
> varlines = fibindjclvar.readlines()

You don't need to pre-initialise a variable to an empty list before reading lines.


> for line in fi: #write out all the lines in the first part of JCL
>      fo.write(line)
> fo.write('\n')
> varline = ''

The above variable name is not helpful. "varline" -- what does it represent? It's very similar to "varlines" defined above, so much so that the first time I read it I thought you had overridden the same variable. Can you think of a more descriptive name for this?

Also, again, there's no need to pre-initialise it.


> for dbrm in filistofdbrms:
>      fo.write('\n')
>      for index in range(0,9):

Is this correct? You only care about the first nine lines of the fibindjclvar file? What if it has fewer than nine lines? What if it has more?

By the way, it is normal to write that as range(9) rather than range(0, 9).


>          if varlines[index].find('member') > 0:
>              varline = varlines[index] + '('+ dbrm + ')' + ' -'
>          else:
>              varline = varlines[index]
>          fo.write(varline)


Since you never use the index except as a way of extracting a line from varlines, perhaps this snippet will be better:


for dbrm in filistofdbrms:
     fo.write('\n')
     for line in varlines[:9]:  # Process only the first 9 lines.
         if line.find('member') > 0:
             line += '('+ dbrm + ')' + ' -'
         fo.write(line)


> fo.close()
> fi.close()
> fibindjclvar.close()
> filistofdbrms.close()


This bit is fine :-)



-- 
Steven


More information about the Tutor mailing list