We shall employ the same reflectometry script as on the preceding page to explain some Python syntax used in BornAgain scripts.
For easy reference, here again the full script:
We shall now explain the code.
The shebang line
makes the script executable under Unix-like operating systems, see the chapter on how to run scripts from the command line.
Lines between triple quotes
""" Text, text, text """
def get_sample(): ...
constructs and returns a sample model. For more information, see the sample section.
def get_simulation(sample, scan_size=500): ...
if __name__ == '__main__':
ensures that the following statements are only executed if the script is called directly, as a “main” program. This precaution is required by the GUI, where scripts can be imported without being immediately executed.
digests some command-line arguments that are mostly needed by the developers for automatized testing.
sample = get_sample() simulation = get_simulation(sample) result = simulation.simulate()
construct a sample and instrument model and run a simulation.
simulate() returns a
plots the simulated reflectivity as function of the incident glancing angle, using MatPlotLib.