blob: d36ddb91f34cf4eb70f943b564239a1d43a40e11 [file] [log] [blame]
Raymond Hettingerfe63faa2003-09-10 21:12:59 +00001'''
2A class which presents the reverse of a sequence without duplicating it.
3From: "Steven D. Majewski" <sdm7g@elvis.med.virginia.edu>
Guido van Rossum102abab1993-10-30 12:38:16 +00004
Raymond Hettingerfe63faa2003-09-10 21:12:59 +00005It works on mutable or inmutable sequences.
Guido van Rossum102abab1993-10-30 12:38:16 +00006
Alexander Belopolsky06360402010-07-04 17:38:32 +00007>>> Rev('Hello World!')
Raymond Hettingerfe63faa2003-09-10 21:12:59 +00008!dlroW olleH
9
10The .forw is so you can use anonymous sequences in __init__, and still
11keep a reference the forward sequence. )
12If you give it a non-anonymous mutable sequence, the reverse sequence
13will track the updated values. ( but not reassignment! - another
14good reason to use anonymous values in creating the sequence to avoid
15confusion. Maybe it should be change to copy input sequence to break
16the connection completely ? )
17
Alexander Belopolsky06360402010-07-04 17:38:32 +000018>>> nnn = list(range(3))
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000019>>> rnn = Rev(nnn)
Alexander Belopolsky06360402010-07-04 17:38:32 +000020>>> for n in rnn: n
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000021...
222
231
240
25>>> for n in range(4, 6): nnn.append(n) # update nnn
26...
Alexander Belopolsky06360402010-07-04 17:38:32 +000027>>> for n in rnn: n # prints reversed updated values
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000028...
295
304
312
321
330
34>>> nnn = nnn[1:-1]
35>>> nnn
36[1, 2, 4]
Alexander Belopolsky06360402010-07-04 17:38:32 +000037>>> for n in rnn: n # prints reversed values of old nnn
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000038...
395
404
412
421
430
44
45#
46>>> WH = Rev('Hello World!')
Alexander Belopolsky06360402010-07-04 17:38:32 +000047>>> print(WH.forw, WH.back)
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000048Hello World! !dlroW olleH
Alexander Belopolsky06360402010-07-04 17:38:32 +000049>>> nnn = Rev(list(range(1, 10)))
50>>> print(nnn.forw)
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000051[1, 2, 3, 4, 5, 6, 7, 8, 9]
Alexander Belopolsky06360402010-07-04 17:38:32 +000052>>> print(nnn.back)
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000053[9, 8, 7, 6, 5, 4, 3, 2, 1]
54
Alexander Belopolsky06360402010-07-04 17:38:32 +000055>>> Rev(nnn)
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000056<1, 2, 3, 4, 5, 6, 7, 8, 9>
57
58'''
59
Guido van Rossum102abab1993-10-30 12:38:16 +000060class Rev:
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000061 def __init__(self, seq):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000062 self.forw = seq
63 self.back = self
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000064
65 def __len__(self):
66 return len(self.forw)
67
68 def __getitem__(self, j):
69 return self.forw[-(j + 1)]
70
71 def __repr__(self):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000072 seq = self.forw
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000073 if isinstance(seq, list):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000074 wrap = '[]'
75 sep = ', '
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000076 elif isinstance(seq, tuple):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000077 wrap = '()'
78 sep = ', '
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000079 elif isinstance(seq, str):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000080 wrap = ''
81 sep = ''
82 else:
83 wrap = '<>'
84 sep = ', '
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000085 outstrs = [str(item) for item in self.back]
86 return wrap[:1] + sep.join(outstrs) + wrap[-1:]
87
88def _test():
89 import doctest, Rev
90 return doctest.testmod(Rev)
91
92if __name__ == "__main__":
93 _test()