optimization question

Bengt Richter bokr at oz.net
Tue Aug 13 23:40:38 CEST 2002


On 13 Aug 2002 16:58:54 GMT, bokr at oz.net (Bengt Richter) wrote:
>you can write
>    W(s)[i:j]==t
>
>using (untested!)
>    class W:
>        from sys import maxint
>        def __init__(self, s, lo=0, hi=None):
>            self.s = s; self.lo = lo
>            if hi is None or hi == self.maxint: self.hi = len(s)
>            else: self.hi = hi

>        def __getitem__(self, i ):
>            if isinstance(i, int): return self.s[i] # single char
>            return W(self.s, i.start, i.stop) # simplified slice assumption for example!

Oops, __getitem__ should be relative to the [lo:hi] region of self.s, not the whole self.s.
Sorry, no time to fix (there's a little more to it than just adding self.lo), but I noticed
I left out the adjustments.

>        def __len__(self): return self.hi-self.lo
>        def __eq__(self, other): # like eqsub
>            return len(other) == (self.hi-self.lo) and self.s[self.lo:self.hi] == other
>        def __str__(self): return self.s[self.lo:self.hi]
>
>which I assume could be made reasonably fast in C.

Regards,
Bengt Richter



More information about the Python-list mailing list