Re: Frankenstring

2005-07-18 Thread Thomas Lotze
Peter Otten wrote: > I hope you'll let us know how much faster your > final approach turns out to be OK, here's a short report on the current state. Such code as there is can be found at , with a Python mock-up in the same directory. Thi

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]
>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. Then why not subclass it and alter the iteration scheme to do a read(1) or something? from StringIO import StringIO

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
Peter Otten wrote: > Not clumsy, just slow. As you wish ;o) I didn't mean clumsy as in "clumsy looking Python code" anyway, rather as in "clumsy to use the Python machinery for operations that are straight-forward and efficient in C, in which language str and cStringIO are implemented already".

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 >> ... return c > > Repeated read(1) on a file-like object is one

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("0123456789") for c in f: > ...

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 of the ways of doing it with exist

Re: Frankenstring

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

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 >>> class frankenstring(Strin

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 = frankenstring("0123456789") for c in f: >

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
On Tue, 12 Jul 2005 22:08:55 +0200, Thomas Lotze <[EMAIL PROTECTED]> 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: > >>

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