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

Skip to content

Commit 7565b93

Browse files
committed
*** empty log message ***
1 parent 3cbc16d commit 7565b93

8 files changed

Lines changed: 51 additions & 49 deletions

File tree

Demo/classes/Complex.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44
from math import sqrt
55

66

7-
def complex(re, im):
8-
return Complex().init(re, im)
7+
class complex:
98

10-
11-
class Complex:
12-
13-
def init(self, re, im):
9+
def __init__(self, re, im):
1410
self.re = float(re)
1511
self.im = float(im)
16-
return self
12+
13+
def __coerce__(self, other):
14+
if type(other) == type(self):
15+
if other.__class__ == self.__class__:
16+
return self, other
17+
else:
18+
raise TypeError, 'cannot coerce to complex'
19+
else:
20+
# The cast to float() may raise an exception!
21+
return self, complex(float(other), 0.0)
1722

1823
def __repr__(self):
1924
return 'complex' + `self.re, self.im`
@@ -60,9 +65,16 @@ def __neg__(self):
6065
def test():
6166
a = complex(2, 0)
6267
b = complex(3, 4)
63-
print a, b
64-
print a+b, a-b, a*b, a/b
65-
print b+a, b-a, b*a, b/a
68+
print a
69+
print b
70+
print a+b
71+
print a-b
72+
print a*b
73+
print a/b
74+
print b+a
75+
print b-a
76+
print b*a
77+
print b/a
6678
i = complex(0, 1)
6779
print i, i*i, i*i*i, i*i*i*i
6880
j = complex(1, 1)

Demo/classes/Dbm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def has_key(self, key):
4141

4242

4343
def test():
44-
d = Dbm('@dbm', 'rw', 0666)
44+
d = Dbm('@dbm', 'rw', 0600)
4545
print d
4646
while 1:
4747
try:
48-
key = eval(raw_input('key: '))
48+
key = input('key: ')
4949
if d.has_key(key):
5050
value = d[key]
5151
print 'currently:', value

Demo/classes/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Examples of classes that implement special operators (see class.doc):
1+
Examples of classes that implement special operators (see reference manual):
22

33
Complex.py Complex numbers
44
Dates.py Date manipulation package by Tim Peters

Demo/classes/Range.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def range(*a):
1616
start, stop, step = a
1717
else:
1818
raise TypeError, 'range() needs 1-3 arguments'
19-
return Range().init(start, stop, step)
19+
return Range(start, stop, step)
2020

2121

2222
# Class implementing a range object.
@@ -26,14 +26,13 @@ def range(*a):
2626
class Range:
2727

2828
# initialization -- should be called only by range() above
29-
def init(self, start, stop, step):
29+
def __init__(self, start, stop, step):
3030
if step == 0:
3131
raise ValueError, 'range() called with zero step'
3232
self.start = start
3333
self.stop = stop
3434
self.step = step
3535
self.len = max(0, int((self.stop - self.start) / self.step))
36-
return self
3736

3837
# implement `x` and is also used by print x
3938
def __repr__(self):

Demo/classes/Rat.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
def rat(num, den):
5-
return Rat().init(num, den)
5+
return Rat(num, den)
66

77

88
def gcd(a, b):
@@ -13,13 +13,12 @@ def gcd(a, b):
1313

1414
class Rat:
1515

16-
def init(self, num, den):
16+
def __init__(self, num, den):
1717
if den == 0:
1818
raise ZeroDivisionError, 'rat(x, 0)'
1919
g = gcd(num, den)
2020
self.num = num/g
2121
self.den = den/g
22-
return self
2322

2423
def __repr__(self):
2524
return 'rat' + `self.num, self.den`
@@ -49,8 +48,10 @@ def __coerce__(a, b):
4948
return a, rat(b, 1L)
5049
if t == type(0.0):
5150
return a.__float__(), b
51+
if t == type(a) and a.__class__ == b.__class__:
52+
return a, b
5253
raise TypeError, 'Rat.__coerce__: bad other arg'
53-
54+
5455
def __add__(a, b):
5556
if type(b) <> type(a):
5657
a, b = a.__coerce__(b)
@@ -93,4 +94,4 @@ def test():
9394
except ZeroDivisionError:
9495
print 'OK'
9596

96-
#test()
97+
test()

Demo/classes/Rev.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# ...
99
# !dlroW olleH
1010
#
11-
# The .forw is so you can use anonymous sequences in init, and still
11+
# The .forw is so you can use anonymous sequences in __init__, and still
1212
# keep a reference the forward sequence. )
1313
# If you give it a non-anonymous mutable sequence, the reverse sequence
1414
# will track the updated values. ( but not reassignment! - another

Demo/classes/Vec.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33

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

77

88
class Vec:
99

10-
def init(self, *v):
10+
def __init__(self, *v):
1111
self.v = []
1212
for x in v:
1313
self.v.append(x)
14-
return self
1514

1615

1716
def fromlist(self, v):

Demo/classes/bitvec.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _check_slice(len, i, j):
4848

