Like Progressive Metal? Check out my band on Spotify, please follow us!
Python Scripting Tutorial
When your scripts start to become very large, you may tend to notice that you are repeating code more often in your scripts. You have the
ability to create functions inside of your script to help with code reuse. Writing the same code in multiple sections of your script can lead
to severe maintenance problems. When you fix a bug in a section of code you need to be sure that all sections of code that are repeated will
also have those fixes. A function is a block of code that can be called from other parts of your script. It can have parameters passed to it as
if it were a separate script itself. As an example, we will create a function called logit, which will take two parameters, a level and
a message. The level will be a number between 1 and 3 that will indicate the severity of the log message. The level of messages that you want
to view will be passed in the command line of the script.
#!/usr/bin/env python import sys logLevel = int(sys.argv) def logit(level, msg): if level >= logLevel: print 'MSG' + str(level) + ':', msg def getUser(): logit(2, 'Entering Function getUser()...') user = raw_input('Enter User Name: ') logit(1, 'Leaving Function getUser()...') return user logit(3, 'Starting Script...') logit(3, 'User Entered: ' + getUser()) logit(3, 'Ending Script.')
In this script, we are using 3 different numbers for log levels. 3 is like an INFO level, 2 is like a DEBUG level and 1 is like a TRACE level, to see the most log detail possible. The log level to be used is passed as the parameter to the script for simplicity. Let's save this file as logit.py and run it several times with different input levels...
$ ./logit.py 3 MSG3: Starting Script... Enter User Name: Python MSG3: User Entered: Python MSG3: Ending Script. $ ./logit.py 2 MSG3: Starting Script... MSG2: Entering Function getUser()... Enter User Name: Python MSG3: User Entered: Python MSG3: Ending Script. $ ./logit.py 1 MSG3: Starting Script... MSG2: Entering Function getUser()... Enter User Name: Python MSG1: Leaving Function getUser()... MSG3: User Entered: Python MSG3: Ending Script.
The script creates two functions, one for logging and another to get the user name input from the user. Notice that we use the def keyword to define a function, and the syntax is very simple. After the function name, you give the parameters to the function. The parameters are accessed in the function just as local variables. The indentation defines where the function starts and ends, just as it does with if/else and looping blocks.
You will also notice that one function has a return value and the other doesn't. You don't need to define the return value in the definition of the function, you just need to use the return keyword in the function for it to return a value.
You can write recursive functions in a python script as well. Consider the factoral:
#!/usr/bin/env python import sys def fac(n): if n > 1: return n * fac(n - 1) else: return 1 num = int(raw_input('Enter a number: ')) print str(num) + '! =', fac(num)
A recursive function is a function that calls itself. Notice at line 6 in the script, it calls the fac function inside itself. Recursive functions have the possibility of going in an endless loop and crashing if you do not code them right, so only use a recursive function if you really understand what you are trying to do.
Now that you know about functions, you can learn how to organize them into logical units with classes.
Blob Entry 1
Blob Entry 2
Blob Entry 3
Blob Entry 4
Blob Entry 5
Blob Entry 6