[kupfer] plugin.calculator: Tweak how result is presented



commit 9b9bf876ff6278836972be7de432103663381120
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Thu Sep 10 05:07:05 2009 +0200

    plugin.calculator: Tweak how result is presented

 kupfer/plugin/calculator.py |   25 +++++++++++++------------
 1 files changed, 13 insertions(+), 12 deletions(-)
---
diff --git a/kupfer/plugin/calculator.py b/kupfer/plugin/calculator.py
index 37981be..c9ce934 100644
--- a/kupfer/plugin/calculator.py
+++ b/kupfer/plugin/calculator.py
@@ -18,12 +18,14 @@ class KupferSurprise (float):
 		utils.show_url(version.WEBSITE)
 		return self
 
-class NiceComplex (complex):
-	def __unicode__(self):
-		if not self.imag:
-			return unicode(self.real)
-		else:
-			return u"%s+%sj" % (self.real, self.imag)
+def format_result(res):
+	cres = complex(res)
+	parts = []
+	if cres.real:
+		parts.append(u"%s" % cres.real)
+	if cres.imag:
+		parts.append(u"%s" % complex(0, cres.imag))
+	return u"+".join(parts) or u"%s" % res
 
 class Calculate (Action):
 	# since it applies only to special queries, we can up the rank
@@ -45,14 +47,13 @@ class Calculate (Action):
 		# make the builtins inaccessible
 		environment["__builtins__"] = {}
 		try:
-			result = NiceComplex(eval(expr, environment))
+			result = eval(expr, environment)
+			resultstr = format_result(result)
+			self.last_result = result
 		except Exception, exc:
 			pretty.print_error(__name__, type(exc).__name__, exc)
-			result = unicode(exc)
-		else:
-			self.last_result = result
-			result = unicode(result)
-		return TextLeaf(result)
+			resultstr = unicode(exc)
+		return TextLeaf(resultstr)
 
 	def item_types(self):
 		yield TextLeaf



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]