Up until now, we've assumed that all of our lines processed are very well-formed and will never generate exceptions. In order to illustrate the use of the Template class, we'll fix that here. Under normal circumstances, it would probably be preferred to simply print an error just quietly pass by incorrectly formatted lines.
Using
logscan-f.py
as a starting place, createlogscan-g.py
. We'll use this as our starting point.At the top of the file, add
import string
to the list of modules imported.Immediately after the
docstring
forLogProcessor
, add the following code:tmpl = string.Template( 'line $line is malformed, raised $exc error: $error')
Replace the parse method in
LogProcessor
with the following new method:def parse(self, handle): """ Parses the logfile. Returns a dictionary composed of log entry values, for easy data summation. """ line_count = 0 for line in handle...