Basic misunderstanding of generators - resolved

Barak, Ron Ron.Barak at lsi.com
Mon Dec 22 05:16:16 EST 2008


Hi Chris,

Thanks for the super fast reply.

I tried your fix (with a slight modification, namely, I changed your line to be: line_ = generator.next())

And I got the printout I expected.

Many thanks,
Ron.

P.S.: My program looks like so, with your suggestion:

$ cat LogManager_try.py
#!/usr/bin/env python

import gzip
import os

class LogStream():
    """
    """

    def __init__(self, filename):
        self.filename = filename
        self.input_file = self.open_file(filename)

    def open_file(self, in_file):
        """
        The gzip module checks if the input file is a gzipped file, only at the read stage.
        This is why the f.readline() is needed.
        """
        try:
            f = gzip.GzipFile(in_file, "r")
            f.readline()
        except IOError:
            f = open(in_file, "r")
            f.readline()

        f.seek(0)
        return(f)

    def next_line(self, in_file):
        """
        """
        for line_ in in_file:
            yield line_.strip()

if __name__ == "__main__":
    filename = "sac.log.gz"
    log_stream = LogStream(filename)
    generator = log_stream.next_line(log_stream.input_file) #create generator
    line_ = generator.next() #get next item from generator
    print line_



-----Original Message-----
From: cvrebert at gmail.com [mailto:cvrebert at gmail.com] On Behalf Of Chris Rebert
Sent: Monday, December 22, 2008 11:53
To: Barak, Ron
Cc: python-list at python.org
Subject: Re: Basic misunderstanding of generators

On Mon, Dec 22, 2008 at 1:47 AM, Barak, Ron wrote:
> Hi All,
>
> I want to use generators to print lines taken from a gzipped file.
> I've never used generators, so probably my problem is basic
> misunderstanding of generators.
>
> In the below program, I expected the last line ("print line_") to
> print the first line of the sac.log.gz file.
> Instead, I get:
>
> <generator object at 0x00B93A08>
>
> Could you tell me what I'm doing wrong (or point me to a URL that
> could set me straight) ?
>
> Thanks,
> Ron.
>
>
> $ cat LogManager_try.py
> #!/usr/bin/env python
>
> import gzip
> import os
>
> class LogStream():
>     """
>     """
>
>     def __init__(self, filename):
>         self.filename = filename
>         self.input_file = self.open_file(filename)
>
>     def open_file(self, in_file):
>         """
>         The gzip module checks if the input file is a gzipped file,
> only at the read stage.
>         This is why the f.readline() is needed.
>         """
>         try:
>             f = gzip.GzipFile(in_file, "r")
>             f.readline()
>         except IOError:
>             f = open(in_file, "r")
>             f.readline()
>
>         f.seek(0)
>         return(f)
>
>     def next_line(self, in_file):
>         """
>         """
>         for line_ in in_file:
>             yield line_.strip()
>
> if __name__ == "__main__":
>     filename = "sac.log.gz"
>     log_stream = LogStream(filename)
     generator = log_stream.next_line(log_stream.input_file) #create generator
     line_ = generator() #get next item from generator
     print line_

And as you can see, this makes next_line a bit of a misnomer.

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081222/aa97b291/attachment-0001.html>


More information about the Python-list mailing list