Frankenstring

Roland Heiber newstonne at web.de
Wed Jul 13 07:30:25 EDT 2005


Thomas Lotze wrote:
> AIUI (and as a little experimenting seems to confirm), you can't
> reposition an iterator over an mmap'ed file by seeking. True, you have
> both iterating by characters and seeking/telling, but the two
> functionalities don't play together.

A quick and dirty hack!? Maybe i'm missing what it is that you need ...

class MmapWithSeekAndTell(object):
     def __init__(self, m, size):
         self._m = m
         self._pos = 0
         self._size = size-1

     def __iter__(self):
         return self

     def next(self):
         if self._pos < self._size-1:
             self._pos += 1
             return self._m[self._pos]
         raise StopIteration

     def seek(self, offset, whence=0):
         if whence == 0 and 0 <= offset < self._size:
             self._pos = offset
         elif whence == 1 and 0 <= (self._pos + offset) < self._size:
             self._pos += offset
         elif whence == 2 and 0<= (self._size - offset) < self._size:
             self._pos = self._size - offset

     def tell(self):
         return self._pos


HtH, Roland




More information about the Python-list mailing list