Negative array indicies and slice()

2012-10-28 Thread andrewr3mail
The slice operator does not give any way (I can find!) to take slices from 
negative to positive indexes, although the range is not empty, nor the expected 
indexes out of range that I am supplying.

Many programs that I write would require introducing variables and logical 
statements to correct the problem which is very lengthy and error prone unless 
there is a simple work around.

I *hate* replicating code every time I need to do this!

I also don't understand why slice() is not equivalent to an iterator, but can 
replace an integer in __getitem__() whereas xrange() can't.


Here's an example for Linux shell, otherwise remove /bin/env...
{{{#!/bin/env python
a=[1,2,3,4,5,6,7,8,9,10]
print a[-4:3]  # I am interested in getting [7,8,9,10,1,2] but I get [].
}}}
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Negative array indicies and slice()

2012-10-28 Thread andrewr3mail
On Sunday, October 28, 2012 8:43:30 PM UTC-7, Ian wrote:
> On Sun, Oct 28, 2012 at 9:12 PM, andrew wrote:
> 
> > The slice operator does not give any way (I can find!) to take slices from 
> > negative to positive indexes, although the range is not empty, nor the 
> > expected indexes out of range that I am supplying.
> 
> >
> 
> > Many programs that I write would require introducing variables and logical 
> > statements to correct the problem which is very lengthy and error prone 
> > unless there is a simple work around.
> 
> >
> 
> > I *hate* replicating code every time I need to do this!
> 
> >
> 
> > I also don't understand why slice() is not equivalent to an iterator, but 
> > can replace an integer in __getitem__() whereas xrange() can't.
> 
> >
> 
> >
> 
> > Here's an example for Linux shell, otherwise remove /bin/env...
> 
> > {{{#!/bin/env python
> 
> > a=[1,2,3,4,5,6,7,8,9,10]
> 
> > print a[-4:3]  # I am interested in getting [7,8,9,10,1,2] but I get [].
> 
> > }}}
> 
> 
> 
> 
> 
> For a sequence of length 10, "a[-4:3]" is equivalent to "a[6:3]",
> 
> which is an empty slice since index 6 is after index 3.
> 
> 
> 
> If you want it to wrap around, then take two slices and concatenate
> 
> them with "a[-4:] + a[:3]".

Hi Ian,
Well, no it really isn't equivalent.
Consider a programmer who writes:
xrange(-4,3) *wants* [-4,-3,-2,-1,0,1,2]

That is the "idea" of a range; for what reason would anyone *EVER* want -4 to 
+3 to be 6:3???

I do agree that the data held in -4 is equivalent to the data in 6, but the 
index is not the same.

So: Why does python choose to convert them to positive indexes, and have slice 
operate differently than xrange -- for the slice() object can't possibly know 
the size of the array when it is passed in to __getitem__;  They are totally 
separate classes.

I realize I can concat. two slice ranges, BUT, the ranges do not always span 
from negative to positive.

eg: a line in my program reads:
a[x-5:x]

if x is 7, then this is a positive index to a positive index.
So, there is no logic to using two slices concatd !

I use this arbitrary range code *often* so I need a general purpose solution.
I looked up slice() but the help is of no use, I don't even know how I might 
overload it to embed some logic to concatenate ranges of data; nor even if it 
is possible.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Negative array indicies and slice()

2012-10-29 Thread andrewr3mail
On Sunday, October 28, 2012 10:14:03 PM UTC-7, Paul Rubin wrote:
> Andrew writes:
> 
> > So: Why does python choose to convert them to positive indexes, and
> 
> > have slice operate differently than xrange 
> 
> 
> 
> There was a thread a few years back, I think started by Bryan Olson,
> 
> that made the case that slice indexing is a Python wart for further
> 
> reasons than the above, and suggesting a notation like x[$-5] to denote
> 
> what we now call x[-5] (i.e. $ is the length of the string).  So your
> 
> example x[$-4:3] would clearly be the same thing as x[6:3] and not give
> 
> any suggestion that it might wrap around.

