Input Methods¶

Often we write scripts that should be run multiple times with different arguments, an easy example being a script that should be run on different files.

Therefore we want a comfortable way to pass arguments to our script. The easiest method is to make the filename a variable in the source code and change the variable value in the source code. Similar methods are running the script in interactive mode (via python -i myscriptname.py) and then calling a function with different parameters. However, these methods can be very time-consuming, as well as limit the portability and reusability of our code.

Therefore, we will explore a few options to provide arguments to Python code.

Unfortunately, the majority of these methods do not play nicely with Jupyter Notebooks (mostly due to the fact that Jupyter Notebooks are a simple REPL in the browser). Therefore, it is recommended to run the examples as actual Python scripts.

1. The Input Function¶

Python provides the built-in input function which allows to prompt the user for input. It can be called with an additional prompt argument that will be displayed when prompting for the input.

In [ ]:
name = input("Please enter your name: ")
print(f"Your name is '{name}'")

The input function will always return a string. Numbers and other datatypes have to be converted.

In [ ]:
age = input("Please enter your age: ")
age = int(age)

if age % 10 == 9:
    print("Your next birthday will be a special birthday :)")
else:
    print(f"Your next birthday will not be a special birthday :(")

Bonus Tip: The input function can also be very useful to stop the execution of the program. Just add an input function at the position your code should wait. This can be useful, if the code is run in a command line window that will close after execution (e.g. by double-clicking python files on window machines) but the user should be provided with information at the end of the program run.

The advantages of the input function are its simple implementation and ease of use. However, the input function relies on an open terminal which may not be desirable in GUI applications. Additionally, the input function can be clunky for e.g. file selction where a filedialog is more convenient.

2. Dialogs¶

Dialogs are GUI elements that allow to select certain options. Python's standard library includes Tkinter, a GUI library, which provides functions for standard user prompts (for more detailed information see the documentation). These include inter alia

  • floats
  • integers
  • strings
  • filename(s)
  • directory

Furthermore, simple questions are supported via message boxes.

As an example, the content of the following cell will show a filedialog for a filename (remember that this code will not run in Jupyter Notebooks).

In [ ]:
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
filename = filedialog.asksaveasfilename() 
root.destroy()

Dialogs make it easy to enter arguments due to them being part of a GUI. However, the GUI part makes it almost impossible for other software to call your Python code.

3. Command Line Arguments¶

Command line arguments are another way to provide arguments to your script. This makes it also easy to call your script from some other code, making it a building block for further software.

Different modules allow you to easily incorporate command line files into your script (e.g. argparse, or click). argparse is part of the Python standard library, making it a fairly general and reliable solution.

Probably, you'll have used command line arguments several times in your day-to-day work, e.g. running a file with python from your command line:

python3 -i myscript.py

or maneuvering your file system:

cd new_folder
rm -R new_subfolder

Also Pyckett heavily relies on argparse for its CLI tools:

import argparse

parser = argparse.ArgumentParser(prog='Partitionfunction', epilog=recommended_use, formatter_class=argparse.RawDescriptionHelpFormatter)
	
parser.add_argument('egyfile', type=str, help='Filename of the .egy file')
parser.add_argument('--temperatures', '-T', metavar='T', nargs='*', type=float, help='Additional temperatures in Kelvin to evaluate the partition function for')
parser.add_argument('--pickett', '-p', dest='use_pickett_factor', action='store_true', help='Use the (less precise) numeric values from SPCAT')
parser.add_argument('--convergence', '-c', dest='convergence', action='store_true', help='Produce convergence plot')

args = parser.parse_args()

Even though this looks a little intimidating first, it is quiet easy to understand as the documentation is really good.

In [ ]:
!command pyckett_partitionfunction -h