473,397 Members | 2,099 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,397 software developers and data experts.

Problem with slices.

I'm for the moment writing two classes.

A table, which is like a list, but can start at any integer.

A tree which is like a dictionary, but will iterate over the
keys in sorted order.

The problem is that I would like to implemet slices but, that
seems to be impossible with how slices are implemented now.

I wrote the following class to test things out.

class Tst:
def __getitem__(self, key):
print key

then I called the interpreter and got this:
from tst import Tst
t=Tst()
t[:] slice(0, 2147483647, None) t[:9] slice(0, 9, None) t[:'ok'] slice(None, 'ok', None) t['ok':] slice('ok', None, None) t[6:] slice(6, 2147483647, None) t[1,2] (1, 2) t[1,2:] (1, slice(2, None, None)) t[(1,2):]

slice((1, 2), None, None)
Now suppose tab is a table with indexes from -5 to 12.

tab[:4] would have to make a table ranging from -5 to 4
tab[0:4] would have to make a table ranging from 0 to 4.

But each time I would be given the same argument, being
slice(0, 4, None). So I would be unable to distinghuish
between the two.

I don't think it very likely but I could have a table
with indexes from 2147483647 to 2147483700, so having
2147483647 as value that indicated till the end of
the sequence is a bit awkward.

The same problems occur when I have a tree with integer
key values. But even if I don't use integers as keys
I have a problem with what is returned since None is
a valid key and thus it shouldn't be used this way.

--
Antoon Pardon
Oct 3 '05 #1
1 1297
Antoon Pardon wrote:
I'm for the moment writing two classes.

A table, which is like a list, but can start at any integer.

A tree which is like a dictionary, but will iterate over the
keys in sorted order.

The problem is that I would like to implemet slices but, that
seems to be impossible with how slices are implemented now.

I wrote the following class to test things out.

class Tst:
def __getitem__(self, key):
print key

then I called the interpreter and got this:
from tst import Tst
t=Tst()
t[:] slice(0, 2147483647, None) t[:9] slice(0, 9, None) t[:'ok'] slice(None, 'ok', None) t['ok':] slice('ok', None, None) t[6:] slice(6, 2147483647, None) t[1,2] (1, 2) t[1,2:] (1, slice(2, None, None)) t[(1,2):] slice((1, 2), None, None)
Now suppose tab is a table with indexes from -5 to 12.

tab[:4] would have to make a table ranging from -5 to 4
tab[0:4] would have to make a table ranging from 0 to 4.

But each time I would be given the same argument, being
slice(0, 4, None). So I would be unable to distinghuish
between the two.

I don't think it very likely but I could have a table
with indexes from 2147483647 to 2147483700, so having
2147483647 as value that indicated till the end of
the sequence is a bit awkward.

The same problems occur when I have a tree with integer
key values. But even if I don't use integers as keys
I have a problem with what is returned since None is
a valid key and thus it shouldn't be used this way.

Consider new-style classes:
class T(object): .... def __getitem__(self, key):
.... return key
.... t = T()
t[:4] slice(None, 4, None) t[0:4] slice(0, 4, None) t[0:]

slice(0, None, None)

Peter
Oct 3 '05 #2

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

Similar topics

4
by: Microsoft | last post by:
I used photoshop to slice up some images. PS, creates the tables and differant images. You are supposed to simple upload the photos and copy and past the table codes taht was generated from the...
3
slightlybefuddled
by: slightlybefuddled | last post by:
(Exporting ImageReady slices as CSS rather than tables) apparently means it'll work just fine in Firefox, but do wacky stuff in IE? Can anyone help me figure out why on earth the slices are not...
31
by: Antoon Pardon | last post by:
The following is part of the explanation on slices in the tutorial: The best way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the...
3
by: David | last post by:
That xpairs() generator is nice, but it's not the best possible code What do you mean by best possible? Most efficient? Most readable? And why don't you use islice? eg: def xpairs(seq):...
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...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.