Aloha, Thomas Lotze wrote: > I think I need an iterator over a string of characters pulling them out > one by one, like a usual iterator over a str does. At the same time the > thing should allow seeking and telling like a file-like object: >>>>f = frankenstring("0123456789") >>>>for c in f: > ... print c > ... if c == "2": > ... break > ... > 0 > 1 > 2 >>>>f.tell() > 3L >>>>f.seek(7) >>>>for c in f: > ... print c > ... > 7 > 8 > 9 > I can think of more than one clumsy way to implement the desired > behaviour in Python; I'd rather like to know whether there's an > implementation somewhere that does it fast. (Yes, it's me and speed > considerations again; this is for a tokenizer at the core of a library, > and I'd really like it to be fast.)
You can already think my answer, because i'm doing this at the core of a similar library, but to give others the chance to discuss. >>> f = "0123456789" >>> p = 0 >>> t2 = f.find('2')+1 >>> for c in f[p:t2]: ... print c ... 0 1 2 >>> p = 7 >>> for c in f[p:]: ... print c ... 7 8 9 A string, and a pointer on that string. If you give up the boundary condition to tell backwards, you can start to eat up the string via f = f[p:]. There was a performance difference with that, in fact it was faster ~4% on a python2.2. I dont't expect any iterator solution to be faster than that. Wishing a happy day LOBI -- http://mail.python.org/mailman/listinfo/python-list