[Tutor] Introduction - log exercise

Antonio de la Fuente toni at muybien.org
Wed Nov 18 01:31:56 CET 2009


* Dave Angel <davea at ieee.org> [2009-11-17 16:30:43 -0500]:

> Date: Tue, 17 Nov 2009 16:30:43 -0500
> From: Dave Angel <davea at ieee.org>
> To: Antonio de la Fuente <toni at muybien.org>
> CC: Python Tutor mailing list <tutor at python.org>
> Subject: Re: [Tutor] Introduction - log exercise
> User-Agent: Thunderbird 2.0.0.23 (Windows/20090812)
> Message-ID: <4B031603.4000306 at ieee.org>
> 
> Antonio de la Fuente wrote:
> >Hi everybody,
> >

[...] 

> >This is my problem: I've got a log file that is filling up very quickly, this
> >log file is made of blocks separated by a blank line, inside these blocks there
> >is a line "foo", I want to discard blocks with that line inside it, and create a
> >new log file, without those blocks, that will reduce drastically the size of the
> >log file.
> >

[...] 

> You've got some good ideas, and I'm going to give you hints, rather
> than just writing it for you, as you suggested.
> 
Much appreciated, really.

> First, let me point out that there are advanced features in Python
> that could make a simple program that'd be very hard for a beginner
> to understand.  I'll give you the words, but recommend that you not
> try it at this time.  If you were to wrap the file in a generator
> that returned you a "paragraph" at a time, the same way as it's now
> returning a line at a time, then the loop would be simply a for-loop
> on that generator, checking each paragraph for whether it contained
> "foo" and if so, writing it to the output.
> 
> 
> But you can also do it without using advanced features, and that's
> what I'm going to try to outline.
> 

[...] 

> Inside the for loop, if the line is non-empty, add it to the
> paragraph.  If the line is empty, then print the paragraph (with
> something before and after it,
> so you can see what came from each print stmt).  Then blank it
> (outlist = []).
> Check whether this result looks good, and if so, continue on.

for line in fileIn:                                                                                      
    if line.isspace():                                                                                   
        print "***** blank line ****"                                                                    
        print myList                                                                                     
	print "***** fancy blank line ****"
        myList = []                                                                                      
    else:                                                                                                
        myList.append(line)       

I think is what i expect, but confuse me that is in this format:

['Tue Nov 17 16:11:47 GMT 2009\n'], '\tbladi bladi bla', '\ttarila ri la\n', '\tpatatin pataton\n', '\ttatati tatata\n', '\tfoo\n']
***** fancy blank line ****
***** blank line ****

with linefeeds and tabs all over, I see why everybody calls it
paragraph.
Once I write to a file from the list, it will comeback the initial
format of the file?

> 
> Next version of the code: whenever you have a non-blank line, in
> addition to adding it to the list, also check it for whether it's
> equal-foo.
> If so, set a flag.  When printing the outlist, skip the printing if
> the flag is set.  Remember that you'll have to clear this flag each
> time you blank
> the mylist, both before the loop, and in the middle of the loop.

I am a bit lost with the flags, is it what Bob Gailer was calling keep =
True, keep = False, right?

> 
> Once this makes sense, you can worry about actually writing the
> output to a real file, maybe compressing it, maybe doing deletes and
> renames
> as appropriate.   You probably don't need shutil module,  os module
> probably has enough functions for this.
> 
> At any of these stages, if you get stuck, call for help.  But your
> code will be only as complex as that stage needs, so we can find one
> bug at a time.
> 
> DaveA
> 
Thank you, it has been very helpful.

-- 
-----------------------------
Antonio de la Fuente Martínez
E-mail: toni at muybien.org
-----------------------------

Power, like a desolating pestilence,
Pollutes whate'er it touches...
		-- Percy Bysshe Shelley


More information about the Tutor mailing list