More Python 3 support

Mon, 11 Jan 2016 18:29:14 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 11 Jan 2016 18:29:14 +0200
changeset 164
e18f73e4c2e2
parent 163
ed3d52b37f19
child 165
8131cf387e3d

More Python 3 support

calculator.py file | annotate | diff | comparison | revisions
fraction.py file | annotate | diff | comparison | revisions
mod_util.py file | annotate | diff | comparison | revisions
--- a/calculator.py	Mon Jan 11 18:28:19 2016 +0200
+++ b/calculator.py	Mon Jan 11 18:29:14 2016 +0200
@@ -189,7 +189,10 @@
 		OperatorSymbols[op.symbol] = [op]
 
 def sgn (x):
-	return cmp (x, 0)
+	if abs(x) < 1e-10:
+		return 0
+	else:
+		return x / abs(x)
 
 def integerclamp (x, bits, signed):
 	x = x % (2 ** bits)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fraction.py	Mon Jan 11 18:29:14 2016 +0200
@@ -0,0 +1,96 @@
+def gcd(a, b):
+	"""
+	Calculate the Greatest Common Divisor of a and b.
+
+	Unless b==0, the result will have the same sign as b (so that when
+	b is divided by it, the result comes out positive).
+	"""
+	while b:
+		a, b = b, a % b
+	return a
+
+class Fraction (object):
+	def __init__ (self, numerator, denominator = 1):
+		if denominator == 0:
+			raise ValueError ('Denominator may not be 0')
+
+		self.numerator = int (numerator)
+		self.denominator = int (denominator)
+		self.simplify()
+
+	@property
+	def real (self):
+		if self.numerator.real:
+			return Fraction(self.numerator.real, self.denominator.real)
+		else:
+			return Fraction(0,1)
+
+	@property
+	def imag (self):
+		if self.numerator.imag:
+			return Fraction(self.numerator.imag, self.denominator.imag)
+		else:
+			return Fraction(0,1)
+
+	def simplify (self):
+		divisor = gcd (self.numerator, self.denominator)
+		self.numerator //= divisor
+		self.denominator //= divisor
+
+	def __repr__ (self):
+		return '%s(%d, %d)' % (self.__class__.__name__, self.numerator, self.denominator)
+
+	def __str__ (self):
+		if self.numerator > self.denominator:
+			if self.numerator % self.denominator == 0:
+				return str (self.numerator // self.denominator)
+			else:
+				return '%d + (%d / %d)' % (self.numerator // self.denominator,
+					self.numerator % self.denominator, self.denominator)
+		else:
+			return str (self.numerator) + '/' + str (self.denominator)
+
+	def __add__ (self, other):
+		if isinstance (other, Fraction):
+			n1, n2, d1, d2 = (self.numerator, other.numerator, self.denominator, other.denominator)
+			return Fraction ((n1 * d2) + (n2 * d1), d1 * d2)
+		else:
+			return Fraction (self.numerator + (other * self.denominator), self.denominator)
+
+	def __radd__ (self, other):
+		return self + other
+
+	def __sub__ (self, other):
+		if isinstance (other, Fraction):
+			return self.__add__ (Fraction (-other.numerator, other.denominator))
+		else:
+			return self.__add__ (-other)
+
+	def __rsub__ (self, other):
+		return -self + other
+
+	def __mul__ (self, other):
+		if isinstance (other, Fraction):
+			return Fraction (self.numerator * other.numerator, self.denominator * other.denominator)
+		elif isinstance (other, float):
+			return (self.numerator / self.denominator) * other
+		elif isinstance (other, int):
+			return Fraction (self.numerator * int (other), self.denominator)
+
+	def __rmul__ (self, other):
+		return self * other
+
+	def __truediv__ (self, other):
+		if isinstance (other, Fraction):
+			return Fraction (self.numerator * other.denominator, self.denominator * other.numerator)
+		else:
+			return Fraction (self.numerator, self.denominator * int (other))
+
+	def __rtruediv__ (self, other):
+		return other * Fraction (self.denominator, self.numerator)
+
+	def __neg__ (self):
+		return Fraction (-self.numerator, self.denominator)
+
+	def __abs__ (self):
+		return Fraction (abs (self.numerator), self.denominator)
\ No newline at end of file
--- a/mod_util.py	Mon Jan 11 18:28:19 2016 +0200
+++ b/mod_util.py	Mon Jan 11 18:29:14 2016 +0200
@@ -124,7 +124,7 @@
 @modulecore.irc_command()
 def calcfunctions (bot, reply, **rest):
 	'''Lists the functions supported by .calc.'''
-	names = ', '.join (sorted (name for name, data in calculator.Functions.items()))
+	names = ', '.join (sorted (calculator.Functions.keys()))
 	reply ('Available functions for .calc:', names)
 
 @modulecore.irc_command (args='<expression...>')

mercurial