Creating Simulation Input Files Using Python

I am using a scientific modeling package that requires several text input files for each experiment. These files can be quite long and have many sections of templates in them; however, in many places specific “experiment-specific” values ​​must be entered in these files.

I would like to automate the creation of these files and make it so that it is convenient.

I am currently using the Python script that I wrote, which uses triple quotation marks for text and variable substitution (using% and .format ()) to create sections in files. Then I write these blocks to the appropriate files.

Considering proper aesthetic indentation in the resulting input files is difficult; in addition, the script auto-generator is becoming more and more opaque, as I am improving the types of modeling and parameters that can be processed.

Does anyone have any suggestions for managing this task in a more elegant and convenient way?

I know patterns like jinja. Do they have advantages beyond generating html-like files? Has anyone used them for the above purpose?

Perhaps a completely different approach would be better.

Any suggestions are welcome.

+4
source share
2 answers

Jinja doesn't care what type of file you make. Text is text, it is text, unless it is binary. Not even sure Jinja cares either.

IPython , and in particular nbconvert, uses Jinja2 to export LaTeX , ipynb, markdown, etc.

There is also an IPython laptop with Jinja2 magic if you want a demo.

+2
source

My usual approach to this problem is to create a small library of functions that help me create and customize a boiler stove. I don’t know what your experiment definition language looks like, but usually I need to write a function that writes text to initialize the simulation, a function that writes text to complete the simulation, and some other functions to write out different pieces of text that define each type of experiment.

By putting these functions in a file named mysim , let's say I could use them as follows:

 from mysim import sim_init, sim_conclude, experimentType1, experimentType2 sim_init (name="Today Simulation", author="Simon") for param1 in [0,1,2,3,4,5,6,7,8,20,30,40,50,60,70]: experimentType1 (param1) for param2 in ["A", "B", "C"]: experimentType2 (param1, param2) sim_conclude (savefile="output.txt") 

This Python script will generate an input simulation file that will run an experiment of type 1 for each param1 value and conduct an experiment of type 2 for each combination of param1 and param2 .

Implementations of the functions themselves may look messy, but the script that creates the particular simulation file will be simple and straightforward.

+1
source

Source: https://habr.com/ru/post/1501985/


All Articles