|
81
24th March 12:22
External User
|
Bug in string.find; was: Re: Proposed PEP: New style indexing,was Re: Bug in slice type
IMO the problem is that the index sign is doing two jobs, which for zero-based
reverse indexing have to be separate: i.e., to show direction _and_ a _signed_
offset which needs to be realtive to the direction and base position.
A list-like class, and an option to use a zero-based reverse index will illustrate:
... def __init__(self, value=0):
... self.value = value
... def __repr__(self): return 'Zbrx(%r)'%self.value
... def __sub__(self, other): return Zbrx(self.value - other)
... def __add__(self, other): return Zbrx(self.value + other) ...
... def normslc(self, slc):
... sss = [slc.start, slc.stop, slc.step]
... for i,s in enumerate(sss):
... if isinstance(s, Zbrx): sss[i] = len(self.value)-1-s.value
... return tuple(sss), slice(*sss)
... def __init__(self, value):
... self.value = value
... def __getitem__(self, i):
... if isinstance(i, int):
... return '[%r]: %r'%(i, self.value[i])
... elif isinstance(i, Zbrx):
... return '[%r]: %r'%(i, self.value[len(self.value)-1-i.value])
... elif isinstance(i, slice):
... sss, slc = self.normslc(i)
... return '[%r:%r:%r]: %r'%(sss+ (list.__getitem__(self.value, slc),))
... def __setitem__(self, i, v):
... if isinstance(i, int):
... list.__setitem__(self, i, v)
... elif isinstance(i, slice):
... sss, slc = self.normslc(i)
... list.__setitem__(self.value, slc, v)
... def __repr__(self): return 'Zbrxlist(%r)'%self.value ...
Zbrxlist([0, 1, 2, 3, 4, 5, 6, 7, 8, 'end', 9])
Zbrxlist([0, 1, 2, 3, 4, 5, 6, 7, 8, 'end', 9, 'final'])
Zbrxlist(['a', 'b', 'c', 'd', 'e'])
Forgot to provide a __len__ method ;-)
Zbrxlist(['a', 'b', 'c', 'd', 'e', 'end'])
lastx refers to the last items by zero-based reverse indexing
Zbrxlist(['a', 'b', 'c', 'd', 'e', 'last', 'end'])
As expected, or do you want to define different semantics?
You still need to spell len(a) in the slice somehow to indicate
beond the top. E.g.,
Zbrxlist(['a', 'b', 'c', 'd', 'e', 'last', 'end', 'final'])
Perhaps you can take the above toy and make something that works
they way you had in mind? Nothing like implementation to give
your ideas reality ;-)
Regards,
Bengt Richter
|