Slice inconsistency?
Peter Otten
__peter__ at web.de
Sat Sep 27 11:25:45 EDT 2003
Roberto A. F. De Almeida wrote:
> 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.
[...]
> 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
Have you thought about a proxy class on the client side?
It can defer the actual server access until it has the slice/index
information for all dimensions:
<code>
class Proxy:
def __init__(self, dims, slices=None):
if slices == None:
self.slices = []
else:
self.slices = slices[:]
self.dims = dims
def fetchData(self, index):
return "fetching data: " + str(self.slices + [index])
def __len__(self):
return self.dims[0]
def __getitem__(self, index):
if len(self.dims) == 1:
return self.fetchData(index)
return Proxy(self.dims[1:], self.slices + [index])
def __str__(self):
return "data access deferred"
p = Proxy([10, 42, 9])
print p[:4]
print p[:-1][:-2][:-3]
print p[1][-2][:-3]
q = Proxy([0]*3)
print q[:4]
print q[:-1][:-2][:-3]
print q[1][-2][:-3]
</code>
If I have understood you right, the above should be a workable solution.
If you do not know the nested arrays' lengths beforehand, you could
implement __len__() to always return 0, which saves you from an
AttributeError for negative indices.
Peter
More information about the Python-list
mailing list