473,763 Members | 3,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(l en(sequence))
if isinstance(sequ ence, VSlice):
start = sequence.start + start * sequence.step
stop = sequence.start + stop * sequence.step
step *= sequence.step
sequence = sequence.sequen ce
if step == 1:
return V1Slice(sequenc e, start, stop)
else:
return VxSlice(sequenc e, 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("A ttempt to instantiate abstract base " +
"class VSlice. To create a VSlice, call vslice.vslice() .")

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

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

def get_stop(self):
return self._stop
stop = property(get_st op, 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__(sel f, 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__(sel f, 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__(sel f, name):
# return getattr(self[:], name)
# return getattr(self[0:0], name)
# return getattr(self._s eq, 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._se q, 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(leng th)[0]

def _translate_slic e(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._se q, 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('VxS lice index %d out of range' % index)
return self._start + (index * self._step)

def _translate_slic e(self, key):
start, stop, step = key.indices(sel f._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(vs lice, 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(vslic e)):
blist.append(vs lice[i])
j = 0 - i - 1
clist.append(vs lice[j])
dlist.append(vs lice[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(vs lice(a, None, None, -1), a[:: -1])
assert_equal(vs lice(va, None, None, -3), a[:: -3])
assert_equal(vs lice(a, 4L, 67, 5), a[4: 67 : 5])
assert_equal(vs lice(va, -84, 67, 4), a[-84: 67 : 4])
assert_equal(vs lice(va, 22, -12, 8), a[22: -12 : 8])
assert_equal(vs lice(va, -91, -17, 7), a[-91: -17 : 7])
assert_equal(vs lice(a, -97, -10, -6), a[-97: -10 : -6])
assert_equal(vs lice(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 2060
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
5053
by: Christian Stigen Larsen | last post by:
Consider the following: class parent { public: virtual void print() { printf("Parent\n"); } }; class child : public parent {
12
1800
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
3745
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 would I proceed with operator=? Do I need to supply 2 operators:
10
3770
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 you can delete a derived-class object via a base-class pointer...So, the correct destructor(s) gets invoked(the derived class one in particular) and the correct amount of memory is also released. But if the above is true, why isnt it a good...
11
2052
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 a section of a sliced string in Python 2.3 and there doesn't seem to be mention of this as a Python 2.4 feature (don't have time to actually try
9
3622
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 be so? is that has something to do with 16 bit environment and 32 bit environment? thanks in advance
17
3575
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 B(INT32 i =0):i_(i){} virtual ~B(){}
2
1502
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 nominal_attribute (the derived classes). But I need a list of attributes, and list<attribute> crashes if the print() function (or any function) is pure virtual. Continuous and nominal both have their own print() function, overriding the base print(). So I...
1
3474
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() { } // A virtual destructor
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.