ng extensions
in C) is give the evolving FrankenString some search methods that enable
searching for the first occurrence in the string of any character out of
a set of characters given as a string, or any character not in such a
set. This has nothing to do yet with iterators and seeking/telling.
well that appears to have been munged up ...
that tell() belongs immediately after self.str. making it
self.str.tell()
class FrankenString:
def __init__(self,string=None):
self.str = StringIO(string)
self.atEnd = False
self.lastidx = 0
self.seek = self.str.seek
Here is a cStringIO based version:
class FrankenString:
def __init__(self,string=None):
self.str = StringIO(string)
self.atEnd = False
self.lastidx = 0
self.seek = self.str.seek
self.tell = self.str.tell
import StringIO
class FrankenString(StringIO):
lastidx = 0
atEnd = False
def __iter__(self):
while not self.atEnd:
char = self.read(1)
idx = self.tell()
if self
Thomas Lotze wrote:
> And I wonder whether there shouldn't be str.findany and
> str.iterfindany, which takes a sequence as an argument and returns the
> next match on any element of it.
On second thought, that wouldn't gain much on a loop over finding each
sequence, but add more complexity than i
Aloha,
Thomas Lotze wrote:
>>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.
> When I tried it just now,
are implemented already".
> I hope you'll let us know how much faster your
> final approach turns out to be.
I'm pretty convinced that implementing an algorithmically nice state
machine that goes through a string char by char won't get any faster than
using s[index] all t
Andreas Lobinger wrote:
> >>> t2 = f.find('2')+1
This is indeed faster than going through a string char by char. It doesn't
make for a nice character-based state machine, but of course it avoids
making Python objects for every character and uses the C implementation of
str for searching.
Howeve
Thomas Lotze wrote:
> Peter Otten wrote:
>
>>>>> class frankenstring(StringIO):
>> ... def next(self):
>> ... c = self.read(1)
>> ... if not c:
>> ... raise StopIteration
>> ... retu
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("0
Roland Heiber wrote:
> class MmapWithSeekAndTell(object):
> def __init__(self, m, size):
.. where m is a mmap-object and size the filesize ... sorry.
--
http://mail.python.org/mailman/listinfo/python-list
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
Peter Otten wrote:
>>>> class frankenstring(StringIO):
> ... def next(self):
> ... c = self.read(1)
> ... if not c:
> ... raise StopIteration
> ... return c
Repeated read(1) on a file-like object is one o
e
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 la
Roland Heiber wrote:
> if i did understand what you mean, what about using mmap?
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 do
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:
>>> from StringIO import StringIO
>&
Thomas Lotze wrote:
> It's definitely no help that file-like objects are iterable; I do want
> to get a character, not a complete line, at a time.
Hi,
if i did understand what you mean, what about using mmap? Iterating over
characters in a file like this:
# -*- coding: iso-8859-1 -*-
import os
On Wed, 13 Jul 2005 03:49:16 +0200, Thomas Lotze <[EMAIL PROTECTED]> wrote:
>Scott David Daniels wrote:
>
>> Now if you want to do it for a file, you could do:
>>
>> for c in thefile.read():
>>
>
>The whole point of the exercise is that seeking on a file doesn't
>influence iter
Scott David Daniels wrote:
> Now if you want to do it for a file, you could do:
>
> for c in thefile.read():
>
The whole point of the exercise is that seeking on a file doesn't
influence iteration over its content. In the loop you suggest, I can
seek() on thefile to my heart's
Thomas Lotze wrote:
> Hi,
> 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 = frankenstrin
jay graves wrote:
> see StringIO or cStringIO in the standard library.
Just as with files, iterating over them returns whole lines, which is
unfortunately not what I want.
--
Thomas
--
http://mail.python.org/mailman/listinfo/python-list
ke 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
Thomas Lotze wrote:
>Hi,
>
>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* comp
see StringIO or cStringIO in the standard library.
--
http://mail.python.org/mailman/listinfo/python-list
Hi,
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:
25 matches
Mail list logo