regexp search on infinite string?

John Machin sjmachin at lexicon.net
Sat Sep 15 09:07:47 EDT 2007


On Sep 15, 10:56 pm, Paddy <paddy3... at googlemail.com> wrote:
> On Sep 14, 9:49 pm, Paddy <paddy3... at googlemail.com> wrote:
>
> > Lets say i have a generator running that generates successive
> > characters of a 'string'>From what I know, if I want to do a regexp search for a pattern of
>
> > characters then I would have to 'freeze' the generator  and pass the
> > characters so far to re.search.
> > It is expensive to create successive characters, but caching could be
> > used for past characters. is it possible to wrap the generator in a
> > class, possibly inheriting from string, that would allow the regexp
> > searching of the string but without terminating the generator? In
> > other words duck typing for the usual string object needed by
> > re.search?
>
> > - Paddy.
>
> There seems to be no way of breaking into the re library accessing
> characters from the string:
>
> >>> class S(str):
>
> ...     def __getitem__(self, *a):
> ...             print "getitem:",a
> ...             return str.__getitem__(self, *a)
> ...     def __get__(self, *a):
> ...             print "get:",a
> ...             return str.__get__(self, *a)
> ...>>> s = S('sdasd')
> >>> m = re.search('as', s); m.span()
> (2, 4)
> >>> m = sre.search('as', s); m.span()
> (2, 4)
> >>> class A(array.array):
>
> ...     def __getitem__(self, *a):
> ...             print "getitem:",a
> ...             return str.__getitem__(self, *a)
> ...     def __get__(self, *a):
> ...             print "get:",a
> ...             return str.__get__(self, *a)
> ...
>
> >>> s = A('c','sdasd')
> >>> m = re.search('as', s); m.span()
> (2, 4)
> >>> m = sre.search('as', s); m.span()
> (2, 4)
>
> - Paddy.

That would no doubt be because it either copies the input [we hope
not] or more likely because it hands off the grunt work to a C module
(_sre).

Why do you want to "break into" it, anyway?




More information about the Python-list mailing list