<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE></TITLE>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.3462" name=GENERATOR></HEAD>
<BODY><!-- Converted from text/plain format -->
<P><FONT color=#0000ff><FONT size=2>Hi Chris,<BR><BR>Thanks for the super fast
reply.<BR><BR>I tried your fix<SPAN class=691350810-22122008> (with a slight
modification, namely, </SPAN></FONT><FONT size=2><SPAN
class=691350810-22122008>I changed your line to be: </SPAN></FONT></FONT><SPAN
class=691350810-22122008><FONT face="Courier New" size=1>line_ =
generator.next()<FONT color=#0000ff>)</FONT></FONT></SPAN></P>
<P><SPAN class=691350810-22122008><FONT color=#0000ff size=2>And I got the
printout I expected.</FONT></SPAN></P>
<P><FONT color=#0000ff><SPAN class=691350810-22122008><FONT size=2>Many
thanks,<BR></FONT></SPAN><SPAN class=691350810-22122008><FONT
size=2>Ron.</FONT></SPAN></FONT></P>
<P><FONT size=2><FONT color=#0000ff><SPAN class=691350810-22122008>P.S.:
</SPAN>My program looks like so, with your
suggestion:</FONT><BR><BR></FONT><FONT face="Courier New" size=2>$ cat
LogManager_try.py<BR>#!/usr/bin/env python</FONT></P>
<P><FONT size=2><FONT face="Courier New">import gzip<BR>import
os</FONT></FONT></P>
<P><FONT size=2><FONT face="Courier New">class
LogStream():<BR> """<BR>
"""<BR> <BR> def __init__(self,
filename):<BR> self.filename =
filename<BR> self.input_file =
self.open_file(filename)</FONT></FONT></P>
<P><FONT size=2><FONT face="Courier New"> def open_file(self,
in_file):<BR>
"""<BR> The gzip module checks if the
input file is a gzipped file, only at the read
stage.<BR> This is why the
f.readline() is needed.<BR>
"""<BR>
try:<BR> f =
gzip.GzipFile(in_file,
"r")<BR>
f.readline()<BR> except
IOError:<BR> f
= open(in_file,
"r")<BR>
f.readline()</FONT></FONT></P>
<P><FONT size=2><FONT
face="Courier New">
f.seek(0)<BR>
return(f)</FONT></FONT></P>
<P><FONT size=2><FONT face="Courier New"> def next_line(self,
in_file):<BR>
"""<BR>
"""<BR> for line_ in
in_file:<BR>
yield line_.strip()</FONT></FONT></P>
<P><FONT size=2><FONT face="Courier New"><BR>if __name__ ==
"__main__":<BR> filename = "sac.log.gz"<BR>
log_stream = LogStream(filename)<BR> generator =
log_stream.next_line(log_stream.input_file) #create
generator<BR> line_ = generator.next() #get next item from
generator<BR> print line_<BR></FONT><BR><BR><BR>-----Original
Message-----<BR>From: cvrebert@gmail.com [<A
href="mailto:cvrebert@gmail.com">mailto:cvrebert@gmail.com</A>] On Behalf Of
Chris Rebert<BR>Sent: Monday, December 22, 2008 11:53<BR>To: Barak, Ron<BR>Cc:
python-list@python.org<BR>Subject: Re: Basic misunderstanding of
generators<BR><BR>On Mon, Dec 22, 2008 at 1:47 AM, Barak, Ron wrote:<BR>> Hi
All,<BR>><BR>> I want to use generators to print lines taken from a
gzipped file.<BR>> I've never used generators, so probably my problem is
basic<BR>> misunderstanding of generators.<BR>><BR>> In the below
program, I expected the last line ("print line_") to<BR>> print the first
line of the sac.log.gz file.<BR>> Instead, I get:<BR>><BR>>
<generator object at 0x00B93A08><BR>><BR>> Could you tell me what
I'm doing wrong (or point me to a URL that<BR>> could set me straight)
?<BR>><BR>> Thanks,<BR>> Ron.<BR>><BR>><BR>> $ cat
LogManager_try.py<BR>> #!/usr/bin/env python<BR>><BR>> import
gzip<BR>> import os<BR>><BR>> class
LogStream():<BR>> """<BR>>
"""<BR>><BR>> def __init__(self,
filename):<BR>> self.filename
= filename<BR>>
self.input_file =
self.open_file(filename)<BR>><BR>> def
open_file(self,
in_file):<BR>>
"""<BR>> The gzip module
checks if the input file is a gzipped file,<BR>> only at the read
stage.<BR>> This is why the
f.readline() is needed.<BR>>
"""<BR>>
try:<BR>>
f = gzip.GzipFile(in_file,
"r")<BR>>
f.readline()<BR>> except
IOError:<BR>>
f = open(in_file,
"r")<BR>>
f.readline()<BR>><BR>>
f.seek(0)<BR>>
return(f)<BR>><BR>> def next_line(self,
in_file):<BR>>
"""<BR>>
"""<BR>> for line_ in
in_file:<BR>>
yield line_.strip()<BR>><BR>> if __name__ ==
"__main__":<BR>> filename =
"sac.log.gz"<BR>> log_stream =
LogStream(filename)<BR> generator =
log_stream.next_line(log_stream.input_file) #create
generator<BR> line_ = generator() #get next item from
generator<BR> print line_<BR><BR>And as you can see,
this makes next_line a bit of a
misnomer.<BR><BR>Cheers,<BR>Chris<BR><BR>--<BR>Follow the path of the
Iguana...<BR><A
href="http://rebertia.com">http://rebertia.com</A><BR></P></FONT></BODY></HTML>