Python:Boilerplate code
by Sorressean on Jan.07, 2010, under tech
Many people have chosen python as the scripting language for choice for both applications, and quick utilities or scripts. Whether you wish to write a frontend to a program, or a quick and easy script to filter files, Python is a great solution.
Providing easy-to-use modules, python gives you the ability to build off of a module that has been created, or easily add your own, in order to accomplish your task. Using Python’s extensive library, most of your day-to-day tasks can be accomplished.
As I will be posting utilities and small tutorials on tasks using the python library, I thought it fit to start off with some boilerplate code. The goal of this article and script, is to give you something you can easily build off of, while maintaining flexibility and ease-of-use.
The code can either be downloaded from this article as a python file, or copied and pasted from below.
#!/usr/bin/env python
#begin boilerplate code:
#You can append other imports after this.
import sys,optparse
def main(argv):
#we set up our option parser; this can be extended, for now it contains one option:
#A quiet switch.
#This controls whether or not messages are printed to the screen.
cmdparser=optparse.OptionParser()
cmdparser.add_option("-q","--quiet",action="store_false",dest="debug",default=False,help="Suppresses all messages from being printed to stdout")
(options,args)=cmdparser.parse_args()
#program-code goes here:
return 1
if (__name__ == "__main__"):
#we call the main function passing a list of args, and exit with the return code passed back.
sys.exit(main(sys.argv))
As you’ll notice, the first line just tells the shell to use the python interpreter. By using env, the shell will invoke whatever version is the default on the users system; this makes it easier than specifying a specific version in the path, as the user may or may not have that version. After we tell the shell what program to invoke for parsing, we import two modules: sys and optparse, which will both be used later in the code.
For those familiar with c or c++, the next line may look familiar; we declare an entrypoint for the program. While this isn’t needed, I prefer to include it for readability purposes. The main function takes one argument, which is an array of arguments passed to the script, with argv[0] being the name of the program, as it is in c or c++ programs.
Directly inside the main function, we declare an option parser, and add one option to it; that of the quiet switch. This may or may not be used, in most applications it would be intended to stop all output going to the console. This may be useful for example, if the user were running your program from within a script, or as a daemon. After we feed the option parser it’s switches, we parse the options and end up with two more variables. The options variable is a single object, containing the fields of the options that were set, or provided as defaults to the option parser, while the args variable is simply an array of args that was not parsed from within the option parser.
By now, the main function has three variables within it’s scope: an argv variable, holding all the arguments passed to the program. An args variable, which holds the arguments passed to the option parser that weren’t parsed (if any), and an options variable that holds the value of the options.
At this point, you would insert your program code, and return with an exit code, which will be passed back to the operating system. Returning 0 signifies a successful program run, while returning anything else signals an error; this may be useful for passing an exit status back to the calling script.
Directly after the main function, is an if statement to insure that we aren’t executing within the bounds of a function, and then the main function is executed, returning the exit code back to the shell, and passing along the arguments that were passed to the script.
I hope that this code will be of some use in the future for your application development in python. The tutorials and code samples beyond this point will all be built off of this boilerplate code, so you will know what to expect when reading the code in the future.
In the next article, I plan on creating a parser that will take the links either from a URL, or an html page, and parsing out the links. The user will be able to determine whether the links should be passed to an output file, or displayed directly on the console.