[Tutor] Script to generate statements

Peter Otten __peter__ at web.de
Sat Mar 16 11:05:41 CET 2013


Charles Leviton wrote:

> I was recently given this task.  it's a very IBM mainframe specific task
> so
> I'm not sure how to find equivalent terms in another environment.  I will
> just use the mainframe terminology and hopefully y'all can figure out what
> I mean.
> 
> Given a list of DBRM members create a JCL which has a series of bind
> statements for each DBRM.
> 
> This is the tack I took.  I have 3 input files
> a_ contains the fixed part of the JCL
> b_ contains the template for the bind statement.
> c_ contains the list of DBRMs
> 
> 
> This is the script I came up with...Would you critique it and let me know
> how I could have done it better?

Incremental changes that have not been mentioned:

You can use

with open(...) as fo:
    ...

instead of

fo = open(...)
...
fo.close()



> #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')
> 
> varlines =[]
> varlines = fibindjclvar.readlines()
> for line in fi: #write out all the lines in the first part of JCL
>     fo.write(line)
> fo.write('\n')
> varline = ''
> for dbrm in filistofdbrms:

dbrm will have a trailing newline that you probably need to strip:

      dbrm = dbrm.strip()

>     fo.write('\n')
>     for index in range(0,9):
>         if varlines[index].find('member') > 0:

find() is case-sensitive; you probably need find("MEMBER"). Also, find() 
returns -1 when the token is not in the line. Because of the leeading spaces 
in

>      MEMBER

it doesn't matter here, but you should make it a habit to test

some_string.find(token) >= 0

or use the less errorprone variant

token in some_string

>             varline = varlines[index] + '('+ dbrm + ')' + ' -'

>         else:
>             varline = varlines[index]
>         fo.write(varline)
> 
> fo.close()
> fi.close()
> fibindjclvar.close()
> filistofdbrms.close()
> 
> The "variable" part of the bind statement is where I have to include the
> DBRM name.  I look for the word 'member' in the template.
> Template looks like this (content of bindjclvariable.txt)
> BIND PACKAGE(PROD) -
>      MEMBER
>      OWNER(PRODOWNR) -
>      QUALIFIER(PRODTBLS) -
>      ISOLATION(CS) -
>      EXPLAIN(YES) -
>      ACTION(REPLACE) -
>      CURRENTDATA(YES) -
>      VALIDATE(BIND)
> 
> Thanks!

Finally, throwing in some guesswork as I don't know JCL, here's how I would 
attack your problem: Given

$ cat bindjcldbrmlist.txt 
alpha
beta
$ cat bindjclfirstpart.txt 
fixed
header

$ cat bindjclvariable.txt 
BIND PACKAGE(PROD) -
     MEMBER({0}) -
     OWNER(PRODOWNR) -
     QUALIFIER(PRODTBLS) -
     ISOLATION(CS) -
     EXPLAIN(YES) -
     ACTION(REPLACE) -
     CURRENTDATA(YES) -
     VALIDATE(BIND)

$ 

the script

$ cat generate.py
with open('jclout.txt', 'w') as fo:

    with open('bindjclfirstpart.txt','rU') as header:
        fo.writelines(header)

    with open('bindjclvariable.txt','rU') as fibindjclvar:
        template = fibindjclvar.read()

    with open('bindjcldbrmlist.txt','rU') as dbrms:
        for dbrm in dbrms:
            dbrm = dbrm.strip()
            fo.write(template.format(dbrm))

produces the following output:

$ cat jclout.txt 
fixed
header

BIND PACKAGE(PROD) -
     MEMBER(alpha) -
     OWNER(PRODOWNR) -
     QUALIFIER(PRODTBLS) -
     ISOLATION(CS) -
     EXPLAIN(YES) -
     ACTION(REPLACE) -
     CURRENTDATA(YES) -
     VALIDATE(BIND)

BIND PACKAGE(PROD) -
     MEMBER(beta) -
     OWNER(PRODOWNR) -
     QUALIFIER(PRODTBLS) -
     ISOLATION(CS) -
     EXPLAIN(YES) -
     ACTION(REPLACE) -
     CURRENTDATA(YES) -
     VALIDATE(BIND)

$ 



More information about the Tutor mailing list