Re: Frankenstring

2005-07-18 Thread Thomas Lotze
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.

Re: Frankenstring

2005-07-14 Thread [EMAIL PROTECTED]
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

Re: Frankenstring

2005-07-14 Thread [EMAIL PROTECTED]
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

Re: Frankenstring

2005-07-14 Thread [EMAIL PROTECTED]
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

Re: Frankenstring

2005-07-14 Thread Thomas Lotze
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

Re: Frankenstring

2005-07-14 Thread Andreas Lobinger
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,

Re: Frankenstring

2005-07-14 Thread Thomas Lotze
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

Re: Frankenstring

2005-07-14 Thread Thomas Lotze
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

Re: Frankenstring

2005-07-13 Thread Peter Otten
Thomas Lotze wrote: > Peter Otten wrote: > >>>>> class frankenstring(StringIO): >> ... def next(self): >> ... c = self.read(1) >> ... if not c: >> ... raise StopIteration >> ... retu

Re: Frankenstring

2005-07-13 Thread Andreas Lobinger
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

Re: Frankenstring

2005-07-13 Thread Roland Heiber
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

Re: Frankenstring

2005-07-13 Thread Roland Heiber
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

Re: Frankenstring

2005-07-13 Thread Thomas Lotze
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

Re: Frankenstring

2005-07-13 Thread Thomas Lotze
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

Re: Frankenstring

2005-07-13 Thread Thomas Lotze
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

Re: Frankenstring

2005-07-13 Thread Peter Otten
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 >&

Re: Frankenstring

2005-07-13 Thread Roland Heiber
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

Re: Frankenstring

2005-07-12 Thread Bengt Richter
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

Re: Frankenstring

2005-07-12 Thread Thomas Lotze
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

Re: Frankenstring

2005-07-12 Thread Scott David Daniels
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

Re: Frankenstring

2005-07-12 Thread Thomas Lotze
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

Re: Frankenstring

2005-07-12 Thread Bengt Richter
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

Re: Frankenstring

2005-07-12 Thread Mike C. Fletcher
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

Re: Frankenstring

2005-07-12 Thread jay graves
see StringIO or cStringIO in the standard library. -- http://mail.python.org/mailman/listinfo/python-list

Frankenstring

2005-07-12 Thread Thomas Lotze
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: