[Tutor] Introduction - log exercise

Antonio de la Fuente toni at muybien.org
Tue Nov 17 21:23:15 CET 2009


* Wayne Werner <waynejwerner at gmail.com> [2009-11-17 11:41:25 -0600]:

> Date: Tue, 17 Nov 2009 11:41:25 -0600
> From: Wayne Werner <waynejwerner at gmail.com>
> To: Antonio de la Fuente <toni at muybien.org>
> Cc: Python Tutor mailing list <tutor at python.org>
> Subject: Re: [Tutor] Introduction - log exercise
> Message-ID: <333efb450911170941g709e7ea3l4b4316044be0990d at mail.gmail.com>
> 
> On Tue, Nov 17, 2009 at 10:58 AM, Antonio de la Fuente <toni at muybien.org>wrote:
> 
> > Hi everybody,
> >
> > This is my first post here. I have started learning python and I am new to
> > programing, just some bash scripting, no much.
> > Thank you for the kind support and help that you provide in this list.
> >
> 
> You're welcome!
> 
> 
> >
> > 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.
> >
[...] 
> 
> Let me start out by saying this; I'm very impressed with the thought you've
> put into the problem and the way you've presented it.
> 

Thank you, it took me some time, but it did help me to understand
better the problem.

> The first thing that pops into my mind is to simply strip whitespace from
> the line and check if the line == ''. Upon further experiment there's the
> "isspace" method:
> 
> In [24]: x = '   \n\n\r\t\t'
> 
> In [25]: x.isspace()
> Out[25]: True
> 
> x contains a bunch of spaces, newlines, and tab chars. From the docs:
> 
> " Return True if all characters in S are whitespace
>     and there is at least one character in S, False otherwise."
> 

So, I could use it like this:
while not line.isspace()
      if line == 'foo':
           Somehow re-initialise myList 
	   break
	   [and the rest]

> 
> 
> > I don't expect the solution, as I think this is a great exercise to get wet
> > with python, but if anyone thinks that this is the wrong way of solving the
> > problem, please let me know.
> > <snip some code>
> > for line in fileIn:
> >    while line != 'blank_line':
> >        if line == 'foo':
> >            Somehow re-initialise myList
> >            break
> >        else:
> >            myList.append(line)
> >    fileOut.writelines(myList)
> 
>  <snip some more>
> 
> 
> Rather than using a while, you can use an if statement with the space method
> (and join your statement with an:
> 
> if not line.isspace() and not line == 'foo':
>     fileOut.write(line)
> 
But then, the new log file will have all the blocks, even the ones that
had 'foo' on it, even if the foo lines weren't there anymore. No? or
is there anything that I don't get?

> then you can get rid of the whole myList. Based on what you've written,
> there's really no need to have a list, it's more efficient to just write the
> line straight to the file.
My idea of using a list was that I can put the block in the list, and
if block does not content line with 'foo' then write it to file,
otherwise discard block.
> 
> for the renaming part, take a look at the shutil module.
> 
> HTH,
> Wayne
Thank you Wayne.
-- 
-----------------------------
Antonio de la Fuente Martínez
E-mail: toni at muybien.org
-----------------------------

Dios mira las manos limpias, no las llenas.
		-- Publio Siro. 


More information about the Tutor mailing list