Like Progressive Metal? Check out my band on Spotify, please follow us!
Python Scripting Tutorial
In order for a script to be very useful, you will need to be able to test the conditions of variables. Most programming and scripting languages have some sort of if/else expression and so does python. Unlike most other languages, spaces are very important when using an if statement. Let's do a simple script that will ask a user for a password before allowing him to continue. This is obviously not how you would implement such security in a real system, but it will make a good example of using if and else statements.
#!/usr/bin/env python # This is some secure program that uses security. import sys validPassword = 'secret' #this is our password. inputPassword = raw_input('Please Enter Password: ') if inputPassword == validPassword: print 'You have access!' else: print 'Access denied!' sys.exit(0) print 'Welcome!'
Remember that the spacing is very important in the if statement. The indented lines of code after the if condition will run if the condition is true. If the condition is false and there is an else section, then the indented code after the else section will run.
In this example, the "is equal to" comparison is used in the if condition. This is represented by the "==" operator. If you wanted to do the check "is not equal to", you could use the operator "!=". Below is a table of some of the comparisons you can use in your scripts:Comparisons:
Another thing new to you in this script is the sys.exit function, which is used to exit a python script. The zero as the parameter to sys.exit represents the return status of the script process. The function raw_input() is also shown in this example. This will display the string given as a parameter to the function and then wait for the user to type input and hit enter. The input is returned by the function into the variable "inputPassword".
Let's enhance our years.py script to include some better checking. This time we will prompt for the variables if they are not given on the command line.
#!/usr/bin/env python # Years till 100 import sys if len(sys.argv) > 1: name = sys.argv else: name = raw_input('Enter Name:') if len(sys.argv) > 2: age = int(sys.argv) else: age = int(raw_input('Enter Age:')) sayHello = 'Hello ' + name + ',' if age == 100: sayAge = 'You are already 100 years old!' elif age < 100: sayAge = 'You will be 100 in ' + str(100 - age) + ' years!' else: sayAge = 'You turned 100 ' + str(age - 100) + ' years ago!' print sayHello, sayAge
With this script, you do input in several ways. You can give the name and age on the command line, you can give just the name on the command line and read the age from user input, or you can give nothing on the command line and read name and age from user input. When reading from user input to a number (integer) variable, you must use the int() cast just like you do for reading sys.argv. Also, when appending to a string with the + operator, you must cast any int value (such as "100 - age") to a string with the str() cast.
This script also introduces a new keyword, elif this is the "else if" of python. Notice that our if conditions now have 3 checks, if the age is 100, if the age is less than 100, and everything else (age > 100). We could have put elif age > 100 in place of the else keyword, but they would do the same thing in this situation.
There is still something missing in this script. How is the user to know what the input should be on the command line? What if the user assumes that the age comes before the name? The script will crash. One way that this can be resolved is to use the powerful OptionParser in the argparse package, which we will discuss in the next section.
Blob Entry 1
Blob Entry 2
Blob Entry 3
Blob Entry 4
Blob Entry 5
Blob Entry 6