Re: altering an object as you iterate over it?

2006-05-22 Thread bruno at modulix
Paul McGuire wrote: > "Bruno Desthuilliers" <[EMAIL PROTECTED]> wrote in > message news:[EMAIL PROTECTED] > >>bruno at modulix a écrit : >>(snip) >> >>(responding to myself) >>(but under another identity - now that's a bit schizophrenic, isn't it ?-) >> > > > Do you ever flame yourself? class M

Re: altering an object as you iterate over it?

2006-05-20 Thread netvaibhav
bruno at modulix wrote: > fin = open(path, 'r') > fout = open(temp, 'w') > for line in fin: > if line.strip(): > fout.write(line) > fin.close() > fout.close() > > then delete path and rename temp, and you're done. And yes, this is > actually the canonical way to do this !-) What if there's a

Re: DO NOT USE file() (was Re: altering an object as you iterate over it?)

2006-05-19 Thread Tim Peters
[Tim Peters] >> In 2.5 `file` is unchanged but `open` becomes a function: >> >> >>> file >> >> >>> open >> [Paul Rubin] > So which one are we supposed to use? Use for what? If you're trying to check an object's type, use the type; if you're trying to open a file, use the function. >>> type(op

Re: DO NOT USE file() (was Re: altering an object as you iterate over it?)

2006-05-19 Thread Paul Rubin
"Tim Peters" <[EMAIL PROTECTED]> writes: > In 2.5 `file` is unchanged but `open` becomes a function: > > >>> file > > >>> open > So which one are we supposed to use? -- http://mail.python.org/mailman/listinfo/python-list

Re: DO NOT USE file() (was Re: altering an object as you iterate over it?)

2006-05-19 Thread John Salerno
Tim Peters wrote: > [John Salerno, on the difference between `open` and `file`] >> Interesting. What is the difference between them now? > > In 2.5 `file` is unchanged but `open` becomes a function: > file > open > So they are still used in the same way though? -- http://mail.python

Re: DO NOT USE file() (was Re: altering an object as you iterate over it?)

2006-05-19 Thread Tim Peters
[John Salerno, on the difference between `open` and `file`] > Interesting. What is the difference between them now? In 2.5 `file` is unchanged but `open` becomes a function: >>> file >>> open -- http://mail.python.org/mailman/listinfo/python-list

Re: altering an object as you iterate over it?

2006-05-19 Thread Paul McGuire
"Bruno Desthuilliers" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > bruno at modulix a écrit : > (snip) > > (responding to myself) > (but under another identity - now that's a bit schizophrenic, isn't it ?-) > Do you ever flame yourself? -- Paul -- http://mail.python.org/mailma

Re: DO NOT USE file() (was Re: altering an object as you iterate over it?)

