6. Runtime Control with Scripts#

There are numerous ways to control what and how Python scripts run and move through various tasks to generate the needed analysis/outcome that is coded within your program. There are many instances where you have essentially the same task that you want to perform, but just on a different set of data. Instead of going in and modifying a script to be able to read in a certain data file, a program can be set up to either ask for input or an argument could be added when going to run the script. Either way can make it easier to accomplish needed tasks without endlessly modifying individual pieces of code.

6.1. Interactive#

One way to control a script at runtime to make it interactive (i.e., where it will ask you a question and record your response to run the script). Below is an example script that you can put into a new text file to demonstrate how interactive scripting can work.

The script first prints the question or statement, then awaits a response. The input() function contains the text that is printed to the screen variable and will ingest the value submitted from the command line that you type in and hit enter, finally setting the value of the variable to the left of the equal sign to whatever value was input. Create and run the script (name it ex_interactive.py) below and plug in your first name, for the first input, and your last name, for the second input to test how interactive scripting can work.

# Ask for input and store in variable
firstline = input('Go ahead: ')
print(f'You entered: {firstline}\n')

# Ask for second input and store in variable
secondline = input('Go ahead (2nd time): ')
print(f'You next entered: {secondline}\n')

Run the script from the Linux command prompt:

kgoebber@bergeron:~> python ex_interactive.py

Example Input/Output:

Go ahead: Kevin
You entered: Kevin

Go ahead (2nd time): Goebbert
You next entered: Goebbert

A practical way of using an interactive script for helping make a map is to have a script ask you which region you want to plot for a surface map.

6.2. Arguments#

Another way to control script workflow is to give the script some information from the command line when you call the script. In the example using the while loop below we set the variable total using the first system argument value (i.e., sys.argv[1] in the code below) which allows the users to tell the script how many iterations we wanted to loop over at run time.

import sys

# Get value from runtime argument
total = int(sys.argv[1])

# Set initial value
inc = 0
while inc < total:
    print(inc)
    # increment variable value by adding 1
    inc += 1

Note

In order to use the system argument we need to import the sys library, which is part of the base Python language. The way in which the arguments are parsed are by separating the inputs via spaces.

Create a script using the code written above and call it ex_while_argv.py. Then to run the code you will need to supply one argument (a number) to allow the script to complete its work. Otherwise you will get an error. Try a couple of different numbers to see how the running of the program changes depending one the value used for the argument.

Run the script from the Linux command prompt:

kgoebber@bergeron:~> python ex_while_argv.py 15

You can also have more than one argument. The second argument would be the variable sys.argv[2] and the third argument sys.argv[3], and so on. Essentially, you are accessing the contents of a list that contains everything that comes after the initial python call. So in the example running the ex_while_argv.py file above, the system argument list would be ['ex_while_argv.py', '15']. Recall that list addresses begin with zero, so that the first “usable” argument will be at address one (i.e., sys.argv[1]). A practical situation for using an argument with a script for making weather maps would be to use them in conjunction with if-then statements to allow runtime control to choose different settings for a chosen upper-air maps level (e.g., 850, 500, 300 hPa).