- added e and phi to .calc

Sun, 30 Nov 2014 03:51:39 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 30 Nov 2014 03:51:39 +0200
changeset 106
31583e5b2f49
parent 105
84bd617a2520
child 107
14e1d18d2a2a

- added e and phi to .calc

mod_util.py file | annotate | diff | comparison | revisions
--- a/mod_util.py	Sun Nov 30 03:39:30 2014 +0200
+++ b/mod_util.py	Sun Nov 30 03:51:39 2014 +0200
@@ -118,17 +118,24 @@
 
 	bot.privmsg (replyto, '%s %s: %s' % (cmd['name'], cmd['args'], cmd['description']))
 
+def mathsubstitute (expr, token, value):
+	rex = re.compile (r'^(.*)\b' + token + r'\b(.*)$')
+	match = rex.match (expr)
+
+	while match:
+		expr = match.group(1) + str (value) + match.group(2)
+		match = rex.match (expr)
+
+	return expr
+
 def cmd_calc (bot, replyto, args, **rest):
 	expr = args['expression']
 
 	try:
-		# I want pi around so yeah
-		pirex = re.compile (r'^(.*)\bpi\b(.*)$')
-		match = pirex.match (expr)
-
-		while match:
-			expr = match.group(1) + '3.14159265358979323846264338327950288' + match.group(2)
-			match = pirex.match (expr)
+		# Substitute some mathematical constants
+		expr = mathsubstitute (expr, 'pi' , 3.141592653589793)
+		expr = mathsubstitute (expr, 'e'  , 2.718281828459045)
+		expr = mathsubstitute (expr, 'phi', 1.618033988749895) # golden ratio
 
 		result = subprocess.check_output (['calc', expr], stderr=subprocess.STDOUT) \
 			.replace ('\t', '') \

mercurial