[Tutor] Logfile multiplexing

Hugo Arts hugo.yoshi at gmail.com
Tue Nov 10 14:35:16 CET 2009


On Tue, Nov 10, 2009 at 2:25 PM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:
><snip>
>
>From here I get:
>
> import gzip
>
> class LogFile:
>    def __init__(self, filename, date):
>        self.logfile = gzip.open(filename, 'r')
>        self.date = date
>
>    def __iter__(self):
>        for logline in self.logfile:
>            stamp = self.timestamp(logline)
>            if stamp.startswith(date):
>                yield (stamp, logline)
>
>    def timestamp(self, line):
>        return " ".join(self.line.split()[3:5])
>
> l = LogFile("/home/stephen/access_log-20091105.gz", "[04/Nov/2009")
>
> I get:
>
> Python 2.4.3 (#1, Jan 21 2009, 01:11:33)
> [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import kent
>>>> kent.l
> <kent.LogFile instance at 0x2afb05142bd8>
>>>> dir(kent.l)
> ['__doc__', '__init__', '__iter__', '__module__', 'date', 'logfile',
> 'timestamp']
>>>> for line in kent.l:
> ...   print line
> ...
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>  File "kent.py", line 10, in __iter__
>    stamp = self.timestamp(logline)
>  File "kent.py", line 15, in timestamp
>    return " ".join(self.line.split()[3:5])
> AttributeError: LogFile instance has no attribute 'line'
>>>> for stamp,line in kent.l:
> ...   print stamp,line
> ...
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>  File "kent.py", line 10, in __iter__
>    stamp = self.timestamp(logline)
>  File "kent.py", line 15, in timestamp
>    return " ".join(self.line.split()[3:5])
> AttributeError: LogFile instance has no attribute 'line'
>>>> for stamp,logline in kent.l:
> ...   print stamp,logline
> ...
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>  File "kent.py", line 10, in __iter__
>    stamp = self.timestamp(logline)
>  File "kent.py", line 15, in timestamp
>    return " ".join(self.line.split()[3:5])
> AttributeError: LogFile instance has no attribute 'line'
>

probably that line should have been " ".join(line.split()[3:5]), i.e.
no self. The line variable is a supplied argument.

Hugo


More information about the Tutor mailing list