mod_util.py

changeset 106
31583e5b2f49
parent 105
84bd617a2520
child 107
14e1d18d2a2a
equal deleted inserted replaced
105:84bd617a2520 106:31583e5b2f49
116 if not ModuleCore.is_available (cmd, ident, host): 116 if not ModuleCore.is_available (cmd, ident, host):
117 command_error ('you may not use %s' % cmd['name']) 117 command_error ('you may not use %s' % cmd['name'])
118 118
119 bot.privmsg (replyto, '%s %s: %s' % (cmd['name'], cmd['args'], cmd['description'])) 119 bot.privmsg (replyto, '%s %s: %s' % (cmd['name'], cmd['args'], cmd['description']))
120 120
121 def mathsubstitute (expr, token, value):
122 rex = re.compile (r'^(.*)\b' + token + r'\b(.*)$')
123 match = rex.match (expr)
124
125 while match:
126 expr = match.group(1) + str (value) + match.group(2)
127 match = rex.match (expr)
128
129 return expr
130
121 def cmd_calc (bot, replyto, args, **rest): 131 def cmd_calc (bot, replyto, args, **rest):
122 expr = args['expression'] 132 expr = args['expression']
123 133
124 try: 134 try:
125 # I want pi around so yeah 135 # Substitute some mathematical constants
126 pirex = re.compile (r'^(.*)\bpi\b(.*)$') 136 expr = mathsubstitute (expr, 'pi' , 3.141592653589793)
127 match = pirex.match (expr) 137 expr = mathsubstitute (expr, 'e' , 2.718281828459045)
128 138 expr = mathsubstitute (expr, 'phi', 1.618033988749895) # golden ratio
129 while match:
130 expr = match.group(1) + '3.14159265358979323846264338327950288' + match.group(2)
131 match = pirex.match (expr)
132 139
133 result = subprocess.check_output (['calc', expr], stderr=subprocess.STDOUT) \ 140 result = subprocess.check_output (['calc', expr], stderr=subprocess.STDOUT) \
134 .replace ('\t', '') \ 141 .replace ('\t', '') \
135 .replace ('\n', '') 142 .replace ('\n', '')
136 143

mercurial