--- /dev/null
+'''\r
+A class which presents the reverse of a sequence without duplicating it.\r
+From: "Steven D. Majewski" <sdm7g@elvis.med.virginia.edu>\r
+\r
+It works on mutable or inmutable sequences.\r
+\r
+>>> chars = list(Rev('Hello World!'))\r
+>>> print ''.join(chars)\r
+!dlroW olleH\r
+\r
+The .forw is so you can use anonymous sequences in __init__, and still\r
+keep a reference the forward sequence. )\r
+If you give it a non-anonymous mutable sequence, the reverse sequence\r
+will track the updated values. ( but not reassignment! - another\r
+good reason to use anonymous values in creating the sequence to avoid\r
+confusion. Maybe it should be change to copy input sequence to break\r
+the connection completely ? )\r
+\r
+>>> nnn = range(3)\r
+>>> rnn = Rev(nnn)\r
+>>> for n in rnn: print n\r
+...\r
+2\r
+1\r
+0\r
+>>> for n in range(4, 6): nnn.append(n) # update nnn\r
+...\r
+>>> for n in rnn: print n # prints reversed updated values\r
+...\r
+5\r
+4\r
+2\r
+1\r
+0\r
+>>> nnn = nnn[1:-1]\r
+>>> nnn\r
+[1, 2, 4]\r
+>>> for n in rnn: print n # prints reversed values of old nnn\r
+...\r
+5\r
+4\r
+2\r
+1\r
+0\r
+\r
+#\r
+>>> WH = Rev('Hello World!')\r
+>>> print WH.forw, WH.back\r
+Hello World! !dlroW olleH\r
+>>> nnn = Rev(range(1, 10))\r
+>>> print nnn.forw\r
+[1, 2, 3, 4, 5, 6, 7, 8, 9]\r
+>>> print nnn.back\r
+[9, 8, 7, 6, 5, 4, 3, 2, 1]\r
+\r
+>>> rrr = Rev(nnn)\r
+>>> rrr\r
+<1, 2, 3, 4, 5, 6, 7, 8, 9>\r
+\r
+'''\r
+\r
+class Rev:\r
+ def __init__(self, seq):\r
+ self.forw = seq\r
+ self.back = self\r
+\r
+ def __len__(self):\r
+ return len(self.forw)\r
+\r
+ def __getitem__(self, j):\r
+ return self.forw[-(j + 1)]\r
+\r
+ def __repr__(self):\r
+ seq = self.forw\r
+ if isinstance(seq, list):\r
+ wrap = '[]'\r
+ sep = ', '\r
+ elif isinstance(seq, tuple):\r
+ wrap = '()'\r
+ sep = ', '\r
+ elif isinstance(seq, str):\r
+ wrap = ''\r
+ sep = ''\r
+ else:\r
+ wrap = '<>'\r
+ sep = ', '\r
+ outstrs = [str(item) for item in self.back]\r
+ return wrap[:1] + sep.join(outstrs) + wrap[-1:]\r
+\r
+def _test():\r
+ import doctest, Rev\r
+ return doctest.testmod(Rev)\r
+\r
+if __name__ == "__main__":\r
+ _test()\r