diff options
Diffstat (limited to 'Demo/classes')
-rwxr-xr-x | Demo/classes/Complex.py | 30 | ||||
-rwxr-xr-x | Demo/classes/Dates.py | 40 | ||||
-rwxr-xr-x | Demo/classes/Dbm.py | 26 | ||||
-rwxr-xr-x | Demo/classes/Range.py | 10 | ||||
-rwxr-xr-x | Demo/classes/Rat.py | 72 | ||||
-rwxr-xr-x | Demo/classes/Vec.py | 16 | ||||
-rwxr-xr-x | Demo/classes/bitvec.py | 75 |
7 files changed, 134 insertions, 135 deletions
diff --git a/Demo/classes/Complex.py b/Demo/classes/Complex.py index 2019cdf..b6c4b10 100755 --- a/Demo/classes/Complex.py +++ b/Demo/classes/Complex.py @@ -114,7 +114,7 @@ class Complex: self.__dict__['im'] = _im def __setattr__(self, name, value): - raise TypeError, 'Complex numbers are immutable' + raise TypeError('Complex numbers are immutable') def __hash__(self): if not self.im: @@ -144,17 +144,17 @@ class Complex: def __int__(self): if self.im: - raise ValueError, "can't convert Complex with nonzero im to int" + raise ValueError("can't convert Complex with nonzero im to int") return int(self.re) def __long__(self): if self.im: - raise ValueError, "can't convert Complex with nonzero im to long" - return long(self.re) + raise ValueError("can't convert Complex with nonzero im to long") + return int(self.re) def __float__(self): if self.im: - raise ValueError, "can't convert Complex with nonzero im to float" + raise ValueError("can't convert Complex with nonzero im to float") return float(self.re) def __cmp__(self, other): @@ -199,7 +199,7 @@ class Complex: def __div__(self, other): other = ToComplex(other) d = float(other.re*other.re + other.im*other.im) - if not d: raise ZeroDivisionError, 'Complex division' + if not d: raise ZeroDivisionError('Complex division') return Complex((self.re*other.re + self.im*other.im) / d, (self.im*other.re - self.re*other.im) / d) @@ -209,10 +209,10 @@ class Complex: def __pow__(self, n, z=None): if z is not None: - raise TypeError, 'Complex does not support ternary pow()' + raise TypeError('Complex does not support ternary pow()') if IsComplex(n): if n.im: - if self.im: raise TypeError, 'Complex to the Complex power' + if self.im: raise TypeError('Complex to the Complex power') else: return exp(math.log(self.re)*n) n = n.re r = pow(self.abs(), n) @@ -229,21 +229,21 @@ def exp(z): def checkop(expr, a, b, value, fuzz = 1e-6): - print ' ', a, 'and', b, + print(' ', a, 'and', b, end=' ') try: result = eval(expr) except: result = sys.exc_info()[0] - print '->', result + print('->', result) if isinstance(result, str) or isinstance(value, str): ok = (result == value) else: ok = abs(result - value) <= fuzz if not ok: - print '!!\t!!\t!! should be', value, 'diff', abs(result - value) + print('!!\t!!\t!! should be', value, 'diff', abs(result - value)) def test(): - print 'test constructors' + print('test constructors') constructor_test = ( # "expect" is an array [re,im] "got" the Complex. ( (0,0), Complex() ), @@ -260,9 +260,9 @@ def test(): for t in constructor_test: cnt[0] += 1 if ((t[0][0]!=t[1].re)or(t[0][1]!=t[1].im)): - print " expected", t[0], "got", t[1] + print(" expected", t[0], "got", t[1]) cnt[1] += 1 - print " ", cnt[1], "of", cnt[0], "tests failed" + print(" ", cnt[1], "of", cnt[0], "tests failed") # test operators testsuite = { 'a+b': [ @@ -310,7 +310,7 @@ def test(): ], } for expr in sorted(testsuite): - print expr + ':' + print(expr + ':') t = (expr,) for item in testsuite[expr]: checkop(*(t+item)) diff --git a/Demo/classes/Dates.py b/Demo/classes/Dates.py index 6494b6a..afb0ff2 100755 --- a/Demo/classes/Dates.py +++ b/Demo/classes/Dates.py @@ -57,7 +57,7 @@ for dim in _DAYS_IN_MONTH: dbm = dbm + dim del dbm, dim -_INT_TYPES = type(1), type(1L) +_INT_TYPES = type(1), type(1) def _is_leap(year): # 1 if leap year, else 0 if year % 4 != 0: return 0 @@ -68,7 +68,7 @@ def _days_in_year(year): # number of days in year return 365 + _is_leap(year) def _days_before_year(year): # number of days before year - return year*365L + (year+3)/4 - (year+99)/100 + (year+399)/400 + return year*365 + (year+3)/4 - (year+99)/100 + (year+399)/400 def _days_in_month(month, year): # number of days in month of year if month == 2 and _is_leap(year): return 29 @@ -86,7 +86,7 @@ _DI400Y = _days_before_year(400) # number of days in 400 years def _num2date(n): # return date with ordinal n if type(n) not in _INT_TYPES: - raise TypeError, 'argument must be integer: %r' % type(n) + raise TypeError('argument must be integer: %r' % type(n)) ans = Date(1,1,1) # arguments irrelevant; just getting a Date obj del ans.ord, ans.month, ans.day, ans.year # un-initialize it @@ -120,17 +120,17 @@ def _num2day(n): # return weekday name of day with ordinal n class Date: def __init__(self, month, day, year): if not 1 <= month <= 12: - raise ValueError, 'month must be in 1..12: %r' % (month,) + raise ValueError('month must be in 1..12: %r' % (month,)) dim = _days_in_month(month, year) if not 1 <= day <= dim: - raise ValueError, 'day must be in 1..%r: %r' % (dim, day) + raise ValueError('day must be in 1..%r: %r' % (dim, day)) self.month, self.day, self.year = month, day, year self.ord = _date2num(self) # don't allow setting existing attributes def __setattr__(self, name, value): - if self.__dict__.has_key(name): - raise AttributeError, 'read-only attribute ' + name + if name in self.__dict__: + raise AttributeError('read-only attribute ' + name) self.__dict__[name] = value def __cmp__(self, other): @@ -151,7 +151,7 @@ class Date: # Python 1.1 coerces neither int+date nor date+int def __add__(self, n): if type(n) not in _INT_TYPES: - raise TypeError, 'can\'t add %r to date' % type(n) + raise TypeError('can\'t add %r to date' % type(n)) return _num2date(self.ord + n) __radd__ = __add__ # handle int+date @@ -164,7 +164,7 @@ class Date: # complain about int-date def __rsub__(self, other): - raise TypeError, 'Can\'t subtract date from integer' + raise TypeError('Can\'t subtract date from integer') def weekday(self): return _num2day(self.ord) @@ -179,30 +179,30 @@ def test(firstyear, lastyear): a = Date(9,30,1913) b = Date(9,30,1914) if repr(a) != 'Tue 30 Sep 1913': - raise DateTestError, '__repr__ failure' + raise DateTestError('__repr__ failure') if (not a < b) or a == b or a > b or b != b: - raise DateTestError, '__cmp__ failure' + raise DateTestError('__cmp__ failure') if a+365 != b or 365+a != b: - raise DateTestError, '__add__ failure' + raise DateTestError('__add__ failure') if b-a != 365 or b-365 != a: - raise DateTestError, '__sub__ failure' + raise DateTestError('__sub__ failure') try: x = 1 - a - raise DateTestError, 'int-date should have failed' + raise DateTestError('int-date should have failed') except TypeError: pass try: x = a + b - raise DateTestError, 'date+date should have failed' + raise DateTestError('date+date should have failed') except TypeError: pass if a.weekday() != 'Tuesday': - raise DateTestError, 'weekday() failure' + raise DateTestError('weekday() failure') if max(a,b) is not b or min(a,b) is not a: - raise DateTestError, 'min/max failure' + raise DateTestError('min/max failure') d = {a-1:b, b:a+1} if d[b-366] != b or d[a+(b-a)] != Date(10,1,1913): - raise DateTestError, 'dictionary failure' + raise DateTestError('dictionary failure') # verify date<->number conversions for first and last days for # all years in firstyear .. lastyear @@ -214,9 +214,9 @@ def test(firstyear, lastyear): lord = ford + _days_in_year(y) - 1 fd, ld = Date(1,1,y), Date(12,31,y) if (fd.ord,ld.ord) != (ford,lord): - raise DateTestError, ('date->num failed', y) + raise DateTestError('date->num failed', y) fd, ld = _num2date(ford), _num2date(lord) if (1,1,y,12,31,y) != \ (fd.month,fd.day,fd.year,ld.month,ld.day,ld.year): - raise DateTestError, ('num->date failed', y) + raise DateTestError('num->date failed', y) y = y + 1 diff --git a/Demo/classes/Dbm.py b/Demo/classes/Dbm.py index 482806a..fd73571 100755 --- a/Demo/classes/Dbm.py +++ b/Demo/classes/Dbm.py @@ -12,7 +12,7 @@ class Dbm: def __repr__(self): s = '' - for key in self.keys(): + for key in list(self.keys()): t = repr(key) + ': ' + repr(self[key]) if s: t = ', ' + t s = s + t @@ -32,35 +32,35 @@ class Dbm: def keys(self): res = [] - for key in self.db.keys(): + for key in list(self.db.keys()): res.append(eval(key)) return res def has_key(self, key): - return self.db.has_key(repr(key)) + return repr(key) in self.db def test(): - d = Dbm('@dbm', 'rw', 0600) - print d + d = Dbm('@dbm', 'rw', 0o600) + print(d) while 1: try: - key = input('key: ') - if d.has_key(key): + key = eval(input('key: ')) + if key in d: value = d[key] - print 'currently:', value - value = input('value: ') + print('currently:', value) + value = eval(input('value: ')) if value == None: del d[key] else: d[key] = value except KeyboardInterrupt: - print '' - print d + print('') + print(d) except EOFError: - print '[eof]' + print('[eof]') break - print d + print(d) test() diff --git a/Demo/classes/Range.py b/Demo/classes/Range.py index 3f1daae..0f84157 100755 --- a/Demo/classes/Range.py +++ b/Demo/classes/Range.py @@ -60,7 +60,7 @@ class oldrange: if 0 <= i <= self.len: return self.start + self.step * i else: - raise IndexError, 'range[i] index out of range' + raise IndexError('range[i] index out of range') def test(): @@ -73,7 +73,7 @@ def test(): raise Exception("error in implementation:\ncorrect = %s" "\nold-style = %s\ngenerator = %s" % (correct_result, oldrange_result, genrange_result)) - print "Timings for range(1000):" + print("Timings for range(1000):") t1 = time.time() for i in oldrange(1000): pass @@ -84,9 +84,9 @@ def test(): for i in __builtin__.range(1000): pass t4 = time.time() - print t2-t1, 'sec (old-style class)' - print t3-t2, 'sec (generator)' - print t4-t3, 'sec (built-in)' + print(t2-t1, 'sec (old-style class)') + print(t3-t2, 'sec (generator)') + print(t4-t3, 'sec (built-in)') if __name__ == '__main__': diff --git a/Demo/classes/Rat.py b/Demo/classes/Rat.py index 24670b6..8caf1c5 100755 --- a/Demo/classes/Rat.py +++ b/Demo/classes/Rat.py @@ -42,7 +42,7 @@ class Rat: def __init__(self, num, den = 1): if den == 0: - raise ZeroDivisionError, 'rat(x, 0)' + raise ZeroDivisionError('rat(x, 0)') # normalize @@ -106,9 +106,9 @@ class Rat: return rat(a.__num * b.__den + b.__num * a.__den, a.__den * b.__den) except OverflowError: - return rat(long(a.__num) * long(b.__den) + - long(b.__num) * long(a.__den), - long(a.__den) * long(b.__den)) + return rat(int(a.__num) * int(b.__den) + + int(b.__num) * int(a.__den), + int(a.__den) * int(b.__den)) def __radd__(b, a): return Rat(a) + b @@ -119,9 +119,9 @@ class Rat: return rat(a.__num * b.__den - b.__num * a.__den, a.__den * b.__den) except OverflowError: - return rat(long(a.__num) * long(b.__den) - - long(b.__num) * long(a.__den), - long(a.__den) * long(b.__den)) + return rat(int(a.__num) * int(b.__den) - + int(b.__num) * int(a.__den), + int(a.__den) * int(b.__den)) def __rsub__(b, a): return Rat(a) - b @@ -131,8 +131,8 @@ class Rat: try: return rat(a.__num * b.__num, a.__den * b.__den) except OverflowError: - return rat(long(a.__num) * long(b.__num), - long(a.__den) * long(b.__den)) + return rat(int(a.__num) * int(b.__num), + int(a.__den) * int(b.__den)) def __rmul__(b, a): return Rat(a) * b @@ -142,8 +142,8 @@ class Rat: try: return rat(a.__num * b.__den, a.__den * b.__num) except OverflowError: - return rat(long(a.__num) * long(b.__den), - long(a.__den) * long(b.__num)) + return rat(int(a.__num) * int(b.__den), + int(a.__den) * int(b.__num)) def __rdiv__(b, a): return Rat(a) / b @@ -154,7 +154,7 @@ class Rat: try: div = int(div) except OverflowError: - div = long(div) + div = int(div) return a - b * div def __rmod__(b, a): @@ -175,8 +175,8 @@ class Rat: try: return rat(a.__num ** b.__num, a.__den ** b.__num) except OverflowError: - return rat(long(a.__num) ** b.__num, - long(a.__den) ** b.__num) + return rat(int(a.__num) ** b.__num, + int(a.__den) ** b.__num) def __rpow__(b, a): return Rat(a) ** b @@ -187,7 +187,7 @@ class Rat: return rat(-a.__num, a.__den) except OverflowError: # a.__num == sys.maxint - return rat(-long(a.__num), a.__den) + return rat(-int(a.__num), a.__den) # abs(a) def __abs__(a): @@ -199,7 +199,7 @@ class Rat: # long(a) def __long__(a): - return long(a.__num) / long(a.__den) + return int(a.__num) / int(a.__den) # float(a) def __float__(a): @@ -272,38 +272,38 @@ def test(): (3+1.5j) 1.5j (2.25+2.25j) (1+1j) (1.18235814075+2.85446505899j) 1 (3+3j) 0j 4.5j (1+0j) (-0.638110484918+0.705394566962j) 0 ''' - print rat(-1L, 1) - print rat(1, -1) + print(rat(-1, 1)) + print(rat(1, -1)) a = rat(1, 10) - print int(a), long(a), float(a), complex(a) + print(int(a), int(a), float(a), complex(a)) b = rat(2, 5) l = [a+b, a-b, a*b, a/b] - print l + print(l) l.sort() - print l - print rat(0, 1) - print a+1 - print a+1L - print a+1.0 + print(l) + print(rat(0, 1)) + print(a+1) + print(a+1) + print(a+1.0) try: - print rat(1, 0) - raise SystemError, 'should have been ZeroDivisionError' + print(rat(1, 0)) + raise SystemError('should have been ZeroDivisionError') except ZeroDivisionError: - print 'OK' - print rat(2), rat(1.5), rat(3, 2), rat(1.5+1.5j), rat(31415926,10000000) + print('OK') + print(rat(2), rat(1.5), rat(3, 2), rat(1.5+1.5j), rat(31415926,10000000)) list = [2, 1.5, rat(3,2), 1.5+1.5j] for i in list: - print i, + print(i, end=' ') if not isinstance(i, complex): - print int(i), float(i), - print complex(i) - print + print(int(i), float(i), end=' ') + print(complex(i)) + print() for j in list: - print i + j, i - j, i * j, i / j, i ** j, + print(i + j, i - j, i * j, i / j, i ** j, end=' ') if not (isinstance(i, complex) or isinstance(j, complex)): - print cmp(i, j) - print + print(cmp(i, j)) + print() if __name__ == '__main__': diff --git a/Demo/classes/Vec.py b/Demo/classes/Vec.py index 56cb839..edb3147 100755 --- a/Demo/classes/Vec.py +++ b/Demo/classes/Vec.py @@ -27,17 +27,17 @@ class Vec: def __add__(self, other): # Element-wise addition - v = map(lambda x, y: x+y, self, other) + v = list(map(lambda x, y: x+y, self, other)) return Vec().fromlist(v) def __sub__(self, other): # Element-wise subtraction - v = map(lambda x, y: x-y, self, other) + v = list(map(lambda x, y: x-y, self, other)) return Vec().fromlist(v) def __mul__(self, scalar): # Multiply by scalar - v = map(lambda x: x*scalar, self.v) + v = [x*scalar for x in self.v] return Vec().fromlist(v) @@ -45,10 +45,10 @@ class Vec: def test(): a = vec(1, 2, 3) b = vec(3, 2, 1) - print a - print b - print a+b - print a-b - print a*3.0 + print(a) + print(b) + print(a+b) + print(a-b) + print(a*3.0) test() diff --git a/Demo/classes/bitvec.py b/Demo/classes/bitvec.py index 934d33a..97921f2 100755 --- a/Demo/classes/bitvec.py +++ b/Demo/classes/bitvec.py @@ -11,14 +11,14 @@ error = 'bitvec.error' def _check_value(value): if type(value) != type(0) or not 0 <= value < 2: - raise error, 'bitvec() items must have int value 0 or 1' + raise error('bitvec() items must have int value 0 or 1') import math def _compute_len(param): mant, l = math.frexp(float(param)) - bitmask = 1L << l + bitmask = 1 << l if bitmask <= param: raise 'FATAL', '(param, l) = %r' % ((param, l),) while l: @@ -31,11 +31,11 @@ def _compute_len(param): def _check_key(len, key): if type(key) != type(0): - raise TypeError, 'sequence subscript not int' + raise TypeError('sequence subscript not int') if key < 0: key = key + len if not 0 <= key < len: - raise IndexError, 'list index out of range' + raise IndexError('list index out of range') return key def _check_slice(len, i, j): @@ -49,15 +49,15 @@ def _check_slice(len, i, j): class BitVec: def __init__(self, *params): - self._data = 0L + self._data = 0 self._len = 0 if not len(params): pass elif len(params) == 1: param, = params if type(param) == type([]): - value = 0L - bit_mask = 1L + value = 0 + bit_mask = 1 for item in param: # strict check #_check_value(item) @@ -66,39 +66,38 @@ class BitVec: bit_mask = bit_mask << 1 self._data = value self._len = len(param) - elif type(param) == type(0L): + elif type(param) == type(0): if param < 0: - raise error, 'bitvec() can\'t handle negative longs' + raise error('bitvec() can\'t handle negative longs') self._data = param self._len = _compute_len(param) else: - raise error, 'bitvec() requires array or long parameter' + raise error('bitvec() requires array or long parameter') elif len(params) == 2: param, length = params - if type(param) == type(0L): + if type(param) == type(0): if param < 0: - raise error, \ - 'can\'t handle negative longs' + raise error('can\'t handle negative longs') self._data = param if type(length) != type(0): - raise error, 'bitvec()\'s 2nd parameter must be int' + raise error('bitvec()\'s 2nd parameter must be int') computed_length = _compute_len(param) if computed_length > length: - print 'warning: bitvec() value is longer than the length indicates, truncating value' + print('warning: bitvec() value is longer than the length indicates, truncating value') self._data = self._data & \ - ((1L << length) - 1) + ((1 << length) - 1) self._len = length else: - raise error, 'bitvec() requires array or long parameter' + raise error('bitvec() requires array or long parameter') else: - raise error, 'bitvec() requires 0 -- 2 parameter(s)' + raise error('bitvec() requires 0 -- 2 parameter(s)') def append(self, item): #_check_value(item) #self[self._len:self._len] = [item] self[self._len:self._len] = \ - BitVec(long(not not item), 1) + BitVec(int(not not item), 1) def count(self, value): @@ -121,7 +120,7 @@ class BitVec: data = (~self)._data index = 0 if not data: - raise ValueError, 'list.index(x): x not in list' + raise ValueError('list.index(x): x not in list') while not (data & 1): data, index = data >> 1, index + 1 return index @@ -130,7 +129,7 @@ class BitVec: def insert(self, index, item): #_check_value(item) #self[index:index] = [item] - self[index:index] = BitVec(long(not not item), 1) + self[index:index] = BitVec(int(not not item), 1) def remove(self, value): @@ -140,7 +139,7 @@ class BitVec: def reverse(self): #ouch, this one is expensive! #for i in self._len>>1: self[i], self[l-i] = self[l-i], self[i] - data, result = self._data, 0L + data, result = self._data, 0 for i in range(self._len): if not data: result = result << (self._len - i) @@ -151,7 +150,7 @@ class BitVec: def sort(self): c = self.count(1) - self._data = ((1L << c) - 1) << (self._len - c) + self._data = ((1 << c) - 1) << (self._len - c) def copy(self): @@ -199,16 +198,16 @@ class BitVec: def __getitem__(self, key): #rprt('%r.__getitem__(%r)\n' % (self, key)) key = _check_key(self._len, key) - return self._data & (1L << key) != 0 + return self._data & (1 << key) != 0 def __setitem__(self, key, value): #rprt('%r.__setitem__(%r, %r)\n' % (self, key, value)) key = _check_key(self._len, key) #_check_value(value) if value: - self._data = self._data | (1L << key) + self._data = self._data | (1 << key) else: - self._data = self._data & ~(1L << key) + self._data = self._data & ~(1 << key) def __delitem__(self, key): #rprt('%r.__delitem__(%r)\n' % (self, key)) @@ -221,7 +220,7 @@ class BitVec: #rprt('%r.__getslice__(%r, %r)\n' % (self, i, j)) i, j = _check_slice(self._len, i, j) if i >= j: - return BitVec(0L, 0) + return BitVec(0, 0) if i: ndata = self._data >> i else: @@ -230,7 +229,7 @@ class BitVec: if j != self._len: #we'll have to invent faster variants here #e.g. mod_2exp - ndata = ndata & ((1L << nlength) - 1) + ndata = ndata & ((1 << nlength) - 1) return BitVec(ndata, nlength) def __setslice__(self, i, j, sequence, *rest): @@ -250,7 +249,7 @@ class BitVec: #rprt('%r.__delslice__(%r, %r)\n' % (self, i, j)) i, j = _check_slice(self._len, i, j) if i == 0 and j == self._len: - self._data, self._len = 0L, 0 + self._data, self._len = 0, 0 elif i < j: self._data = self[:i]._data | (self[j:]._data >> i) self._len = self._len - j + i @@ -264,18 +263,18 @@ class BitVec: def __mul__(self, multiplier): #rprt('%r.__mul__(%r)\n' % (self, multiplier)) if type(multiplier) != type(0): - raise TypeError, 'sequence subscript not int' + raise TypeError('sequence subscript not int') if multiplier <= 0: - return BitVec(0L, 0) + return BitVec(0, 0) elif multiplier == 1: return self.copy() #handle special cases all 0 or all 1... - if self._data == 0L: - return BitVec(0L, self._len * multiplier) - elif (~self)._data == 0L: - return ~BitVec(0L, self._len * multiplier) + if self._data == 0: + return BitVec(0, self._len * multiplier) + elif (~self)._data == 0: + return ~BitVec(0, self._len * multiplier) #otherwise el cheapo again... - retval = BitVec(0L, 0) + retval = BitVec(0, 0) while multiplier: retval, multiplier = retval + self, multiplier - 1 return retval @@ -309,7 +308,7 @@ class BitVec: def __invert__(self): #rprt('%r.__invert__()\n' % (self,)) - return BitVec(~self._data & ((1L << self._len) - 1), \ + return BitVec(~self._data & ((1 << self._len) - 1), \ self._len) def __coerce__(self, otherseq, *rest): @@ -323,7 +322,7 @@ class BitVec: return int(self._data) def __long__(self): - return long(self._data) + return int(self._data) def __float__(self): return float(self._data) |