Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe63faa

Browse files
committed
SF patch #803449: modernize demo scripts
(Contributed by George Yoshida.)
1 parent 7c4d8f3 commit fe63faa

2 files changed

Lines changed: 89 additions & 93 deletions

File tree

Demo/classes/Rev.py

Lines changed: 79 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,95 @@
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
345
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]
5846
#
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+
'''
6261

63-
from string import joinfields
6462
class Rev:
65-
def __init__( self, seq ):
63+
def __init__(self, seq):
6664
self.forw = seq
6765
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):
7374
seq = self.forw
74-
if type(seq) == type( [] ) :
75+
if isinstance(seq, list):
7576
wrap = '[]'
7677
sep = ', '
77-
elif type(seq) == type( () ) :
78+
elif isinstance(seq, tuple):
7879
wrap = '()'
7980
sep = ', '
80-
elif type(seq) == type( '' ) :
81+
elif isinstance(seq, str):
8182
wrap = ''
8283
sep = ''
8384
else:
8485
wrap = '<>'
8586
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()

Demo/classes/Vec.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,42 @@
22

33

44
def vec(*v):
5-
return apply(Vec, v)
5+
return Vec(*v)
66

77

88
class Vec:
99

1010
def __init__(self, *v):
11-
self.v = []
12-
for x in v:
13-
self.v.append(x)
14-
11+
self.v = list(v)
1512

1613
def fromlist(self, v):
17-
self.v = []
18-
if type(v) <> type([]):
14+
if not isinstance(v, list):
1915
raise TypeError
2016
self.v = v[:]
2117
return self
2218

23-
2419
def __repr__(self):
25-
return 'vec(' + `self.v`[1:-1] + ')'
20+
return 'vec(' + repr(self.v)[1:-1] + ')'
2621

2722
def __len__(self):
2823
return len(self.v)
2924

3025
def __getitem__(self, i):
3126
return self.v[i]
3227

33-
def __add__(a, b):
28+
def __add__(self, other):
3429
# Element-wise addition
35-
v = []
36-
for i in range(len(a)):
37-
v.append(a[i] + b[i])
30+
v = map(lambda x, y: x+y, self, other)
3831
return Vec().fromlist(v)
3932

40-
def __sub__(a, b):
33+
def __sub__(self, other):
4134
# Element-wise subtraction
42-
v = []
43-
for i in range(len(a)):
44-
v.append(a[i] - b[i])
35+
v = map(lambda x, y: x-y, self, other)
4536
return Vec().fromlist(v)
4637

4738
def __mul__(self, scalar):
4839
# Multiply by scalar
49-
v = []
50-
for i in range(len(self.v)):
51-
v.append(self.v[i]*scalar)
40+
v = map(lambda x: x*scalar, self.v)
5241
return Vec().fromlist(v)
5342

5443

@@ -59,6 +48,7 @@ def test():
5948
print a
6049
print b
6150
print a+b
51+
print a-b
6252
print a*3.0
6353

6454
test()

0 commit comments

Comments
 (0)