Re: How to write a file generator

2011-07-12 Thread Thomas Rachel
Am 12.07.2011 16:46 schrieb Billy Mays: I want to make a generator that will return lines from the tail of /var/log/syslog if there are any, but my function is reopening the file each call ... I have another solution: an object which is not an iterator, but an iterable. class Follower(objec

Re: How to write a file generator

2011-07-12 Thread Thomas Jollans
On 07/12/2011 06:42 PM, Billy Mays wrote: > On 07/12/2011 11:52 AM, Thomas Jollans wrote: >> On 07/12/2011 04:46 PM, Billy Mays wrote: >>> I want to make a generator that will return lines from the tail of >>> /var/log/syslog if there are any, but my function is reopening the file >>> each call: >>

Re: How to write a file generator

2011-07-12 Thread Thomas Jollans
On 07/12/2011 06:42 PM, Billy Mays wrote: > On 07/12/2011 11:52 AM, Thomas Jollans wrote: >> On 07/12/2011 04:46 PM, Billy Mays wrote: >>> I want to make a generator that will return lines from the tail of >>> /var/log/syslog if there are any, but my function is reopening the file >>> each call: >>

Re: How to write a file generator

2011-07-12 Thread Terry Reedy
On 7/12/2011 10:46 AM, Billy Mays wrote: I want to make a generator that will return lines from the tail of /var/log/syslog if there are any, but my function is reopening the file each call: def getLines(): with open('/var/log/syslog', 'rb') as f: while True: line = f.readline() if line: yield l

Re: How to write a file generator

2011-07-12 Thread sturlamolden
On 12 Jul, 16:46, Billy Mays wrote: > I know the problem lies with the StopIteration, but I'm not sure how to > tell the caller that there are no more lines for now. Try 'yield None' instead of 'raise StopIteration'. Sturla -- http://mail.python.org/mailman/listinfo/python-list

Re: How to write a file generator

2011-07-12 Thread Billy Mays
On 07/12/2011 11:52 AM, Thomas Jollans wrote: On 07/12/2011 04:46 PM, Billy Mays wrote: I want to make a generator that will return lines from the tail of /var/log/syslog if there are any, but my function is reopening the file each call: def getLines(): with open('/var/log/syslog', 'rb') a

Re: How to write a file generator

2011-07-12 Thread Thomas Jollans
On 07/12/2011 04:46 PM, Billy Mays wrote: > I want to make a generator that will return lines from the tail of > /var/log/syslog if there are any, but my function is reopening the file > each call: > > def getLines(): > with open('/var/log/syslog', 'rb') as f: > while True: >

Re: How to write a file generator

2011-07-12 Thread bruno.desthuilli...@gmail.com
On Jul 12, 4:46 pm, Billy Mays wrote: > I want to make a generator that will return lines from the tail of > /var/log/syslog if there are any Err... I must have missed something, but python files are their own iterators. Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2 Type

How to write a file generator

2011-07-12 Thread Billy Mays
I want to make a generator that will return lines from the tail of /var/log/syslog if there are any, but my function is reopening the file each call: def getLines(): with open('/var/log/syslog', 'rb') as f: while True: line = f.readline() if line: