Virtual Slicing

2005-08-27 Thread Bryan Olson


I recently wrote a module supporting value-shared slicing. I
don't know if this functionality already existed somewhere, but
I think it's useful enough that other Pythoners might want it,
so here it is.

Also, my recent notes on Python warts with respect to negative
indexes were based on problems I encoutered debugging this
module, so I'm posting it partially as a concrete example of
what I was talking about.

--
--Bryan



"""
 vslice.py by Bryan G. Olson, 2005
 This module is free software and may be modified and/or
 distributed under the same terms as Python itself.

 Virtual Slicing differs from normal Python slicing in that
 that the cells in the given sequence are not copied; they
 are shared between the underlying sequence and the VSlice.
 VSlices are themselves Python sequences. You can index
 VSlices, slice them, iterate over them, get their len(),
 test 'if val in', compare them, add them, and multiply them
 by integers.

 The 'vslice' function creates virtual slices of sequences:

 vslice(sequence, start, stop, step)

 returns an instance of VSlice that is much-the-same-as:

 sequence[start : stop : step]

 The default for start, stop and step is None, and passing
 None or omitting parameters works the same as in Python
 slicing.

 VSlices also have read-only properties 'sequence', 'start',
 'stop' and 'step', in case you need to access the underlying
 sequence directly. Like Python's 'slice' object, the stop
 value will be negative if and only if step is negative and
 the slice includes the zero index.

 A VSlice of a VSlice will use the same underlying sequence.
 It will translate the start-stop-step values upon
 construction, so later access will go through only one layer
 of VSlicing. The sequence, start, stop, and step properties
 of the VSlice-of-a-VSlice will generally not be same as the
 parameters passed to the vslice factory function; they
 relate to the underlying sequence.

 >>> a = range(100)
 >>> from vslice import vslice
 >>> vs1 = vslice(a, 10, None, 2)
 >>> vs2 = vslice(vs1, 2, -2, 3)
 >>>
 >>> print vs2 == a[10 : None : 2][2 : -2 : 3]
 True
 >>> print vs2.sequence == vs1
 False
 >>> print vs2.sequence == a
 True
 >>> print vs2.sequence is a
 True
 >>> print vs2.start, vs2.stop, vs2.step
 14 96 6
 >>> print vs2 == a[14 : 96 : 6]
 True


 If the underlying sequence is mutable, the VSlice is semi-
 mutable. You can assign to elements, but not insert nor
 delete elements; similarly, no append, push, pop and such.
 Slice assignments must have the same length slice on both
 sides.

 A slice of a VSlice is a regular Python slice; it is a copy
 made by slicing the underlying sequence with translated
 start-stop-step values. For sane sequence types, the slice
 of the VSlice will therefore have the same type as the
 underlying sequence.

 A VSlice's start-stop-step and len are set on construction.
 Adding or removing indices from the underlying sequence will
 not change them, and is usually a bad thing to do.

 VSlices support any positive or negative integer step value,
 but are most efficient in both time and space when the step
 value is one. Fortunately, the need for any other step value
 is rare. The vslice function will choose between two sub-
 classes of VSlice, depending on whether the step is one. The
 VxSlice can support any step size; the V1Slice is faster and
 smaller, but only supports a step of one. VxSlice instances
 store five slots; V1Slices, 3.

"""


def vslice(sequence, start=None, stop=None, step=None):
 """ Return a VSlice (virtual slice). See module's __doc__.
 """
 start, stop, step = slice(start, stop, step).indices(len(sequence))
 if isinstance(sequence, VSlice):
 start = sequence.start + start * sequence.step
 stop = sequence.start + stop * sequence.step
 step *= sequence.step
 sequence = sequence.sequence
 if step == 1:
 return V1Slice(sequence, start, stop)
 else:
 return VxSlice(sequence, start, stop, step)



from itertools import islice

_type_err_note = 'VSlice index must be integer or slice.'

_module_doc = __doc__

class VSlice (object):

 __doc__ = _module_doc

 def __init__(self, *args):
 if self.__class__ == VSlice:
 raise RuntimeError("Attempt to instantiate abstract ba

Re: Virtual Slicing

2005-08-27 Thread Sybren Stuvel
Bryan Olson enlightened us with:
> I recently wrote a module supporting value-shared slicing.

Maybe I'm dumb, but could you explain this concept? Why would someone
whant this?

Sybren
-- 
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself? 
 Frank Zappa
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Virtual Slicing

2005-08-27 Thread Oren Tirosh
Bryan Olson wrote:
> I recently wrote a module supporting value-shared slicing. I
> don't know if this functionality already existed somewhere,

In the Numarray module slices are a view into the underlying array
rather than a copy.

http://www.stsci.edu/resources/software_hardware/numarray

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


Re: Virtual Slicing

2005-08-29 Thread Bryan Olson
Sybren Stuvel wrote:
 > Bryan Olson enlightened us with:
 >
 >>I recently wrote a module supporting value-shared slicing.
 >
 > Maybe I'm dumb, but could you explain this concept? Why would someone
 > whant this?

My original motivation was reduce the amount of copying in some
tools that parse nested structures. All I really needed at the
time was a reference to a string, and the start and stop values.
Once I adopted Python's sequence interface, I thought I might as
well implement it consistently, generally, and completely.

So the first reason someone might want this is for efficiency,
in space and/or time.

The second reason is more abstract. Python's slice assignment is
a useful feature, but the slice selection must appear on the
right-hand-side of assignment. VSlice lets one instantiate the
updatable slice as an object, and pass it around.

I looked into supporting slice assignment between slices of
different sizes when possible, but the various options I came up
with all sucked.


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