File: vector.py
#from private import Private from access import Private, Public # change to test other version @Private('data', 'size') class Vector: def __init__(self, iterable): self.data = list(iterable) def getData(self): return list(self.data) def size(self): return len(self.data) def map(self, func): # or [func(x) for x in self.data] return map(func, self.data) def mapMe(self, func): # or self.data = self.map(func) for i in range(self.size()): self.data[i] = func(self.data[i]) def fold(self, other, func): # or map(func, self, other), but pads shorter with None return [func(x, y) for (x, y) in zip(self.data, other)] def foldMe(self, other, func): for i in range(self.size()): self.data[i] = func(self.data[i], other[i]) def inverse(self): self.data = self.map(lambda x: 1 / x) def square(self): self.mapMe(lambda x: x ** 2) def sqrt(self): import math self.data = [math.sqrt(x) for x in self.data] def add(self, other): self.data = self.fold(other, lambda x, y: x + y) def mul(self, other): self.foldMe(other, lambda x, y: x * y) def sub(self, other): self.data = [(x - y) for (x, y) in zip(self.data, other)] def __getitem__(self, i): return self.data[i] def __str__(self): return 'Vector<' + ', '.join(('%.4g' % x) for x in self.data) + '>' if __name__ == '__main__': V = Vector([1, 2, 3, 4]) print V ##print V.data # fails! ##V.data = [2, 3, 4] # fails! ##print V.size() # fails! ##V.size = 3 # fails! print print V.getData() V.square() print V V.sqrt() print V V.inverse() print V print V = Vector((1, 2, 3, 4)) W = Vector((2, 3, 4, 5)) #*** same self.wrapped print V print W V.add((2, 3, 4, 5)) print V V.foldMe(W, (lambda x, y: x - y)) print V V.mul(W) print V #print type(W), W.__class__, W, W.__getitem__ V.sub(W) print V V.sub([1, 2, 3, 4]) print V X = Vector(range(1,5)) X.mapMe(lambda x: (x, ('spam' * x))) print X.getData() print Vector(range(1,5)).map(lambda x: (x, 'Ni!' * x)) """ The following is a work in progress.. class BaseVector: def __init__(self, iterable): self.data = list(iterable) def getData(self): return list(self.data) def size(self): return len(self.data) def map(self, func): # or [func(x) for x in self.data] return map(func, self.data) def mapMe(self, func): # or self.data = self.map(func) for i in range(self.size()): self.data[i] = func(self.data[i]) def fold(self, other, func): # or map(func, self, other), but pads shorter with None return [func(x, y) for (x, y) in zip(self.data, other)] def foldMe(self, other, func): for i in range(self.size()): self.data[i] = func(self.data[i], other[i]) def inverse(self): self.data = self.map(lambda x: 1 / x) def square(self): self.mapMe(lambda x: x ** 2) def sqrt(self): import math self.data = [math.sqrt(x) for x in self.data] def add(self, other): self.data = self.fold(other, lambda x, y: x + y) def mul(self, other): ##print zip(self.data, other.data) print other.__class__, other.__getitem__, other print zip(self.data, other._onInstance__wrapped.data) self.foldMe(other, lambda x, y: x * y) def sub(self, other): print zip(self.data, other) self.data = [(x - y) for (x, y) in zip(self.data, other)] def __getitem__(self, i): return self.data[i] def __str__(self): return 'Vector<' + ', '.join(('%.4g' % x) for x in self.data) + '>' # much magic here! pubs = [attr for attr in BaseVector.__dict__.keys() if attr not in ('data', 'size')] print pubs @Public(*pubs) class Vector(BaseVector): def __iter__(self): return iter(self.data) pass """