# HG changeset patch # User Teemu Piippo # Date 1417312299 -7200 # Node ID 31583e5b2f4905ab0db456cfd6d836a8c6f5915e # Parent 84bd617a25204e5558082eec4d02a37046a99f0b - added e and phi to .calc diff -r 84bd617a2520 -r 31583e5b2f49 mod_util.py --- 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', '') \