<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.3462" name=GENERATOR></HEAD>
<BODY>
<DIV><SPAN class=263372909-22122008><FONT face=Arial color=#0000ff>Hi
All,</FONT></SPAN></DIV>
<DIV><SPAN class=263372909-22122008><FONT face=Arial
color=#0000ff></FONT></SPAN> </DIV>
<DIV><SPAN class=263372909-22122008><FONT face=Arial color=#0000ff>I want to use
generators to print lines taken from a gzipped file.</FONT></SPAN></DIV>
<DIV><SPAN class=263372909-22122008><FONT face=Arial color=#0000ff>I've never
used generators, so probably my problem is basic misunderstanding of
generators.</FONT></SPAN></DIV>
<DIV><SPAN class=263372909-22122008><FONT face=Arial
color=#0000ff></FONT></SPAN> </DIV>
<DIV><SPAN class=263372909-22122008><FONT face=Arial color=#0000ff>In the below
program, I expected the last line ("print line_") to print the first line of the
sac.log.gz file.<BR>Instead, I get:</FONT></SPAN></DIV>
<DIV><SPAN class=263372909-22122008><FONT face=Arial
color=#0000ff></FONT></SPAN> </DIV>
<DIV><SPAN class=263372909-22122008><FONT color=#0000ff><generator object at
0x00B93A08></FONT></SPAN></DIV>
<DIV><SPAN class=263372909-22122008><FONT face=Arial
color=#0000ff></FONT></SPAN> </DIV>
<DIV><SPAN class=263372909-22122008><FONT face=Arial color=#0000ff>Could you
tell me what I'm doing wrong (or point me to a URL that could set me straight)
?</FONT></SPAN></DIV>
<DIV><SPAN class=263372909-22122008><FONT face=Arial
color=#0000ff></FONT></SPAN> </DIV>
<DIV><SPAN class=263372909-22122008><FONT face=Arial
color=#0000ff>Thanks,<BR>Ron.</FONT></SPAN></DIV>
<DIV><FONT face=Arial color=#0000ff></FONT> </DIV>
<DIV class=Section1>
<P class=MsoNormal dir=ltr
style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial"><FONT
face="Courier New">$ <STRONG>cat LogManager_try.py<BR></STRONG>#!/usr/bin/env
python</FONT></SPAN></P>
<P class=MsoNormal dir=ltr
style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial"><FONT
face="Courier New">import gzip<BR>import os</FONT></SPAN></P>
<P class=MsoNormal dir=ltr
style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial"><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></SPAN></P>
<P class=MsoNormal dir=ltr
style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial"><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></SPAN></P>
<P class=MsoNormal dir=ltr
style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial"><FONT
face="Courier New">
f.seek(0)<BR>
return(f)</FONT></SPAN></P>
<P class=MsoNormal dir=ltr
style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial"><FONT
face="Courier New"> def next_line(self,
in_file):<BR>
"""<BR>
"""<BR> for line_ in
in_file:<BR>
yield line_.strip()</FONT></SPAN></P><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial">
<P class=MsoNormal dir=ltr
style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><FONT
face="Courier New">if __name__ == "__main__":<BR> filename =
"sac.log.gz"<BR> log_stream =
LogStream(filename)<BR> line_ =
log_stream.next_line(log_stream.input_file)<BR> print
line_</FONT></P>
<P class=MsoNormal dir=ltr
style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><FONT
face="Courier New">$ <STRONG>python LogManager_try.py<BR></STRONG><generator
object at 0x00B94648></FONT></P>
<P class=MsoNormal dir=ltr
style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><FONT
face="Courier New">$ </FONT></SPAN></P><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial">
<P class=MsoNormal dir=ltr
style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"
align=left></SPAN> </P>
<P class=MsoNormal dir=ltr
style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial">--<BR>Ron <SPAN
class=SpellE>Barak</SPAN>, System Development
Engineer </SPAN><BR><STRONG><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">LSI Technologies Israel
Ltd</SPAN></STRONG><B><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial"><BR></SPAN></B><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial">63 Bar <SPAN
class=SpellE>Yehuda</SPAN> Road, <SPAN class=SpellE>Nesher</SPAN>
<?XML:NAMESPACE PREFIX = U1 /><U1:POSTALCODE u2:st="on">36651</U1:POSTALCODE>
<U1:COUNTRY-REGION u2:st="on"><U1:COUNTRY-REGION u3:st="on"><U1:PLACE
u3:st="on"><?XML:NAMESPACE PREFIX = ST1 /><ST1:COUNTRY-REGION
u1:st="on"><ST1:PLACE u1:st="on"><st1:country-region u4:st="on"><st1:place
u4:st="on">Israel</st1:place></st1:country-region></U1:PLACE></U1:COUNTRY-REGION></U1:COUNTRY-REGION></ST1:COUNTRY-REGION></SPAN><BR><SPAN
style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Arial">Tel: (+972) 4 8203454
x1542 Fax: (+972) 4 8203464</SPAN><?xml:namespace prefix = o ns =
"urn:schemas-microsoft-com:office:office" /><o:p></o:p></P><?XML:NAMESPACE
PREFIX = U4 /><U4:P></U4:P><IMG height=39 src="cid:263372909@22122008-2BEE"
width=108 v:shapes="_x0000_i1026" u3:shapes="_x0000_i1025"
u5:shapes="_x0000_i1025"><IMG height=21 alt=Ron.Barak@LSI.com
src="cid:263372909@22122008-2BF5" width=142 v:shapes="_x0000_i1025"><SPAN
style="FONT-SIZE: 10pt; COLOR: silver">
<ADDRESS><BR></ADDRESS>
<ADDRESS>
<P><FONT size=2>Remember, Ginger Rogers did everything Fred Astaire did, but
backwards and in high heels. (Faith
Whittlesey)</FONT></P></ADDRESS></SPAN></DIV></ST1:PLACE>
<DIV> </DIV></BODY></HTML>