DebuggingPython has nice capabilities for introspection which really helps when you want to debug a python script or program. This debugging module demonstrates how you can get an informative debugging output from very simple debugging statements in the code. The module source code can be downloaded here. It is also part of the webapp-config tool which has a live code repository here. Basic usageIn order to use the module it must be placed somewhere in the PYTHONPATH. The code can then be imported using the following statement: from debug import OUT If it the file is located in a sub directory the name of the folder must be prepended (e.g. folder.debug). In addition the directory must be marked as a python package by adding an __init__.py file to the directory. The imported OUT object can be used for debugging by adding a statement like this:
OUT.debug('What is happening here?')
It is obvious that this will probably print the given statement. But while such messages may be useful during debugging the main concern are the values of variables that are being used in the code. It is not necessary to add them explicitly to the debug statement here since Python is capable of automatically extracting all variables from the caller of the debug function.
a = 1
b = 'Hello World'
OUT.debug('What is happening here?')
The example above would not only print the statement What is happening here? but in addition the values of variables a and b. Here is a transcript of an example python session:
>>> from debug import OUT
>>> OUT.debug_on()
>>> OUT.color_off()
>>> a = 1
>>> b = 'Hello World'
>>> OUT.debug('What is happening here?')
//---------------------------------------------------------------
// Method: ?
// ------------------> a:
// 1
// ------------------> b:
// Hello World
// ------------------> __builtins__:
// <module '__builtin__' (built-in)>
// ------------------> __name__:
// __main__
// ------------------> __doc__:
// None
// ------------------> OUT:
// <debug.Message instance at 0xb7be784c>
// What is happening here?
Since the example is executed within the scope of the python interpreter there are some additional parameters printed. So the output is somewhat too verbose but there are a number of methods available to control the amount of output. UsageThe module provides two main methods of dealing with the amount of output:
Both have the same capabilities but it is more convenient to use the command line switches while debugging. Controlling debugging from within the code is usually only useful if the script or program does not accept any command line arguments. Debug Control from within the codeTo control the number debug statements that will actually yield output the following functions can be used from within the script. OUT.debug_on():: This will set the debug level to the default value of 4. OUT.set_debug_level(x):: Sets the debug level to x. OUT.debug_off():: This will set the debug level to 0. The debug level is a value ranging from 0 (no output) to 10 (maximal output). Every time you use the debug statement within your code you can specify the level at which this statement should still produce output:
OUT.debug('test', 7)
This statement would generate a message as long as the debugging level has been set to 7 or more. OUT.set_debug_verbosity(x):: Set the verbosity of the debugging output. This does not influence the selection of statements actually displayed. It only varies the length and complexity of each message. OUT.color_on():: Activates colored output. OUT.color_off():: Turn colors off. OUT.set_debug_methods(method_string):: A comma delimited string of method names to indicate the functions that should yield debug output. OUT.set_debug_classes(classes_string):: A comma delimited string of class names to indicate the classes that should yield debug output. OUT.set_debug_variables(variable_string):: A comma delimited string of variable names to indicate the variable values that should be included into the debug output. OUT.class_variables_off():: Class variable will not be displayed in the debugging messages. OUT.class_variables_on():: The values of class variables will be printed in addition to the local variables of the function. Debug Control with command line argumentsInstead of calling the control functions from within the script code it is far more convenient to control the debugging features by using command line switches. The only prerequisite for this is that the script or program needs to provide a handler for command line options, preferably the python optparse module. If that is the case one can use code similar to this in order to include the debugging options into the standard options provided by the program: parser = OptionParser() OUT.cli_opts(self.parser) This will add the debugging option group to the parser. It is no problem to add more options before or after the cli_opts call. To evaluate the switches used by the user of the program the debugging module provides a second method: (options, args) = parser.parse_args() OUT.cli_handle(options) The following command line switches are provided:
Additional functionsThere are a number of additional output functions beside the main debugging function. These are short convenience function to return some (colored) output to the user. The following are available:
Some of these functions (info, warn) allow to specify a level of significance like for the debug function. Back to Personal Wiki Back to Python |