Basic misunderstanding of generators

Chris Rebert clp at rebertia.com
Mon Dec 22 04:53:28 EST 2008


On Mon, Dec 22, 2008 at 1:47 AM, Barak, Ron <Ron.Barak at lsi.com> 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



More information about the Python-list mailing list