[kupfer] plugin.calculator: Calculator plugin



commit 563a527811e10b2b929008c3edf8170b75563985
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Mon Aug 31 16:33:37 2009 +0200

    plugin.calculator: Calculator plugin

 kupfer/plugin/calculator.py |   61 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 61 insertions(+), 0 deletions(-)
---
diff --git a/kupfer/plugin/calculator.py b/kupfer/plugin/calculator.py
new file mode 100644
index 0000000..882d0fa
--- /dev/null
+++ b/kupfer/plugin/calculator.py
@@ -0,0 +1,61 @@
+from __future__ import division
+
+import cmath
+import math
+
+from kupfer.objects import Source, Action, TextLeaf
+from kupfer import pretty
+
+__kupfer_name__ = _("Calculator")
+__kupfer_actions__ = ("Calculate", )
+__description__ = _("Calculate expressions starting with '='")
+__version__ = ""
+__author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
+
+class KupferSurprise (float):
+	def __call__(self):
+		from kupfer import utils, version
+		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)
+
+class Calculate (Action):
+	# since it applies only to special queries, we can up the rank
+	rank_adjust = 10
+	def __init__(self):
+		Action.__init__(self, _("Calculate"))
+
+	def has_result(self):
+		return True
+	def activate(self, leaf):
+		text = leaf.object
+		expr = text.lstrip("= ")
+		environment = dict(math.__dict__)
+		environment.update(cmath.__dict__)
+		# define some constants missing
+		environment["kupfer"] = KupferSurprise("inf")
+		# make the builtins inaccessible
+		environment["__builtins__"] = {}
+		try:
+			result = NiceComplex(eval(expr, environment))
+		except Exception, exc:
+			pretty.print_error(__name__, type(exc).__name__, exc)
+			result = unicode(exc)
+		else:
+			result = unicode(result)
+		return TextLeaf(result)
+
+	def item_types(self):
+		yield TextLeaf
+	def valid_for_item(self, leaf):
+		text = leaf.object
+		return text and text.startswith("=")
+
+	def get_description(self):
+		return _("Calculate expressions starting with '='")



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