Mike C. Fletcher mcfletch at
Tue Jul 12 22:38:01 CEST 2005

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:
Okay, first off, this is never going to be *fast* compared to something
coded in C and wrapped with Python.  You are dealing with every single
character as a Python object, so let's forget fast for the moment and do
a straightforward implementation:

class Franken( str ):
    frankenIndex = 0
    def __iter__( self ):
        while self.frankenIndex < len(self):
            yield self[ self.frankenIndex ]
            self.frankenIndex += 1
        self.frankenIndex = 0
    def seek( self, index ):
        self.frankenIndex = index
    def tell( self, index ):
        return self.frankenIndex

if __name__ == "__main__":
    f = Franken( 'abcdefg' )
    for c in f:
        print 'char', c
        if c == 'c':
            break 5 )
    l1 = list( f )
    l2 = list( f )
    assert l1 == [ 'f','g' ]
    assert l2 == list(str(f))
    print 'first list', l1
    print 'second list', l2

If you want to speed it up, you can optimise for various string sizes
(eg using a slice of the string and the built-in iterator when
appropriate), but in the end, it's not going to be anywhere near as fast
as a C-engine tokeniser, so I'd personally spend more time on elegance
than on speed...

Anywho, have fun,

  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list