4949
class BitVec:
5050

51-
def init(self, *params):
51+
def __init__(self, *params):
5252
self._data = 0L
5353
self._len = 0
5454
if not len(params):
@@ -93,20 +93,12 @@ def init(self, *params):
9393
else:
9494
raise error, 'bitvec() requires 0 -- 2 parameter(s)'
9595

96-
return self
97-
98-
99-
def _init(self, data, len):
100-
self._data = data
101-
self._len = len
102-
return self
103-
10496

10597
def append(self, item):
10698
#_check_value(item)
10799
#self[self._len:self._len] = [item]
108100
self[self._len:self._len] = \
109-
BitVec()._init(long(not not item), 1)
101+
BitVec(long(not not item), 1)
110102

111103

112104
def count(self, value):
@@ -138,7 +130,7 @@ def index(self, value):
138130
def insert(self, index, item):
139131
#_check_value(item)
140132
#self[index:index] = [item]
141-
self[index:index] = BitVec()._init(long(not not item), 1)
133+
self[index:index] = BitVec(long(not not item), 1)
142134

143135

144136
def remove(self, value):
@@ -163,7 +155,7 @@ def sort(self):
163155

164156

165157
def copy(self):
166-
return BitVec()._init(self._data, self._len)
158+
return BitVec(self._data, self._len)
167159

168160

169161
def seq(self):
@@ -229,7 +221,7 @@ def __getslice__(self, i, j):
229221
#rprt(`self`+'.__getslice__'+`i, j`+'\n')
230222
i, j = _check_slice(self._len, i, j)
231223
if i >= j:
232-
return BitVec()._init(0L, 0)
224+
return BitVec(0L, 0)
233225
if i:
234226
ndata = self._data >> i
235227
else:
@@ -239,7 +231,7 @@ def __getslice__(self, i, j):
239231
#we'll have to invent faster variants here
240232
#e.g. mod_2exp
241233
ndata = ndata & ((1L << nlength) - 1)
242-
return BitVec()._init(ndata, nlength)
234+
return BitVec(ndata, nlength)
243235

244236
def __setslice__(self, i, j, sequence, *rest):
245237
#rprt(`self`+'.__setslice__'+`(i, j, sequence) + rest`+'\n')
@@ -274,16 +266,16 @@ def __mul__(self, multiplier):
274266
if type(multiplier) != type(0):
275267
raise TypeError, 'sequence subscript not int'
276268
if multiplier <= 0:
277-
return BitVec()._init(0L, 0)
269+
return BitVec(0L, 0)
278270
elif multiplier == 1:
279271
return self.copy()
280272
#handle special cases all 0 or all 1...
281273
if self._data == 0L:
282-
return BitVec()._init(0L, self._len * multiplier)
274+
return BitVec(0L, self._len * multiplier)
283275
elif (~self)._data == 0L:
284-
return ~BitVec()._init(0L, self._len * multiplier)
276+
return ~BitVec(0L, self._len * multiplier)
285277
#otherwise el cheapo again...
286-
retval = BitVec()._init(0L, 0)
278+
retval = BitVec(0L, 0)
287279
while multiplier:
288280
retval, multiplier = retval + self, multiplier - 1
289281
return retval
@@ -293,7 +285,7 @@ def __and__(self, otherseq, *rest):
293285
if type(otherseq) != type(self):
294286
otherseq = apply(bitvec, (otherseq, ) + rest)
295287
#sequence is now of our own type
296-
return BitVec()._init(self._data & otherseq._data, \
288+
return BitVec(self._data & otherseq._data, \
297289
min(self._len, otherseq._len))
298290

299291

@@ -302,7 +294,7 @@ def __xor__(self, otherseq, *rest):
302294
if type(otherseq) != type(self):
303295
otherseq = apply(bitvec, (otherseq, ) + rest)
304296
#sequence is now of our own type
305-
return BitVec()._init(self._data ^ otherseq._data, \
297+
return BitVec(self._data ^ otherseq._data, \
306298
max(self._len, otherseq._len))
307299

308300

@@ -311,13 +303,13 @@ def __or__(self, otherseq, *rest):
311303
if type(otherseq) != type(self):
312304
otherseq = apply(bitvec, (otherseq, ) + rest)
313305
#sequence is now of our own type
314-
return BitVec()._init(self._data | otherseq._data, \
306+
return BitVec(self._data | otherseq._data, \
315307
max(self._len, otherseq._len))
316308

317309

318310
def __invert__(self):
319311
#rprt(`self`+'.__invert__()\n')
320-
return BitVec()._init(~self._data & ((1L << self._len) - 1), \
312+
return BitVec(~self._data & ((1L << self._len) - 1), \
321313
self._len)
322314

323315
def __coerce__(self, otherseq, *rest):
@@ -337,5 +329,4 @@ def __float__(self):
337329
return float(self._data)
338330

339331

340-
def bitvec(params):
341-
return apply(BitVec().init, params)
332+
bitvec = BitVec

0 commit comments

Comments
 (0)