2006-05-19 Thread John Salerno
Aahz wrote: > Python 2.5a2 (trunk:46052, May 19 2006, 19:54:46) > [GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. open is file > False > > Per the other comments in this thread, Guido agreed that mak

DO NOT USE file() (was Re: altering an object as you iterate over it?)

2006-05-19 Thread Aahz
In article <[EMAIL PROTECTED]>, James Stroud <[EMAIL PROTECTED]> wrote: >Paul McGuire wrote: >> >> 1. open("xxx") still works - not sure if it's even deprecated or not - but >> the new style is to use the file class > >Python 2.3.4 (#4, Oct 25 2004, 21:40:10) >[GCC 3.3.2 (Mandrake Linux 10.0 3.3.

Creating a new file object; open() vs file() (was: Re: altering an object as you iterate over it?)

2006-05-19 Thread Ben Finney
John Salerno <[EMAIL PROTECTED]> writes: > Paul McGuire wrote: > > > I think it is just part of the objectification trend - "f = > > open('xyzzy.dat')" is sort of a functional/verb concept, so it has > > to return something, and its something non-objecty like a file > > handle - urk! Instead, us

Re: altering an object as you iterate over it?

2006-05-19 Thread Max Erickson
Bruno Desthuilliers wrote > > It has been, at a time, recommended to use file() instead of > open(). Don't worry, open() is ok - and I guess almost anyone > uses it. http://mail.python.org/pipermail/python-dev/2005-December/059073.html -- http://mail.python.org/mailman/listinfo/python-list

Re: altering an object as you iterate over it?

2006-05-19 Thread Bruno Desthuilliers
bruno at modulix a écrit : (snip) (responding to myself) (but under another identity - now that's a bit schizophrenic, isn't it ?-) > For the general case, the best way to go would probably be an iterator: > > def iterfilter(fileObj): > for line in fileObj: > if line.strip(): > yield

Re: altering an object as you iterate over it?

2006-05-19 Thread Bruno Desthuilliers
John Salerno a écrit : > Paul McGuire wrote: > >> Your coding style is a little dated - are you using an old version of >> Python? This style is the old-fashioned way: > > > I'm sure it has more to do with the fact that I'm new to Python, but > what is old-fashioned about open()? It has been

Re: altering an object as you iterate over it?

2006-05-19 Thread Bruno Desthuilliers
John Salerno a écrit : > John Salerno wrote: > >> What is the best way of altering something (in my case, a file) while >> you are iterating over it? I've tried this before by accident and got >> an error, naturally. >> >> I'm trying to read the lines of a file and remove all the blank ones. >>

Re: altering an object as you iterate over it?

2006-05-19 Thread Richard Townsend
On Fri, 19 May 2006 13:36:35 -0700, James Stroud wrote: > Paul McGuire wrote: >> Your coding style is a little dated - are you using an old version of >> Python? This style is the old-fashioned way: > [clip] >> 1. open("xxx") still works - not sure if it's even deprecated or not - but >> the new

Re: altering an object as you iterate over it?

2006-05-19 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Paul McGuire wrote: > 1. open("xxx") still works - not sure if it's even deprecated or not - but > the new style is to use the file class It's not deprecated and may be still used for opening files. I guess the main reason for introducing `file` as a synonym was the possi

Re: altering an object as you iterate over it?

2006-05-19 Thread John Salerno
Paul McGuire wrote: > I think it is just part of the objectification trend - "f = > open('xyzzy.dat')" is sort of a functional/verb concept, so it has to return > something, and its something non-objecty like a file handle - urk! Instead, > using "f = file('xyzzy.dat')" is more of an object const

Re: altering an object as you iterate over it?

2006-05-19 Thread John Salerno
Paul McGuire wrote: >>> answer is - you are STILL UPDATING THE LIST YOUR ARE ITERATING OVER!!! >> Doh! I see that now! :) >> > > Sorry about the ALL CAPS... I think I got a little rant-ish in that last > post, didn't mean to shout. :) > > Thanks for being a good sport, Heh heh, actually it was

Re: altering an object as you iterate over it?

2006-05-19 Thread Paul McGuire
"John Salerno" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Paul McGuire wrote: > > > Your coding style is a little dated - are you using an old version of > > Python? This style is the old-fashioned way: > > I'm sure it has more to do with the fact that I'm new to Python, but > w

Re: altering an object as you iterate over it?

2006-05-19 Thread James Stroud
Paul McGuire wrote: > Your coding style is a little dated - are you using an old version of > Python? This style is the old-fashioned way: [clip] > 1. open("xxx") still works - not sure if it's even deprecated or not - but > the new style is to use the file class Python 2.3.4 (#4, Oct 25 2004, 2

Re: altering an object as you iterate over it?

2006-05-19 Thread John Salerno
Paul McGuire wrote: > Your coding style is a little dated - are you using an old version of > Python? This style is the old-fashioned way: I'm sure it has more to do with the fact that I'm new to Python, but what is old-fashioned about open()? Does file() do anything different? I know they are

Re: altering an object as you iterate over it?

2006-05-19 Thread Paul McGuire
"John Salerno" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > John Salerno wrote: > > What is the best way of altering something (in my case, a file) while > > you are iterating over it? I've tried this before by accident and got an > > error, naturally. > > > > I'm trying to read th

Re: altering an object as you iterate over it?

2006-05-19 Thread John Salerno
bruno at modulix wrote: > Now if what you want to do is just to rewrite the file without the blank > files, you need to use a second file: > > fin = open(path, 'r') > fout = open(temp, 'w') > for line in fin: > if line.strip(): > fout.write(line) > fin.close() > fout.close() > > then delet

Re: altering an object as you iterate over it?

2006-05-19 Thread bruno at modulix
John Salerno wrote: > What is the best way of altering something (in my case, a file) while > you are iterating over it? I've tried this before by accident and got an > error, naturally. > > I'm trying to read the lines of a file and remove all the blank ones. > One solution I tried is to open the

Re: altering an object as you iterate over it?

2006-05-19 Thread John Salerno
John Salerno wrote: > What is the best way of altering something (in my case, a file) while > you are iterating over it? I've tried this before by accident and got an > error, naturally. > > I'm trying to read the lines of a file and remove all the blank ones. > One solution I tried is to open

altering an object as you iterate over it?

2006-05-19 Thread John Salerno
What is the best way of altering something (in my case, a file) while you are iterating over it? I've tried this before by accident and got an error, naturally. I'm trying to read the lines of a file and remove all the blank ones. One solution I tried is to open the file and use readlines(), th