This module provides two classes that emulate one and two dimentional lists with fixed sizes but mutable internals. Their primary purpose is to be used as a building tool for classes that need storage structures that cannot change in size.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | '''Support module for array and matrix use.
This module provides two classes that emulate one and two
dimentional lists with fixed sizes but mutable internals.'''
__version__ = 1.2
################################################################################
class Array:
'Array(length[, value]) -> new array'
def __init__(self, length, value=None):
'x.__init__(...) initializes x'
assert type(length) is int and length > 0
self.__data = [value for index in range(length)]
def __repr__(self):
'x.__repr__() <==> repr(x)'
return repr(self.__data)
def __len__(self):
'x.__len__() <==> len(x)'
return len(self.__data)
def __getitem__(self, index):
'x.__getitem__(i) <==> x[i]'
return self.__data[index]
def __setitem__(self, index, value):
'x.__setitem__(i, y) <==> x[i]=y'
self.__data[index] = value
def __delitem__(self, index):
'x.__delitem__(i) <==> del x[i]'
self.__data[index] = None
def __iter__(self):
'x.__iter__() <==> iter(x)'
return iter(self.__data)
def __contains__(self, value):
'x.__contains__(y) <==> y in x'
return value in self.__data
class Matrix:
'Matrix(rows, columns[, value]) -> new matrix'
def __init__(self, rows, columns, value=None):
'x.__init__(...) initializes x'
assert type(rows) is int and rows > 0
self.__data = [Array(columns, value) for index in range(rows)]
def __repr__(self):
'x.__repr__() <==> repr(x)'
return repr(self.__data)
def __len__(self):
'x.__len__() <==> len(x)'
return len(self.__data)
def __getitem__(self, index):
'x.__getitem__(i) <==> x[i]'
return self.__data[index]
def __setitem__(self, index, value):
'x.__setitem__(i, y) <==> x[i]=y'
self.__data[index] = Array(len(self.__data[index]), value)
def __delitem__(self, index):
'x.__delitem__(i) <==> del x[i]'
self.__data[index] = Array(len(self.__data[index]))
def __iter__(self):
'x.__iter__() <==> iter(x)'
return iter(self.__data)
def __contains__(self, value):
'x.__contains__(y) <==> y in x'
for item in self.__data:
if value in item:
return True
return False
################################################################################
if __name__ == '__main__':
import sys
print 'Content-Type: text/plain'
print
print file(sys.argv[0]).read()
|
A use of this recipe is demonstrated in z_html.py (a module listed elsewhere on this web site). The most useful places for the Array and Matrix are where data must be passed to client code and security is needed to prevent the caller from modifying the size of the structure.