158 expr = match.group(1) + str (value) + match.group(2) |
166 expr = match.group(1) + str (value) + match.group(2) |
159 match = rex.match (expr) |
167 match = rex.match (expr) |
160 |
168 |
161 return expr |
169 return expr |
162 |
170 |
|
171 def cmd_calcfunctions (bot, reply, **rest): |
|
172 reply ('Available functions for .calc: %s' % \ |
|
173 ', '.join (sorted ([name for name, data in calc.Functions.iteritems()]))) |
|
174 |
163 def cmd_calc (bot, reply, args, **rest): |
175 def cmd_calc (bot, reply, args, **rest): |
164 expr = args['expression'] |
176 reply (calc.calc (args['expression'])) |
165 |
177 # expr = args['expression'] |
166 try: |
178 # try: |
167 # Substitute some mathematical constants |
179 # Substitute some mathematical constants |
168 expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510) |
180 # expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510) |
169 expr = mathsubstitute (expr, 'e' , 2.71828182845904523536028747135266249775724709369995) |
181 # expr = mathsubstitute (expr, 'e' , 2.71828182845904523536028747135266249775724709369995) |
170 expr = mathsubstitute (expr, 'phi', 1.6180339887498948482) # golden ratio |
182 # expr = mathsubstitute (expr, 'phi', 1.6180339887498948482) # golden ratio |
171 |
183 |
172 result = subprocess.check_output (['calc', '--', expr], stderr=subprocess.STDOUT) \ |
184 # result = subprocess.check_output (['calc', '--', expr], stderr=subprocess.STDOUT) \ |
173 .replace ('\t', '') \ |
185 # .replace ('\t', '') \ |
174 .replace ('\n', '') |
186 # .replace ('\n', '') |
175 |
187 |
176 errmatch = re.compile (r'^.*\bError\b.*$').match (result) |
188 # errmatch = re.compile (r'^.*\bError\b.*$').match (result) |
177 |
189 |
178 if errmatch: |
190 # if errmatch: |
179 command_error ('math error') |
191 # command_error ('math error') |
180 return |
192 # return |
181 |
193 |
182 reply ('Result: %s' % result) |
194 # reply ('Result: %s' % result) |
183 except subprocess.CalledProcessError as e: |
195 # except subprocess.CalledProcessError as e: |
184 command_error (e.output.split('\n')[0]) |
196 # command_error (e.output.split('\n')[0]) |
185 except OSError as e: |
197 # except OSError as e: |
186 command_error ('failed to execute calc: ' + e.strerror) |
198 # command_error ('failed to execute calc: ' + e.strerror) |
187 |
199 |
188 def cmd_more (commandObject, **rest): |
200 def cmd_more (commandObject, **rest): |
189 ModuleCore.print_responses (commandObject) |
201 ModuleCore.print_responses (commandObject) |
190 |
202 |
191 def cmd_yes (**k): |
203 def cmd_yes (**k): |