python - How to duplicate a file but change a few parameters inside? -



python - How to duplicate a file but change a few parameters inside? -

i using python interface several fortran files in model. want duplicate fortran file several times @ each copy, alter parameters describe model.

for example: have fortran file below

!file.f ! fortran code !parameters alpha = 0.5 beta = 100 ...

i want re-create file.f several times such have file1.f, file2.f, file3.f, etc. however, @ each file duplicated want alter parameters alpha , beta automatically. thanks

edit: allow me explain little further. using python implement info assimilation (kalman filtering) models have been developed in fortran. basically, how works @ each specified time step fortran models stop running, integrate real world info model info , in python. after integration (assimilation), rerun same models time using new parameters obtained fusing info model , observations , new initial conditions. utilize python except run model beingness done fortran.

i think consistent way go utilize templating engine. python has lot of then, deployed within web applications.

but purpose of templating engines allow 1 have mass of code, needs nos alter static text, , through special markup interpolate variables generated within python code.

depending on complexity of parameters don't need separte templating engine @ all, , go on python string formating capabilities, in illustration bellow.

template engines provide bit capacity, ability unroll loops , conditionals within template.

example - write fortram template like:

!file.f ! fortran code !parameters alpha = %(alpha)s beta = %(beta)s

and in python code, write like:

template = open("fortram_template.for", "rt").read() info = {"alpha": 0.5, "beta": 100} open("fortram_1.for", "wt") output: output.write (template % data)

python fortran code-duplication generated-code

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -