python切片表达式3个参数_我能在python类方法中“检测”切片表达式吗?

多亏了Martijn Pieters的指导和更多的阅读,我想出了以下代码作为演示。注意Reader类使用netcdf文件和netCDF4库。如果您想自己尝试这段代码,您需要一个带有变量“a”和“b”的netcdf文件,或者将Reader替换为将返回数据数组或数据数组切片的其他文件。在

这个解决方案定义了三个类:Reader执行实际的文件I/O处理,Values管理数据访问部分,并在内存中没有存储数据时调用Reader实例,var是最终的“变量”,在实际生活中它将包含更多的元数据。代码包含两个额外的打印语句,用于教育目的。在"""Implementation of a dynamic variable class which can read data from file when needed or

return the data values from memory if they were read already. This concepts supports

slicing for both memory and file access."""

import numpy as np

import netCDF4 as nc

FILENAME = r"C:\Users\m.schultz\Downloads\data\tmp\MACC_20141224_0001.nc"

VARNAME = "a"

class Reader(object):

"""Implements the actual data access to variable values. Here reading a

slice from a netcdf file.

"""

def __init__(self, filename, varname):

"""Final implementation will also have to take groups into account...

"""

self.filename = filename

self.varname = varname

def read(self, args=slice(None, None, None)):

"""Read a data slice. Args is a tuple of slice objects (e.g.

numpy.index_exp). The default corresponds to [:], i.e. all data

will be read.

"""

with nc.Dataset(self.filename, "r") as f:

values = f.variables[self.varname][args]

return values

class Values(object):

def __init__(self, values=None, reader=None):

"""Initialize Values. You can either pass numerical (or other) values,

preferrably as numpy array, or a reader instance which will read the

values on demand. The reader must have a read(args) method, where

args is a tuple of slices. If no args are given, all data should be

returned.

"""

if values is not None:

self._values = np.array(values)

self.reader = reader

def __getattr__(self, name):

"""This is only be called if attribute name is not present.

Here, the only attribute we care about is _values.

Self.reader should always be defined.

This method is necessary to allow access to variable.values without

a slicing index. If only __getitem__ were defined, one would always

have to write variable.values[:] in order to make sure that something

is returned.

"""

print ">>> in __getattr__, trying to access ", name

if name == "_values":

print ">>> calling reader and reading all values..."

self._values = self.reader.read()

return self._values

def __getitem__(self, args):

print "in __getitem__"

if not "_values" in self.__dict__:

values = self.reader.read(args)

print ">>> read from file. Shape = ", values.shape

if args == slice(None, None, None):

self._values = values # all data read, store in memory

return values

else:

print ">>> read from memory. Shape = ", self._values[args].shape

return self._values[args]

def __repr__(self):

return self._values.__repr__()

def __str__(self):

return self._values.__str__()

class var(object):

def __init__(self, name=VARNAME, filename=FILENAME, values=None):

self.name = name

self.values = Values(values, Reader(filename, name))

if __name__ == "__main__":

# define a variable and access all data first

# this will read the entire array and save it in memory, so that

# subsequent access with or without index returns data from memory

a = var("a", filename=FILENAME)

print "1: a.values = ", a.values

print "2: a.values[-1] = ", a.values[-1]

print "3: a.values = ", a.values

# define a second variable, where we access a data slice first

# In this case the Reader only reads the slice and no data are stored

# in memory. The second access indexes the complete array, so Reader

# will read everything and the data will be stored in memory.

# The last access will then use the data from memory.

b = var("b", filename=FILENAME)

print "4: b.values[0:3] = ", b.values[0:3]

print "5: b.values[:] = ", b.values[:]

print "6: b.values[5:8] = ",b.values[5:8]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值