Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Programming languages > Bug in string.find was: Re: Proposed PEP: New style indexing was
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
81 24th March 12:22
bokr@oz.net (bengt
External User
 
Posts: 1
Default 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
  Reply With Quote


  sponsored links


82 24th March 16:54
terry reedy
External User
 
Posts: 1
Default Bug in string.find



OK, now I understand your question, the answer 'get a one's complement
machine', and your point that without a -0 separate from +0, there is a
subtle asymmetry that is easy to overlook.


Perhaps you did not see my other long post, where I drew ascii pictures?

In brief: if you draw an axis with ticks, label the tick with ints, and put
chars or item references in the space *between* the ticks, then the
'average' coordinate of the stuff between is n.5. The slice interval is
nn+1), but a single int label has to be either n or (n+1), rounding down
or up. And both choices have been made because both have advantages.

In the US (and UK?), the ground level floor of a multifloor building is the
first floor. In continental Europe (all or just some?), the ground floor
is the ground (effectively zeroth) floor while the first floor up is the
first stage (resting place on the stairway).


Yes, the introduction of reversed partly obsoleted the use of negative
strides, at least outside of its numerical array origin.

Terry J. Reedy
  Reply With Quote
83 25th March 04:54
scott david daniels
External User
 
Posts: 1
Default Bug in string.find; was: Re: Proposed PEP: New style indexing,was


Although it is _way_ too late to try something like this, once upon
a time you could have done all of this using the one's complement
operator:
~0 does exist and is distinct from 0.
So you could talk about a slice:
str[4 : ~2]
and so on.

--Scott David Daniels
Scott.Daniels@Acm.Org
  Reply With Quote
84 25th March 08:43
ron adam
External User
 
Posts: 1
Default Bug in string.find


Yes, I don't thinks it falls in the catagory of bugs, probably closer
to a minor wart.


Yes, I saw it. I think we are expressing the same things in different ways.

In the VA Hospital here in Tampa, the ground floor in the front elevator
is on the same level as the lobby, while the ground floor in the
elevator on the other side of the building is on the floor below. ;-)
  Reply With Quote
85 25th March 08:44
ron adam
External User
 
Posts: 1
Default Bug in string.find; was: Re: Proposed PEP: New style indexing,was


Yes, that's definitely part of it.


Thanks, I'll play around with it. ;-)

As you stated before the index is doing two jobs, so limiting it in some
way may be what is needed. Here's a few possible (or impossible) options.

(Some of these aren't pretty.)


* Disallow *all* negative values, use values of start/stop to determine
direction. Indexing from far end needs to be ******** (len(n)-x).

a[len(a):0] reverse order
a[len(a):0:2] reveres order, even items

(I was wondering why list's couldn't have len,min, and max attribute
that are updated when ever the list is modified in place of using
len,min, and max functions? Would the overhead be that much?)

a[len.a:0]


* Disallow negative index's, use negative steps to determine indexing
direction. Order of index's to determine output order.

a[len(a):0:-1] forward order, zero based indexing from end.
a[0:len(a):-1] reverse order, zero based from end.
a[0:1:-1] last item

I works, but single a[-1] is used extremely often. I don't think having
to do a[0:1:-1] would be very popular.


* A reverse index symbol/operator could be used ...
a[~0] -> last item, This works BTW. :-) ~0 == -1
a[~1] -> next to last item

(Could this be related to the original intended use?)


a[~0:~0] slice after end ?. Doesn't work correctly.

What is needed here is to index from the left instead of the right.
a[~0] -> item to left of end gap.

*IF* this could be done; I'm sure there's some reason why this won't
work. ;-), then all indexing operations with '~' could be symmetric with
all positive indexing operations. Then in Python 3k true negative
index's could cause an exception... less bugs I bet. And then negative
steps could reverse lists with a lot less confusion, keeping that
functionality as well.

Maybe a[~:~] == a[-len(a):-0]
a[~:] == a[-len(a):len(a)]
a[:~] == a[0:-0]

a[~:~] == a[:]

This looks too nice, has it been suggested before?


Ok... a lot of off the top of my head thinking which probably means
these need to be thought out much better. But that's how a lot of good
ideas start out. ;-)
  Reply With Quote
86 25th March 12:36
terry reedy
External User
 
Posts: 1
Default Bug in string.find;


Python's list and, I believe, other builtin roster objects do have a
C-level length attribute. For mutables, it is updated. __len__ is just a
get of int value the and conversion to int object. Min and max are defined
and sensible for only a subset of lists, and when they are, are very seldom need repeated updates.


For the fraction of times used, yes.

Terry J. Reedy
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666