I'm getting very frustrated with the editor provided for this group... It keeps 
posting prematurely, and putting my email in even when I tell it not to each 
time; and there is no way to edit a post... but deleting is ok...

I think Olson makes a good point.  The len() operator is so ubiquitous that it 
would be very useful to have a shorthand like that.

I'll have to look for his thread.

I'm thinking that I might just patch my version of Python 3.x, in C, to allow 
iterators to be passed to __getitem__; I haven't ever seen someone wanting to 
use mixed sign indexes to extract a small chunk of an array in the middle; so I 
don't think my patch will break existing code.

The snippets of code given by other posters in the thread might also be used to 
make a compatibility wrapper; I'll have to study it closer; so that distributed 
code would still work on unpatched python, albeit much slower.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Negative array indicies and slice()

2012-10-29 Thread andrewr3mail
On Sunday, October 28, 2012 9:44:56 PM UTC-7, alex23 wrote:
> On Oct 29, 2:09 pm, Andrew  wrote:
> 
> > I use this arbitrary range code *often* so I need a general purpose 
> > solution.
> 
> > I looked up slice() but the help is of no use, I don't even know how I might
> 
> > overload it to embed some logic to concatenate ranges of data; nor even if
> 
> > it is possible.
> 
> 
> 
> Slices are passed in if provided to __getitem__/__setitem__/
> 
> __delitem__, so you'd need to override it at the list level:
> 
> 
> 
> class RangedSlicer(list):
> 
> def __getitem__(self, item):
> 
> # map item.start, .stop and .step to your own semantics
> 
> 
> 
> Then wrap your lists with your RangedSlicer class as needed.

Hmmm...

I began a test in an interactive shell:
>>> class RangedSlicer(list):
... def __getitem__(self,item):
... print item
... 
>>> a=[1,2,3,4,5]
>>> a.__getitem__( slice(1,5) )
[2, 3, 4, 5]

Very odd...  I would have expected [1,2,3,4]

>>> a.__getitem__( slice(1,8) )
[2, 3, 4, 5]

So, slice() somehow was truncated although it ought to have been executed 
first, and passed to __getitem__() before __getitem__ could affect it.
That requires some tricky programming!

Not only that, but,
a.__getitem__( xrange[1,8] )
Causes an exception before the __getitem__ shadowing received it.

I don't see how I can over-ride it with your suggestion, but that's very 
inconsistent for your idea seems to be normal python that would work for 
user defined classes.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Negative array indicies and slice()

2012-10-29 Thread andrewr3mail
On Monday, October 29, 2012 1:38:04 AM UTC-7, Chris Rebert wrote:
> On Mon, Oct 29, 2012 at 1:24 AM, 
> 
> > On Sunday, October 28, 2012 9:44:56 PM UTC-7, alex23 wrote:
> 
> >> On Oct 29, 2:09 pm, Andrew < wrote:
> 
> You never wrapped `a` in a RangedSlicer or otherwise made use of RangedSlicer!
> 
> You wanted:
> 
> a = RangedSlicer([1,2,3,4,5])
> 
> 
> 
>  a.__getitem__( slice(1,5) )
> 
> > [2, 3, 4, 5]
> 
> >
> 
> > Very odd...  I would have expected [1,2,3,4]
> 
> 
> 
> "[2, 3, 4, 5]" is the return value from `a.__getitem__( slice(1,5) )`
> 
> (or, equivalently, from `[1,2,3,4,5][1:5]`). It is not the result of
> 
> "print item"; that line of code is never executed since you never used
> 
> the RangedSlicer class at all.
> 
> 
> 
> Regards,
> 
> Chris

My apology --- I deleted that post; yet it didn't delete... I saw my mistake 
seconds after posting.

* gmail.

Note: I subscribed to the python-list, and am able to recieve e-mails, but I 
don't see how to write a post for this particular thread nor subscribe to this 
particular thread...

A brief suggestion, or link to a howto would be *much* appreciated.

-- 
http://mail.python.org/mailman/listinfo/python-list