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(object):
    def __init__(self, file):
        self.file = file
    def __iter__(self):
        while True:
            l = self.file.readline()
            if not l: return
            yield l

if __name__ == '__main__':
    f = Follower(open("/var/log/messages"))
    while True:
        for i in f: print i,
        print "foo"
        import time
        time.sleep(4)

Here, you iterate over the object until it is exhausted, but you can iterate again to get the next entries.


Thomas
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to