Slice inconsistency?

Roberto A. F. De Almeida roberto at dealmeida.net
Sat Sep 27 15:45:23 CEST 2003


Stephen Horne <$$$$$$$$$$$$$$$$$@$$$$$$$$$$$$$$$$$$$$.co.uk> wrote in message news:<bqeanvgel9ra3va44j090bmn2l126t340g at 4ax.com>...
> I would have expected, given that the tuple contains slice objects
> constructed from the multiple-slice notation, that the same
> translations would be performed on the slices that are inserted into
> the tuple that are applied when the single slice is created.
> 
> That is, whether the single or multiple notation is used, and whether
> the slice objects are placed in a tuple or not, they are constructed
> from the tuple notation - the translation from notation to slice
> object should be done consistently.

Yes, I believe I was not very clear. This is the inconsistency I was
talking about. (And this is with Python 2.3.1)
 
> >What you probably want is b[:-1][:-1], etc.  Each index must be
> >separately bracketed to access items in list of lists (etc).
> Maybe. Maybe not. I assumed he was doing something similar to what
> happens in numeric - they do some rather interesting
> slicing-of-multidimensional-container things over there. Of course I'm
> not a numeric user, so I may be misunderstanding things.

Yes, you're right.

The problem with b[:-1][:-1] is that it will return b[:-1], and then
slice the result again. It's ok if you're working with lists, but not
for what I'm doing.

I'm working in a client to access OPeNDAP servers. It's basically data
served through http, with some metadata and the ability to specify the
variables and ranges you want.

For example, if you have a dataset served at:

  http://server/cgi-bin/opendap/dataset.nc

You can download the variable "A" as ASCII with the following URL:

  http://server/cgi-bin/opendap/dataset.nc.ascii?A

If you want only part of "A", in binary (xdr) format:

  http://server/cgi-bin/opendap/dataset.nc.dods?A[1:10][1:5][6]

What I'm doing is this:

>>> from opendap import client
>>> data = client.dataset("http://server/cgi-bin/opendap/dataset.nc")
>>> print data.variables["A"].shape
(20,10,12)
>>> A = data.variables["A"][1:10,1:5,6]

Data is only retrieved when you slice a variable like above, and only
the data sliced is requested from the OPeNDAP server, instead of the
whole range. If I use multiple slices, eg,

>>> A = data.variables["A"][1:10][1:5][6]

then first the client will have to download A[1:10][:][:], and slice
it later -- more data than needed will be retrieved. If instead I pass
a tuple of slices, I can construct the URL

  http://server/cgi-bin/opendap/dataset.nc.dods?A[1:10][1:5][6]

and request from the server only the data that will be used, and
return it as a Numeric array.

Roberto




More information about the Python-list mailing list