473,378 Members | 1,321 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Virtual Slicing



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 base " +
"class VSlice. To create a VSlice, call vslice.vslice().")

def get_sequence(self):
return self._seq
sequence = property(get_sequence, None, None,
'The underlying sequence, never itself a VSlice.')

def get_start(self):
return self._start
start = property(get_start, None, None,
'Inclusive bound in the underlying sequence.')

def get_stop(self):
return self._stop
stop = property(get_stop, None, None,
'Exclusive bound in the underlying sequence.')

def get_step(self):
return self._step
step = property(lambda self: self.get_step(), None, None,
'Size of steps relative to the underlying sequence.')

def __getitem__(self, key):
if isinstance(key, (int, long)):
return self._seq[self._translate(key)]
elif isinstance(key, slice):
(start, stop, step) = self._translate_slice(key)
return self._seq[start : stop : step]
else:
raise TypeError(_type_err_note)

def __setitem__(self, key, value):
if isinstance(key, (int, long)):
self._seq[self._translate(key)] = value
elif isinstance(key, slice):
(start, stop, step) = self._translate_slice(key)
self._seq[start : stop : step] = value
else:
raise TypeError(_type_err_note)

def __cmp__(self, other):
# Compare progressively larger chunks.
start, stop = 0, 4
while 1:
me = self[start : stop]
them = other[start : stop]
if me != them:
return cmp(me, them)
if len(me) < stop - start:
return 0
start, stop = stop, stop + stop

def __repr__(self):
return 'vslice(%s)' % repr(self[:])

def __add__(self, term):
return self[:] + term

def __mul__(self, term):
return self[:] * term

def __hash__(self):
return hash(self[:])

# Various bad ideas for def __getattr__(self, name):
# return getattr(self[:], name)
# return getattr(self[0:0], name)
# return getattr(self._seq, name)

class V1Slice (VSlice):

'VSlice subclass for step == 1'

__slots__ = '_seq', '_start', '_stop'

def __init__(self, sequence, start, stop):
self._seq, self._start, self._stop = sequence, start, stop

def get_step(self):
return 1

def __len__(self):
return self._stop - self._start

def __iter__(self):
return islice(self._seq, self._start, self._stop)

def _translate(self, i):
length = self._stop - self._start
if not -length <= i < length:
raise IndexError
return slice(i, i + 1).indices(length)[0]

def _translate_slice(self, key):
start, stop, step = key.indices(len(self))
stop = self._start + stop
if stop < 0:
stop = None
return (self._start + start, stop, step)

class VxSlice(VSlice):

'VSlice subclass for step of any integer'

__slots__ = '_seq', '_start', '_stop', '_step', '_length'

def __init__(self, sequence, start=None, stop=None, step=1):
self._seq, self._start, self._stop, self._step = (
sequence, start, stop, step)
self._length = max(0, (self._stop - self._start +
self._step - (self._step / abs(self._step))) // self._step)

def __len__(self):
return self._length

def __iter__(self):
if self._step >= 0:
return islice(self._seq, self._start, self._stop, self._step)
else:
def gen():
seq, i, stop, step = (self._seq, self._start,
self._stop, self._step)
while i > stop:
yield seq[i]
i += step
return gen()

def _translate(self, index):
if index < 0:
index = self._length + index
if index < 0 or index > self._length:
raise IndexError('VxSlice index %d out of range' % index)
return self._start + (index * self._step)

def _translate_slice(self, key):
start, stop, step = key.indices(self._length)
start, stop, step = (
self._start + (start * self._step),
self._start + (stop * self._step),
step * self._step)
if stop < 0:
stop = None
return (start, stop, step)

def test():
print __doc__

def assert_equal(vslice, target):
assert len(vslice) == len(target)
assert vslice == target
assert vslice[:] == target
assert vslice[: 7L] + vslice[7 :] == target
assert vslice[::-1][::-1] == target
assert [x for x in vslice] == [x for x in target]
blist, clist, dlist = [], [], []
for i in range(len(vslice)):
blist.append(vslice[i])
j = 0 - i - 1
clist.append(vslice[j])
dlist.append(vslice[long(i)])
assert blist == [x for x in target]
clist.reverse()
assert clist == [x for x in target]
assert dlist == [x for x in target]
assert vslice[2 : -3] == target[2 : -3]
base = [1 + 2 * n for n in range(100)]
a = base[:]
# Test various copies
va = vslice(a)
assert_equal(va, a)
assert_equal(vslice(a, None, None, -1), a[:: -1])
assert_equal(vslice(va, None, None, -3), a[:: -3])
assert_equal(vslice(a, 4L, 67, 5), a[4: 67 : 5])
assert_equal(vslice(va, -84, 67, 4), a[-84: 67 : 4])
assert_equal(vslice(va, 22, -12, 8), a[22: -12 : 8])
assert_equal(vslice(va, -91, -17, 7), a[-91: -17 : 7])
assert_equal(vslice(a, -97, -10, -6), a[-97: -10 : -6])
assert_equal(vslice(va, -83, -11, -3), a[-83: -11 : -3])
# Test some updates
va[6 : 82 : 7] = [6 + x * 17 for x in range(4, 80, 7)]
assert_equal(va, a)
a[22] = 9427
assert_equal(va, a)
b = base[:]
vb = base[:]
assert_equal(vb, b)
b[37] = 61107
assert tuple(b) != tuple(vb)
vb[37] = 61107
assert tuple(b) == tuple(vb)
newjunk = [12 + x * 3 for x in range(4, 80, 7)]
b[5 : 80 : 7] = newjunk
assert tuple(b) != tuple(vb)
vb[5 : 80 : 7] = newjunk
assert tuple(b) == tuple(vb)
print "Good."
if __name__ == '__main__':
test()

Aug 27 '05 #1
3 2028
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
Aug 27 '05 #2
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/softw...dware/numarray

Aug 27 '05 #3
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
Aug 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Christian Stigen Larsen | last post by:
Consider the following: class parent { public: virtual void print() { printf("Parent\n"); } }; class child : public parent {
12
by: c++novice | last post by:
1--Can operators be virtual? 2--What is the difference between an operator= returning a refernce Vs a value?
17
by: N4M | last post by:
Dear, Suppose I have a Protocol class, in which I need also an assignment operator =(). class B { ..... virtual B& operator=(const B& rb) =0; }; Now in some derived class D: public B, how...
10
by: amparikh | last post by:
Ok, my question is not about Virtual destructors and why, but more on the significance. Generally we have a virtual destructor in the base class ( and inadvertently in the derived class) so that...
11
by: jbperez808 | last post by:
>>> rs='AUGCUAGACGUGGAGUAG' >>> rs='GAG' Traceback (most recent call last): File "<pyshell#119>", line 1, in ? rs='GAG' TypeError: object doesn't support slice assignment You can't assign to...
9
by: sushant | last post by:
hello, my question is related with the address of a variable in C. suppose if i am printing the address of a variable, so that address will be virtual address or physical address. and why it'll...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
2
by: jjsavage | last post by:
Hi everyone, Ok, I've got a base class called attribute, with a virtual print() function. Attributes are never really instantiated, because it has to be a continuous_attribute or a...
1
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.