49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
|
#!/usr/bin/env python3
|
||
|
"""
|
||
|
A basic calculator and python interpreter application.
|
||
|
"""
|
||
|
import sys
|
||
|
|
||
|
import requests
|
||
|
|
||
|
from module import commands, example
|
||
|
from tools.calculation import eval_equation
|
||
|
|
||
|
BASE_TUMBOLIA_URI = 'https://tumbolia-two.appspot.com/'
|
||
|
|
||
|
@commands('c', 'calc')
|
||
|
@example('.c 5 + 3', '8')
|
||
|
def c(bot, trigger):
|
||
|
"""Evaluate some calculation."""
|
||
|
if not trigger.group(2):
|
||
|
return bot.reply("Nothing to calculate.")
|
||
|
# Account for the silly non-Anglophones and their silly radix point.
|
||
|
eqn = trigger.group(2).replace(',', '.')
|
||
|
try:
|
||
|
result = eval_equation(eqn)
|
||
|
result = "{:.10g}".format(result)
|
||
|
except ZeroDivisionError:
|
||
|
result = "Division by zero is not supported in this universe."
|
||
|
except Exception as e:
|
||
|
result = "{error}: {msg}".format(error=type(e), msg=e)
|
||
|
bot.reply(result)
|
||
|
|
||
|
|
||
|
@commands('py')
|
||
|
@example('.py len([1,2,3])', '3')
|
||
|
def py(bot, trigger):
|
||
|
"""Evaluate a Python expression."""
|
||
|
if not trigger.group(2):
|
||
|
return bot.say("Need an expression to evaluate")
|
||
|
|
||
|
query = trigger.group(2)
|
||
|
uri = BASE_TUMBOLIA_URI + 'py/'
|
||
|
res = requests.get(uri + query)
|
||
|
res.raise_for_status()
|
||
|
answer = res.text
|
||
|
if answer:
|
||
|
#bot.say can potentially lead to 3rd party commands triggering.
|
||
|
bot.say(answer)
|
||
|
else:
|
||
|
bot.reply('Sorry, no result.')
|