Вот псевдокод:
Код: Выделить всё
class Raster():
def __init__(self, filepaths, size, mode):
self.filepaths = filepaths
self.size = size
self.mode = mode
def __getitem__(self):
if not isinstance(self.filepaths, list):
array = ... # read the file into array
if array.size != self.size:
array = self.resample(array)
else:
arrays = []
for filepath in self.filepaths:
array = ... # read the file into array
if array.size != self.size:
array = self.resample(array)
arrays.append(array)
if self.mode == "A":
array = self.operation_A(arrays)
elif self.mode == "B":
array = self.operation_B(arrays)
else:
# raise an error
return array
def resample(self, array):
# resample to `self.size`
return resampled_array
def operation_A(self, arrays):
# perform operation A on list of arrays and return one array
return array
def operation_B(self, arrays):
# perform operation B on list of arrays and return one array
return array
Вот псевдокод:
Код: Выделить всё
class Vector():
def __init__(self, filepaths, size, mode):
self.filepaths = filepaths
self.size = size
self.mode = mode
def __getitem__(self):
if not isinstance(self.filepaths, list):
polygons = ... # read the vector file
array = self.rasterize(polygons)
else:
arrays = []
for filepath in self.filepaths:
polygon = ... # read the vector files
array = self.rasterize(polygons)
arrays.append(array)
if self.mode == "A":
array = self.operation_A(arrays)
elif self.mode == "B":
array = self.operation_B(arrays)
else:
# raise an error
return array
def rasterize(self, polygons):
# rasterize the polygons into an array of `self.size`
return rasterized_array
def operation_A(self, arrays):
# perform operation A on list of arrays and return one array
return array
def operation_B(self, arrays):
# perform operation B on list of arrays and return one array
return array
Кроме того, следует классы Operation_A() и Operation_B() должны быть абстрактными (зачем использовать абстрактные базовые классы в Python?)? И их метод? Должен ли это быть @abstractmethod или @classmethod?
Подробнее здесь: https://stackoverflow.com/questions/792 ... nd-methods