Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread Akira Li
Ned Batchelder writes: > On 11/28/14 10:22 AM, Dave Angel wrote: >> On 11/28/2014 10:04 AM, fetchinson . wrote: >>> Hi all, >>> >>> I have a feeling that I should solve this by a context manager but >>> since I've never used them I'm not sure what the optimal (in the >>> python sense) solution is

Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread Michael Torrie
On 11/28/2014 08:04 AM, fetchinson . wrote: > Hi all, > > I have a feeling that I should solve this by a context manager but > since I've never used them I'm not sure what the optimal (in the > python sense) solution is. So basically what I do all the time is > this: I'd personally do it with a g

Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread Marko Rauhamaa
Chris Angelico : > Since we're so good at it on this list, I will nit-pick: that's not a > context manager class, that's an iterator class. A context manager has > __enter__ and __exit__, an iterator has __iter__ (returning self) and > __next__ (returning or raising StopIteration). Excellent. Lea

Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread Chris Angelico
On Sat, Nov 29, 2014 at 4:55 AM, Marko Rauhamaa wrote: > I think the OP can learn from the comparison. One question, many > lessons: > > * Here's how you write a generator. > > * Here's how you write a context manager class. You run into those >quite often as well. > > * See how much more s

Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread Marko Rauhamaa
Dave Angel : > Why would you prefer that over a generator function, as given earlier > in the thread? See for example Ned's message. By using 'yield', you > get Python to generate all the class boilerplate for you. I think the OP can learn from the comparison. One question, many lessons: * Here

Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread Dave Angel
Please don't start a new thread when responding to an existing topic. Just reply-List to the message you're responding to, and include some context from that message. On 11/28/2014 12:06 PM, ast wrote: Hi Here is a solution with a custom iterator which operate on files. I tested it with a sma

Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread ast
Hi Here is a solution with a custom iterator which operate on files. I tested it with a small file. Just a remark, there are no empty line on a file, there is at least '\n' at the end of each lines but maybe the last one. If readline() got an emptyline, then the end of file has been reached.

Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread Dave Angel
On 11/28/2014 11:01 AM, Ned Batchelder wrote: On 11/28/14 10:22 AM, Dave Angel wrote: On 11/28/2014 10:04 AM, fetchinson . wrote: Hi all, I have a feeling that I should solve this by a context manager but since I've never used them I'm not sure what the optimal (in the python sense) solution i

Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread Ned Batchelder
On 11/28/14 10:22 AM, Dave Angel wrote: On 11/28/2014 10:04 AM, fetchinson . wrote: Hi all, I have a feeling that I should solve this by a context manager but since I've never used them I'm not sure what the optimal (in the python sense) solution is. So basically what I do all the time is this:

Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread fetchinson .
On 11/28/14, Dave Angel wrote: > On 11/28/2014 10:04 AM, fetchinson . wrote: >> Hi all, >> >> I have a feeling that I should solve this by a context manager but >> since I've never used them I'm not sure what the optimal (in the >> python sense) solution is. So basically what I do all the time is

Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread Dave Angel
On 11/28/2014 10:04 AM, fetchinson . wrote: Hi all, I have a feeling that I should solve this by a context manager but since I've never used them I'm not sure what the optimal (in the python sense) solution is. So basically what I do all the time is this: for line in open( 'myfile' ): if n

Iterate over text file, discarding some lines via context manager

2014-11-28 Thread fetchinson .
Hi all, I have a feeling that I should solve this by a context manager but since I've never used them I'm not sure what the optimal (in the python sense) solution is. So basically what I do all the time is this: for line in open( 'myfile' ): if not line: # discard empty lines