|
1 | | -# A class which presents the reverse of a sequence without duplicating it. |
2 | | -# From: "Steven D. Majewski" <[email protected]> |
| 1 | +''' |
| 2 | +A class which presents the reverse of a sequence without duplicating it. |
| 3 | +From: "Steven D. Majewski" <[email protected]> |
| 4 | +
|
| 5 | +It works on mutable or inmutable sequences. |
| 6 | +
|
| 7 | +>>> chars = list(Rev('Hello World!')) |
| 8 | +>>> print ''.join(chars) |
| 9 | +!dlroW olleH |
| 10 | +
|
| 11 | +The .forw is so you can use anonymous sequences in __init__, and still |
| 12 | +keep a reference the forward sequence. ) |
| 13 | +If you give it a non-anonymous mutable sequence, the reverse sequence |
| 14 | +will track the updated values. ( but not reassignment! - another |
| 15 | +good reason to use anonymous values in creating the sequence to avoid |
| 16 | +confusion. Maybe it should be change to copy input sequence to break |
| 17 | +the connection completely ? ) |
| 18 | +
|
| 19 | +>>> nnn = range(3) |
| 20 | +>>> rnn = Rev(nnn) |
| 21 | +>>> for n in rnn: print n |
| 22 | +... |
| 23 | +2 |
| 24 | +1 |
| 25 | +0 |
| 26 | +>>> for n in range(4, 6): nnn.append(n) # update nnn |
| 27 | +... |
| 28 | +>>> for n in rnn: print n # prints reversed updated values |
| 29 | +... |
| 30 | +5 |
| 31 | +4 |
| 32 | +2 |
| 33 | +1 |
| 34 | +0 |
| 35 | +>>> nnn = nnn[1:-1] |
| 36 | +>>> nnn |
| 37 | +[1, 2, 4] |
| 38 | +>>> for n in rnn: print n # prints reversed values of old nnn |
| 39 | +... |
| 40 | +5 |
| 41 | +4 |
| 42 | +2 |
| 43 | +1 |
| 44 | +0 |
3 | 45 |
|
4 | | -# It works on mutable or inmutable sequences. |
5 | | -# |
6 | | -# >>> for c in Rev( 'Hello World!' ) : sys.stdout.write( c ) |
7 | | -# ... else: sys.stdout.write( '\n' ) |
8 | | -# ... |
9 | | -# !dlroW olleH |
10 | | -# |
11 | | -# The .forw is so you can use anonymous sequences in __init__, and still |
12 | | -# keep a reference the forward sequence. ) |
13 | | -# If you give it a non-anonymous mutable sequence, the reverse sequence |
14 | | -# will track the updated values. ( but not reassignment! - another |
15 | | -# good reason to use anonymous values in creating the sequence to avoid |
16 | | -# confusion. Maybe it should be change to copy input sequence to break |
17 | | -# the connection completely ? ) |
18 | | -# |
19 | | -# >>> nnn = range( 0, 3 ) |
20 | | -# >>> rnn = Rev( nnn ) |
21 | | -# >>> for n in rnn: print n |
22 | | -# ... |
23 | | -# 2 |
24 | | -# 1 |
25 | | -# 0 |
26 | | -# >>> for n in range( 4, 6 ): nnn.append( n ) # update nnn |
27 | | -# ... |
28 | | -# >>> for n in rnn: print n # prints reversed updated values |
29 | | -# ... |
30 | | -# 5 |
31 | | -# 4 |
32 | | -# 2 |
33 | | -# 1 |
34 | | -# 0 |
35 | | -# >>> nnn = nnn[1:-1] |
36 | | -# >>> nnn |
37 | | -# [1, 2, 4] |
38 | | -# >>> for n in rnn: print n # prints reversed values of old nnn |
39 | | -# ... |
40 | | -# 5 |
41 | | -# 4 |
42 | | -# 2 |
43 | | -# 1 |
44 | | -# 0 |
45 | | -# >>> |
46 | | -# |
47 | | -# WH = Rev( 'Hello World!' ) |
48 | | -# print WH.forw, WH.back |
49 | | -# nnn = Rev( range( 1, 10 ) ) |
50 | | -# print nnn.forw |
51 | | -# print nnn |
52 | | -# |
53 | | -# produces output: |
54 | | -# |
55 | | -# Hello World! !dlroW olleH |
56 | | -# [1, 2, 3, 4, 5, 6, 7, 8, 9] |
57 | | -# [9, 8, 7, 6, 5, 4, 3, 2, 1] |
58 | 46 | # |
59 | | -# >>>rrr = Rev( nnn ) |
60 | | -# >>>rrr |
61 | | -# <1, 2, 3, 4, 5, 6, 7, 8, 9> |
| 47 | +>>> WH = Rev('Hello World!') |
| 48 | +>>> print WH.forw, WH.back |
| 49 | +Hello World! !dlroW olleH |
| 50 | +>>> nnn = Rev(range(1, 10)) |
| 51 | +>>> print nnn.forw |
| 52 | +[1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 53 | +>>> print nnn.back |
| 54 | +[9, 8, 7, 6, 5, 4, 3, 2, 1] |
| 55 | +
|
| 56 | +>>> rrr = Rev(nnn) |
| 57 | +>>> rrr |
| 58 | +<1, 2, 3, 4, 5, 6, 7, 8, 9> |
| 59 | +
|
| 60 | +''' |
62 | 61 |
|
63 | | -from string import joinfields |
64 | 62 | class Rev: |
65 | | - def __init__( self, seq ): |
| 63 | + def __init__(self, seq): |
66 | 64 | self.forw = seq |
67 | 65 | self.back = self |
68 | | - def __len__( self ): |
69 | | - return len( self.forw ) |
70 | | - def __getitem__( self, j ): |
71 | | - return self.forw[ -( j + 1 ) ] |
72 | | - def __repr__( self ): |
| 66 | + |
| 67 | + def __len__(self): |
| 68 | + return len(self.forw) |
| 69 | + |
| 70 | + def __getitem__(self, j): |
| 71 | + return self.forw[-(j + 1)] |
| 72 | + |
| 73 | + def __repr__(self): |
73 | 74 | seq = self.forw |
74 | | - if type(seq) == type( [] ) : |
| 75 | + if isinstance(seq, list): |
75 | 76 | wrap = '[]' |
76 | 77 | sep = ', ' |
77 | | - elif type(seq) == type( () ) : |
| 78 | + elif isinstance(seq, tuple): |
78 | 79 | wrap = '()' |
79 | 80 | sep = ', ' |
80 | | - elif type(seq) == type( '' ) : |
| 81 | + elif isinstance(seq, str): |
81 | 82 | wrap = '' |
82 | 83 | sep = '' |
83 | 84 | else: |
84 | 85 | wrap = '<>' |
85 | 86 | sep = ', ' |
86 | | - outstrs = [] |
87 | | - for item in self.back : |
88 | | - outstrs.append( str( item ) ) |
89 | | - return wrap[:1] + joinfields( outstrs, sep ) + wrap[-1:] |
| 87 | + outstrs = [str(item) for item in self.back] |
| 88 | + return wrap[:1] + sep.join(outstrs) + wrap[-1:] |
| 89 | + |
| 90 | +def _test(): |
| 91 | + import doctest, Rev |
| 92 | + return doctest.testmod(Rev) |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + _test() |
0 commit comments