Frankenstring

Thomas Lotze thomas at thomas-lotze.de
Wed Jul 13 06:56:48 EDT 2005


Bengt Richter wrote:

> ----< lotzefile.py >--------------------------------------------------

Thanks.

[...]
>         byte = self.buf[self.pos]

This is the place where the thing is basically a str whose items are
accessed as sequence elements. It has some iterator behaviour and file
management which makes it nice to use, of course, and to most this will
be enough (and it is a lot indeed). But it loses the efficiency of

for c in "asdf": do_something(c)

Actually, relying on string[index] behind the scenes is one of the ways
of implementing frankenstring I labelled "clumsy" in the original
posting ;o)

> I suspect you could get better performance if you made LotzeFile instances
> able to return interators over buffer chunks and get characters from them,
> which would be string iterators supplying the characters rather than the
> custom .next, but the buffer chunks would have to be of some size to make
> that pay. Testing is the only way to find out what the crossing point is,
> if you really have to.

If I understand this correctly, you'd have to switch to using a new iterator
after seeking, which would make this impossible:

f = LotzeFile('something')
for c in iter(f):
    do_something(c)
    if some_condition:
        f.seek(somewhere)
        # the next iteration reads from the new position

And it would break telling since the class can't know how many
characters have been read from an iterator once it returned one after
seeking or switching to another buffer chunk.

-- 
Thomas




More information about the Python-list mailing list