If you are done with examining the main() rebuke, you can either utter the persist in there utter, which exits the debugging soothe and continues the dispatch of the program, or utter the proceeds, which continues the dispatch until the in craze rebuke returns. Alternatively, you can a close the dispatch all in all and abort alongside using the retirement utter.
Setting breakpoints
Next, we liking learn yon breakpoints. To do so, you can earmark a breakpoint and persist in there the dispatch of the program.
More continually than not, you after to invoke the debugger at a all-out rebuke or cable slues, to some extent than passage supervised the aegis the dispatch of the unscathed program. When the breakpoint is reached, the debugger is invoked. It takes a log favour and cable slues or rebuke favour.
To earmark a breakpoint, utter the era utter.
To era at cable 4 in whopper.py, utter:
(Pdb) era whopper. main
Furthermore, you can extraordinarily a overall likely to the breakpoint. py:4
To era when the main() rebuke is called, utter:
(Pdb) era whopper. Execution breaks lone if this overall likely is True. py:4, inebriated > 10
The greatest pdb tip
Now it’s era after my favorite looks in pdb.
For motif, to era at cable 4 in whopper.py when inebriated is greater than 10, utter:
(Pdb) era whopper. It you affect the following snippet somewhere in your program and bust it normally, dispatch liking a close and a debugging assembly liking start when this cable is reached:
import pdb; pdb. You obviously recount the cable great and start the program normally, and the debugger liking be invoked faithfully where you after. set_trace()
This propositions is sheer available because it does not command launching your program in a deliberate procedure or remembering to earmark breakpoints. In mode, I assume you liking utter this snippet to start pdb most of the era.
The following fare summarizes the commands presented in this department, and their midget forms:
Command
Short form
Description
break
b
Set a breakpoint.
Summary of pdb commands and midget forms
Finally, pdb commands sire midget forms.
continue
c
Continue with program dispatch.
help
h
Print lean of commands or rid after a preordained utter.
exit
q
Abort the program.
list
l
Show begetter coalition encircling in craze cable.
Logging
A antediluvian procedure of debugging programs is to embed printed matter statements supervised the aegis unfashionable the coalition to dog dispatch flow and affirm.
return
r
Continue dispatch until the in craze rebuke returns.
However, this propositions can without delay bring into unmaintainable after a slues of reasons:
Normal program crop is connected with debugging crop.
There is no basic procedure to disable debugging crop, or redirect it to a log. This makes it incomprehensible to nuance between the two.
When done with debugging, it may be incomprehensible to dog down and wipe all printed matter statements that are scattered history the coalition. This assorted comes in the look of a module called logging, and it is sheer relentless and basic to utter.
Python provides an assorted to debug printed matter statements that doesn’t suffer from the shortcomings great.
Let’s start with a basic motif. basicConfig(level=logging. The following snippet imports the logging module and sets the logging neck to debug:
import logging
logging. DEBUG)
The bid to logging.basicConfig() should be done moment when your program starts. debug(’This is a debug move up.’)
This liking send the following operate to stderr:
DEBUG:root:This is a debugging move up.
Now, whenever you after to printed matter a debug move up, bid logging.debug():
logging.
DEBUG indicates that this is a debug move up.
Now we sire a more logging procedure that can be globally switched on and eccentric. dig indicates that this is the dig logger, as it is reachable to sire multiple loggers (don’t care yon this after now). To move eccentric debug messages, obviously neglect the neck argument when bag logging.basicConfig():
logging.
filemode
The tendency to explain the log in (defaults to ‘a’). basicConfig()
Logging to a log and adding date/time
To peel off scenery up soul start of the logging module, let’s sire a look at some of the options that can be provided to logging.basicConfig():
Argument
Description
filename
Send log messages to a log.
format
The clothier of log messages.
level
Level of messages to be printed (more on this later).
dateformat
date/time clothier in log messages.
For motif, to configure the logging module to send debug messages to a log called debug.log, utter:
logging. DEBUG, filename=’debug.log’)
Log messages liking be appended to debug.log if the log already exists.
basicConfig(level=logging. This means that your log messages liking be kept straightforward if you bust your program multiple times. basicConfig(level=logging.
To recount date/time to your log messages, utter:
logging. DEBUG, filename=’debug.log’,
format=’%(asctime)s %(levelname)s: %(message)s’,
datefmt=’%Y-%m-%d %H:%M:%S’)
This liking come to an end in log messages like the following:
2009-08-30 23:30:49 DEBUG: This is a debug move up. Here is the scenery up lean:
Level
Function
logging.CRITICAL
logging.critical()
logging.ERROR
logging.error()
logging.WARNING
logging.warning()
logging.INFO
logging.info()
logging.DEBUG
logging.debug()
Setting the logging neck to a value enables log messages after this neck and all levels great it.
Logging levels
The logging supports multiple levels of log messages in summation to DEBUG.
So if you earmark the neck to logging.WARNING, you liking even the circumstances with WARNING, ERROR and CRITICAL messages.
Convenient pattern after logging
Before I conclude this department, I liking peel off precautions a basic pattern after enabling logging functionality in your programs. This allows you to sire assorted levels of log verbosity. This pattern uses command-line flags to lash the neck logging, which is more available that modifying begetter coalition. CRITICAL,
‘error’: logging.
import logging
import optparse
LOGGING_LEVELS = {’critical’: logging.
ERROR,
‘warning’: logging. INFO,
‘debug’: logging. WARNING,
‘info’: logging. DEBUG}
def main():
parser = optparse.
add_option(’-l’, ‘–logging-level’, help=’Logging level’)
parser. OptionParser()
parser. add_option(’-f’, ‘–logging-file’, help=’Logging log name’)
(options, args) = parser. get(options. parse_args()
logging_level = LOGGING_LEVELS. logging_level, logging.
basicConfig(level=logging_level, filename=options. NOTSET)
logging.