Slice implementation bug
Hello! Let's say slice is multidimensional now - how to interpret it? I excpect these to work: - m[0, 3] - get one element from matrix - m[0:2, 0:2] - get four elements from matrix, iterate over them (I have actually an rtree if it doesn't make sense to you) But it won't, because if m[0, 3] returns something, then m[0:2, 0:2] cannot yield anymore. Ofcourse I could return an iterator, but this would not be so simple. So, maybe we need these: - __isslice__(key) - return true, false - __getitem__(key) - return a value - __getslice__(key) - yield a lots of values So that if isslice returns true, getslice will be called. Would be much more beauty! Right now it's ugly. I love Python, but I also love n-dimensional spaces and those are ugly in Python - last time I did Matrix multiplication I could not implement m[0, 3] (stupid errors), could not m[0j + 3], could not use tuple. Now I was happy that it's implemented and just ran into another problem that I have to choose, if getter returns one or many :) Ofcourse I could return list or other iterable, but I want to be able to do it with three-liner, because the rest of my code mostly consists of those. Except the few masterfunctions, which do some complex manipulations with data. I also need to implement things like that, which also return true for __isslice__: m[Circle(0, 0, 10)] - for RTree, any shape object implementing getExtent and doesOverlap can be used as slice index. Because I do complex map transformations and all this trivial stuff must be *hidden*! Tambet -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice implementation bug
Tambet qtv...@gmail.com writes: Hello! Let's say slice is multidimensional now - how to interpret it? But who said that slice is multidimensional in the first place? Since python is not matlab I don't think it will ever be done, so what's the purpose of talking about a bug in something that doesn't exist? PS. probably in numpy you can find what you look for -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice implementation bug
On 5/12/11 6:06 AM, Tambet wrote: Hello! Let's say slice is multidimensional now - how to interpret it? I excpect these to work: * m[0, 3] - get one element from matrix * m[0:2, 0:2] - get four elements from matrix, iterate over them (I have actually an rtree if it doesn't make sense to you) But it won't, because if m[0, 3] returns something, then m[0:2, 0:2] cannot yield anymore. Let me try to rephrase, since you seem to be leaving out a lot of assumptions. You are trying to say that you want m[0:2,0:2] to return an iterator and that if you define __getitem__() such that m[0,3] returns a value, then you cannot implement __getitem__() to be a generator using a yield statement. Okay. Fine. But there is no reason that __getitem__() needs to be a generator function for you to return an iterator. You can simply return another generator. For example: def __getitem__(self, key): if isinstance(key, tuple): if any(isinstance(x, slice) for x in key): return self._generate_from_slice(key) # The default, boring case. return self._get_value(key) def _generate_from_slice(self, key): yield foo yield bar yield etc Ofcourse I could return an iterator, but this would not be so simple. Really, it is. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list