T
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:
.... print c
.... if c == "2":
.... break
....
0
1
2.... print c
....
7
8
9
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.
I can think of more than one clumsy way to implement the desired
behaviour in Python; I'd rather like to know whether there's an
implementation somewhere that does it fast. (Yes, it's me and speed
considerations again; this is for a tokenizer at the core of a library,
and I'd really like it to be fast.) I don't think there's anything like
it in the standard library, at least not anything that would be obvious
to me.
I don't care whether this is more of a string iterator with seeking and
telling, or a file-like object with a single-character iterator; as long
as it does both efficiently, I'm happy.
I'd even consider writing such a beast in C, albeit more as a learning
exercise than as a worthwhile measure to speed up some code.
Thanks for any hints.
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:
.... print c
.... if c == "2":
.... break
....
0
1
2.... print c
....
7
8
9
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.
I can think of more than one clumsy way to implement the desired
behaviour in Python; I'd rather like to know whether there's an
implementation somewhere that does it fast. (Yes, it's me and speed
considerations again; this is for a tokenizer at the core of a library,
and I'd really like it to be fast.) I don't think there's anything like
it in the standard library, at least not anything that would be obvious
to me.
I don't care whether this is more of a string iterator with seeking and
telling, or a file-like object with a single-character iterator; as long
as it does both efficiently, I'm happy.
I'd even consider writing such a beast in C, albeit more as a learning
exercise than as a worthwhile measure to speed up some code.
Thanks for any hints.