|
1
|
|
|
# coding=utf-8 |
|
2
|
|
|
""" |
|
3
|
|
|
calc.py - Sopel Calculator Module |
|
4
|
|
|
Copyright 2008, Sean B. Palmer, inamidst.com |
|
5
|
|
|
Licensed under the Eiffel Forum License 2. |
|
6
|
|
|
|
|
7
|
|
|
https://sopel.chat |
|
8
|
|
|
""" |
|
9
|
|
|
from __future__ import unicode_literals, absolute_import, print_function, division |
|
10
|
|
|
|
|
11
|
|
|
from sopel.module import commands, example |
|
12
|
|
|
from sopel.tools.calculation import eval_equation |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
@commands('c', 'calc') |
|
16
|
|
|
@example('.c 5 + 3', '8') |
|
17
|
|
|
@example('.c 0.9*10', '9') |
|
18
|
|
|
@example('.c 10*0.9', '9') |
|
19
|
|
|
@example('.c 2*(1+2)*3', '18') |
|
20
|
|
|
@example('.c 2**10', '1024') |
|
21
|
|
|
@example('.c 5 // 2', '2') |
|
22
|
|
|
@example('.c 5 / 2', '2.5') |
|
23
|
|
|
def c(bot, trigger): |
|
24
|
|
|
"""Evaluate some calculation.""" |
|
25
|
|
|
if not trigger.group(2): |
|
26
|
|
|
return bot.reply("Nothing to calculate.") |
|
27
|
|
|
# Account for the silly non-Anglophones and their silly radix point. |
|
28
|
|
|
eqn = trigger.group(2).replace(',', '.') |
|
29
|
|
|
try: |
|
30
|
|
|
result = eval_equation(eqn) |
|
31
|
|
|
result = "{:.10g}".format(result) |
|
32
|
|
|
except ZeroDivisionError: |
|
33
|
|
|
result = "Division by zero is not supported in this universe." |
|
34
|
|
|
except Exception as e: |
|
35
|
|
|
result = "{error}: {msg}".format(error=type(e), msg=e) |
|
36
|
|
|
bot.reply(result) |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
if __name__ == "__main__": |
|
40
|
|
|
from sopel.test_tools import run_example_tests |
|
41
|
|
|
run_example_tests(__file__) |
|
42
|
|
|
|