annotate Demo/classes/Rev.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b8737a31050c
children b0743d52b6a3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
1 '''
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
2 A class which presents the reverse of a sequence without duplicating it.
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
3 From: "Steven D. Majewski" <sdm7g@elvis.med.virginia.edu>
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
4
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
5 It works on mutable or inmutable sequences.
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
6
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
7 >>> chars = list(Rev('Hello World!'))
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
8 >>> print ''.join(chars)
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
9 !dlroW olleH
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
10
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
11 The .forw is so you can use anonymous sequences in __init__, and still
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
12 keep a reference the forward sequence. )
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
13 If you give it a non-anonymous mutable sequence, the reverse sequence
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
14 will track the updated values. ( but not reassignment! - another
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
15 good reason to use anonymous values in creating the sequence to avoid
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
16 confusion. Maybe it should be change to copy input sequence to break
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
17 the connection completely ? )
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
19 >>> nnn = range(3)
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
20 >>> rnn = Rev(nnn)
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
21 >>> for n in rnn: print n
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
22 ...
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
23 2
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
24 1
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
25 0
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
26 >>> for n in range(4, 6): nnn.append(n) # update nnn
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
27 ...
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
28 >>> for n in rnn: print n # prints reversed updated values
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
29 ...
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
30 5
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
31 4
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
32 2
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
33 1
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
34 0
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
35 >>> nnn = nnn[1:-1]
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
36 >>> nnn
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
37 [1, 2, 4]
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
38 >>> for n in rnn: print n # prints reversed values of old nnn
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
39 ...
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
40 5
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
41 4
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
42 2
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
43 1
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
44 0
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
45
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 #
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
47 >>> WH = Rev('Hello World!')
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
48 >>> print WH.forw, WH.back
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
49 Hello World! !dlroW olleH
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
50 >>> nnn = Rev(range(1, 10))
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
51 >>> print nnn.forw
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
52 [1, 2, 3, 4, 5, 6, 7, 8, 9]
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
53 >>> print nnn.back
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
54 [9, 8, 7, 6, 5, 4, 3, 2, 1]
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
56 >>> rrr = Rev(nnn)
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
57 >>> rrr
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
58 <1, 2, 3, 4, 5, 6, 7, 8, 9>
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
59
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
60 '''
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
61
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 class Rev:
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
63 def __init__(self, seq):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
64 self.forw = seq
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
65 self.back = self
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
66
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
67 def __len__(self):
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
68 return len(self.forw)
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
69
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
70 def __getitem__(self, j):
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
71 return self.forw[-(j + 1)]
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
72
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
73 def __repr__(self):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
74 seq = self.forw
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
75 if isinstance(seq, list):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
76 wrap = '[]'
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
77 sep = ', '
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
78 elif isinstance(seq, tuple):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
79 wrap = '()'
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
80 sep = ', '
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
81 elif isinstance(seq, str):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
82 wrap = ''
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
83 sep = ''
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
84 else:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
85 wrap = '<>'
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
86 sep = ', '
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
87 outstrs = [str(item) for item in self.back]
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
88 return wrap[:1] + sep.join(outstrs) + wrap[-1:]
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
89
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
90 def _test():
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
91 import doctest, Rev
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
92 return doctest.testmod(Rev)
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
93
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
94 if __name__ == "__main__":
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
95 _test()