Welcome, guest | Sign In | My Account | Store | Cart
class ShiftMatrix(object):
    """ An NxN matrix generated by shifting the zeroth row left or right.
    Only the zeroth row is stored in memory, the rest is generated. This
    cuts memory usage from NxN to a single N.

    >>> shift_matrix = ShiftMatrix([1, 2, 3, 4])
    >>> print shift_matrix
    [1, 2, 3, 4]
    [4, 1, 2, 3]
    [3, 4, 1, 2]
    [2, 3, 4, 1]

    >>> shift_matrix = ShiftMatrix([1, 2, 3, 4])
    >>> print len(shift_matrix)
    16

    The class works with any iterable, for example a string:
    >>> shift_matrix = ShiftMatrix('Test', shift='left')
    >>> print shift_matrix[1]
    estT

    >>> shift_matrix = ShiftMatrix('Test', shift='left')
    >>> print shift_matrix[1][2]
    t
    """

    def __init__(self, data=None, shift='right'):
        if shift in ['right','left']:
            self.shift = shift
        else:
            raise ValueError("Shift direction must be 'right' or 'left'.")
        if not data:
            self.data = list()
        else:
            try:
                iter(data)
            except TypeError, e:
                raise TypeError("Object is not iterable.")
            self.data = data

    def __getitem__(self, index):
        if index > len(self.data) - 1:
            raise IndexError("Object has no index %d." % index)
        if self.shift == 'right':
            return self.data[-index % len(self.data):] + self.data[:-index]
        if self.shift == 'left':
            return self.data[index:] + self.data[:index % len(self.data)]

    def __len__(self):
        return len(self.data)

    def __str__(self):
        return "\n".join(str(row) for row in self.__iter__())

    def __iter__(self):
        for row_index in range(len(self.data)):
            yield self.__getitem__(row_index)

if __name__ == "__main__":
    shift_matrix = ShiftMatrix([1,2,3],shift='left')
    for row in shift_matrix:
        print row

Diff to Previous Revision

--- revision 6 2012-06-04 20:44:37
+++ revision 7 2012-06-05 12:38:36
@@ -4,12 +4,17 @@
     cuts memory usage from NxN to a single N.
 
     >>> shift_matrix = ShiftMatrix([1, 2, 3, 4])
-    >>> for i in range(len(shift_matrix)): print shift_matrix[i]
+    >>> print shift_matrix
     [1, 2, 3, 4]
     [4, 1, 2, 3]
     [3, 4, 1, 2]
     [2, 3, 4, 1]
 
+    >>> shift_matrix = ShiftMatrix([1, 2, 3, 4])
+    >>> print len(shift_matrix)
+    16
+
+    The class works with any iterable, for example a string:
     >>> shift_matrix = ShiftMatrix('Test', shift='left')
     >>> print shift_matrix[1]
     estT
@@ -27,6 +32,10 @@
         if not data:
             self.data = list()
         else:
+            try:
+                iter(data)
+            except TypeError, e:
+                raise TypeError("Object is not iterable.")
             self.data = data
 
     def __getitem__(self, index):
@@ -40,7 +49,14 @@
     def __len__(self):
         return len(self.data)
 
+    def __str__(self):
+        return "\n".join(str(row) for row in self.__iter__())
+
+    def __iter__(self):
+        for row_index in range(len(self.data)):
+            yield self.__getitem__(row_index)
+
 if __name__ == "__main__":
-    shift_matrix = ShiftMatrix('Test', shift='left')
-    for i in range(len(shift_matrix)):
-        print shift_matrix[i]
+    shift_matrix = ShiftMatrix([1,2,3],shift='left')
+    for row in shift_matrix:
+        print row

History